指针解引用

233 阅读1分钟

指针变量

this->current_var_init_symbol->accept(*this);

SYM_REF1(  ref_expression_c, exp)

SYM_REF1(symbolic_variable_c, var_name)

void *visit(symbolic_variable_c *symbol) {
  TRACE("symbolic_variable_c");
  this->print_variable_prefix();
  symbol->var_name->accept(*this);
  return NULL;
}

基本变量

this->current_var_init_symbol->accept(*this);

SYM_TOKEN(integer_c)

void *visit(integer_c *symbol) {return print_striped_token(symbol);}

指针表达式(REF(A))

symbol->exp->accept(*this);

SYM_REF1(symbolic_variable_c, var_name)

void *visit(symbolic_variable_c *symbol)

symbol->var_name->accept(*this); //generate_c_base_c::visit(symbol);

SYM_TOKEN(identifier_c)

void *visit(identifier_c *symbol) {return print_token(symbol);}

指针解引用

  • ST程序
PROGRAM prg
  VAR
  	A : INT := 250;
  	pt : REF_TO INT := REF(A);
  	B : INT;
  END_VAR
  
	B := pt^;
	pt^ := 520;
END_PROGRAM


CONFIGURATION Config0

  RESOURCE Res0 ON PLC
    TASK task0(INTERVAL := T#20ms,PRIORITY := 1);
    PROGRAM instance0 WITH task0 : prg;
  END_RESOURCE
END_CONFIGURATION
  • POUS.c
void PRG_init__(PRG *data__, BOOL retain) {
  __INIT_VAR(data__->A,250,retain)
  __INIT_VAR(data__->PT,((__GET_VAR_REF(data__->A,))),retain)
  __INIT_VAR(data__->B,0,retain)
}

// Code part
void PRG_body__(PRG *data__) {
  // Initialise TEMP variables

  __SET_VAR(data__->,B,,(*__GET_VAR(data__->PT,)));
  __SET_VAR(data__->,(*PT),(*),520);

  goto __end;

__end:
  return;
} // PRG_body__() 
  • POUS.h
#ifndef __POUS_H
#define __POUS_H

#include "accessor.h"
#include "iec_std_lib.h"

#include <time.h>
__attribute__((weak)) TIME __CURRENT_TIME;
__attribute__((weak)) BOOL __DEBUG;

__DECLARE_REFTO_TYPE(__REF_TO_INT, INT*)
// PROGRAM PRG
// Data part
typedef struct {
  // PROGRAM Interface - IN, OUT, IN_OUT variables

  // PROGRAM private variables - TEMP, private and located variables
  __DECLARE_VAR(INT,A)
  __DECLARE_VAR(__REF_TO_INT,PT)
  __DECLARE_VAR(INT,B)

} PRG;

void PRG_init__(PRG *data__, BOOL retain);
// Code part
void PRG_body__(PRG *data__);
#endif //__POUS_H
  • 相关宏定义
#define __SET_VAR(prefix, name, suffix, new_value)\
	if (!(prefix name.flags & __IEC_FORCE_FLAG)) prefix name.value suffix = new_value
#define __DECLARE_IEC_TYPE(type)\
typedef IEC_##type type;\
\
typedef struct {\
  IEC_##type value;\
  IEC_BYTE flags;\
} __IEC_##type##_t;\
\
typedef struct {\
  IEC_##type *value;\
  IEC_BYTE flags;\
  IEC_##type fvalue;\
} __IEC_##type##_p;
  • 相关宏展开
__DECLARE_REFTO_TYPE(__REF_TO_INT, INT*)
=> __REF_TO_INT value;
=>  REFTO_BYTE flags;