stage4/generate_c/generate_c_il.cc
changeset 210 8387cac2aba6
parent 208 c72748a12ae3
child 211 5249d313ff7c
equal deleted inserted replaced
209:30cbae46190d 210:8387cac2aba6
   200      * of the boolean data type to keep as a reference of the current
   200      * of the boolean data type to keep as a reference of the current
   201      * data type.
   201      * data type.
   202      * The following object is it...
   202      * The following object is it...
   203      */
   203      */
   204     bool_type_name_c bool_type;
   204     bool_type_name_c bool_type;
       
   205     lint_type_name_c lint_type;
       
   206     lword_type_name_c lword_type;
       
   207     lreal_type_name_c lreal_type;
   205 
   208 
   206     /* the data type of the IL default variable... */
   209     /* the data type of the IL default variable... */
   207     #define IL_DEFVAR_T VAR_LEADER "IL_DEFVAR_T"
   210     #define IL_DEFVAR_T VAR_LEADER "IL_DEFVAR_T"
   208     /* The name of the IL default variable... */
   211     /* The name of the IL default variable... */
   209     #define IL_DEFVAR   VAR_LEADER "IL_DEFVAR"
   212     #define IL_DEFVAR   VAR_LEADER "IL_DEFVAR"
   377         s4o.print("if (!");
   380         s4o.print("if (!");
   378         this->default_variable_name.accept(*this);
   381         this->default_variable_name.accept(*this);
   379         s4o.print(") ");
   382         s4o.print(") ");
   380       }
   383       }
   381       else {ERROR;}
   384       else {ERROR;}
       
   385     }
       
   386 
       
   387     void BYTE_operator_result_type(void) {
       
   388       if (search_expression_type->is_literal_integer_type(this->default_variable_name.current_type)) {
       
   389         if (search_expression_type->is_literal_integer_type(this->current_operand_type))
       
   390           this->default_variable_name.current_type = &(this->lword_type);
       
   391         else
       
   392           this->default_variable_name.current_type = this->current_operand_type;
       
   393       }
       
   394       else if (search_expression_type->is_literal_integer_type(this->current_operand_type))
       
   395     	  this->current_operand_type = this->default_variable_name.current_type;
       
   396     }
       
   397 
       
   398     void NUM_operator_result_type(void) {
       
   399       if (search_expression_type->is_literal_real_type(this->default_variable_name.current_type)) {
       
   400         if (search_expression_type->is_literal_integer_type(this->current_operand_type) ||
       
   401             search_expression_type->is_literal_real_type(this->current_operand_type))
       
   402           this->default_variable_name.current_type = &(this->lreal_type);
       
   403         else
       
   404           this->default_variable_name.current_type = this->current_operand_type;
       
   405       }
       
   406       else if (search_expression_type->is_literal_integer_type(this->default_variable_name.current_type)) {
       
   407         if (search_expression_type->is_literal_integer_type(this->current_operand_type))
       
   408           this->default_variable_name.current_type = &(this->lint_type);
       
   409         else if (search_expression_type->is_literal_real_type(this->current_operand_type))
       
   410           this->default_variable_name.current_type = &(this->lreal_type);
       
   411         else
       
   412           this->default_variable_name.current_type = this->current_operand_type;
       
   413       }
       
   414       else if (search_expression_type->is_literal_integer_type(this->current_operand_type) ||
       
   415                search_expression_type->is_literal_real_type(this->current_operand_type))
       
   416         this->current_operand_type = this->default_variable_name.current_type;
   382     }
   417     }
   383 
   418 
   384 
   419 
   385 public:
   420 public:
   386 void *visit(il_default_variable_c *symbol) {
   421 void *visit(il_default_variable_c *symbol) {
  1149 /* B 2.2 Operators */
  1184 /* B 2.2 Operators */
  1150 /*******************/
  1185 /*******************/
  1151 
  1186 
  1152 void *visit(LD_operator_c *symbol)	{
  1187 void *visit(LD_operator_c *symbol)	{
  1153   /* the data type resulting from this operation... */
  1188   /* the data type resulting from this operation... */
       
  1189   if (search_expression_type->is_literal_integer_type(this->current_operand_type))
       
  1190 	  this->default_variable_name.current_type = &(this->lint_type);
       
  1191   else if (search_expression_type->is_literal_real_type(this->current_operand_type))
       
  1192   	  this->default_variable_name.current_type = &(this->lreal_type);
       
  1193   else
       
  1194 	  this->default_variable_name.current_type = this->current_operand_type;
       
  1195   XXX_operator(&(this->default_variable_name), " = ", this->current_operand);
  1154   this->default_variable_name.current_type = this->current_operand_type;
  1196   this->default_variable_name.current_type = this->current_operand_type;
  1155   XXX_operator(&(this->default_variable_name), " = ", this->current_operand);
       
  1156   return NULL;
  1197   return NULL;
  1157 }
  1198 }
  1158 
  1199 
  1159 void *visit(LDN_operator_c *symbol)	{
  1200 void *visit(LDN_operator_c *symbol)	{
  1160   /* the data type resulting from this operation... */
  1201   /* the data type resulting from this operation... */
  1161   this->default_variable_name.current_type = this->current_operand_type;
  1202   if (search_expression_type->is_literal_integer_type(this->current_operand_type))
       
  1203     this->default_variable_name.current_type = &(this->lword_type);
       
  1204   else
       
  1205     this->default_variable_name.current_type = this->current_operand_type;
  1162   XXX_operator(&(this->default_variable_name),
  1206   XXX_operator(&(this->default_variable_name),
  1163                search_expression_type->is_bool_type(this->current_operand_type)?" = !":" = ~",
  1207                search_expression_type->is_bool_type(this->current_operand_type)?" = !":" = ~",
  1164                this->current_operand);
  1208                this->current_operand);
  1165   return NULL;
  1209   return NULL;
  1166 }
  1210 }
  1173   if (search_base_type.type_is_subrange(operand_type)) {
  1217   if (search_base_type.type_is_subrange(operand_type)) {
  1174     s4o.print("__CHECK_");
  1218     s4o.print("__CHECK_");
  1175     operand_type->accept(*this);
  1219     operand_type->accept(*this);
  1176     s4o.print("(");
  1220     s4o.print("(");
  1177   }
  1221   }
       
  1222   if (search_expression_type->is_literal_integer_type(this->default_variable_name.current_type) ||
       
  1223 	  search_expression_type->is_literal_real_type(this->default_variable_name.current_type))
       
  1224     this->default_variable_name.current_type = this->current_operand_type;
  1178   this->default_variable_name.accept(*this);
  1225   this->default_variable_name.accept(*this);
  1179   if (search_base_type.type_is_subrange(operand_type))
  1226   if (search_base_type.type_is_subrange(operand_type))
  1180     s4o.print(")");
  1227     s4o.print(")");
  1181   /* the data type resulting from this operation is unchamged. */
  1228   /* the data type resulting from this operation is unchamged. */
  1182   return NULL;
  1229   return NULL;
  1194   }
  1241   }
  1195   if (search_expression_type->is_bool_type(this->current_operand_type))
  1242   if (search_expression_type->is_bool_type(this->current_operand_type))
  1196     s4o.print("!");
  1243     s4o.print("!");
  1197   else
  1244   else
  1198     s4o.print("~");
  1245     s4o.print("~");
       
  1246   if (search_expression_type->is_literal_integer_type(this->default_variable_name.current_type))
       
  1247 	this->default_variable_name.current_type = this->current_operand_type;
  1199   this->default_variable_name.accept(*this);
  1248   this->default_variable_name.accept(*this);
  1200   if (search_base_type.type_is_subrange(operand_type))
  1249   if (search_base_type.type_is_subrange(operand_type))
  1201     s4o.print(")");
  1250     s4o.print(")");
  1202   /* the data type resulting from this operation is unchamged. */
  1251   /* the data type resulting from this operation is unchamged. */
  1203   return NULL;
  1252   return NULL;
  1242 void *visit(PT_operator_c *symbol)	{return XXX_CAL_operator("PT", this->current_operand);}
  1291 void *visit(PT_operator_c *symbol)	{return XXX_CAL_operator("PT", this->current_operand);}
  1243 
  1292 
  1244 void *visit(AND_operator_c *symbol)	{
  1293 void *visit(AND_operator_c *symbol)	{
  1245   if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
  1294   if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
  1246       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
  1295       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
  1247     XXX_operator(&(this->default_variable_name), " &= ", this->current_operand);
  1296 	BYTE_operator_result_type();
       
  1297 	XXX_operator(&(this->default_variable_name), " &= ", this->current_operand);
  1248     /* the data type resulting from this operation... */
  1298     /* the data type resulting from this operation... */
  1249     this->default_variable_name.current_type = this->current_operand_type;
  1299     this->default_variable_name.current_type = this->current_operand_type;
  1250   }
  1300   }
  1251   else {ERROR;}
  1301   else {ERROR;}
  1252   return NULL;
  1302   return NULL;
  1253 }
  1303 }
  1254 
  1304 
  1255 void *visit(OR_operator_c *symbol)	{
  1305 void *visit(OR_operator_c *symbol)	{
  1256   if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
  1306   if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
  1257       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
  1307       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
  1258     XXX_operator(&(this->default_variable_name), " |= ", this->current_operand);
  1308 	BYTE_operator_result_type();
       
  1309 	XXX_operator(&(this->default_variable_name), " |= ", this->current_operand);
  1259     /* the data type resulting from this operation... */
  1310     /* the data type resulting from this operation... */
  1260     this->default_variable_name.current_type = this->current_operand_type;
  1311     this->default_variable_name.current_type = this->current_operand_type;
  1261   }
  1312   }
  1262   else {ERROR;}
  1313   else {ERROR;}
  1263   return NULL;
  1314   return NULL;
  1264 }
  1315 }
  1265 
  1316 
  1266 void *visit(XOR_operator_c *symbol)	{
  1317 void *visit(XOR_operator_c *symbol)	{
  1267   if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
  1318   if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
  1268       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
  1319       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
  1269     // '^' is a bit by bit exclusive OR !! Also seems to work with boolean types!
  1320 	BYTE_operator_result_type();
       
  1321 	// '^' is a bit by bit exclusive OR !! Also seems to work with boolean types!
  1270     XXX_operator(&(this->default_variable_name), " ^= ", this->current_operand);
  1322     XXX_operator(&(this->default_variable_name), " ^= ", this->current_operand);
  1271     /* the data type resulting from this operation... */
  1323     /* the data type resulting from this operation... */
  1272     this->default_variable_name.current_type = this->current_operand_type;
  1324     this->default_variable_name.current_type = this->current_operand_type;
  1273   }
  1325   }
  1274   else {ERROR;}
  1326   else {ERROR;}
  1276 }
  1328 }
  1277 
  1329 
  1278 void *visit(ANDN_operator_c *symbol)	{
  1330 void *visit(ANDN_operator_c *symbol)	{
  1279   if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
  1331   if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
  1280       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
  1332       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
  1281     XXX_operator(&(this->default_variable_name),
  1333 	BYTE_operator_result_type();
       
  1334 	XXX_operator(&(this->default_variable_name),
  1282                  search_expression_type->is_bool_type(this->current_operand_type)?" &= !":" &= ~",
  1335                  search_expression_type->is_bool_type(this->current_operand_type)?" &= !":" &= ~",
  1283                  this->current_operand);
  1336                  this->current_operand);
  1284     /* the data type resulting from this operation... */
  1337     /* the data type resulting from this operation... */
  1285     this->default_variable_name.current_type = this->current_operand_type;
  1338     this->default_variable_name.current_type = this->current_operand_type;
  1286   }
  1339   }
  1289 }
  1342 }
  1290 
  1343 
  1291 void *visit(ORN_operator_c *symbol)	{
  1344 void *visit(ORN_operator_c *symbol)	{
  1292   if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
  1345   if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
  1293       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
  1346       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
  1294     XXX_operator(&(this->default_variable_name),
  1347 	BYTE_operator_result_type();
       
  1348 	XXX_operator(&(this->default_variable_name),
  1295                  search_expression_type->is_bool_type(this->current_operand_type)?" |= !":" |= ~",
  1349                  search_expression_type->is_bool_type(this->current_operand_type)?" |= !":" |= ~",
  1296                  this->current_operand);
  1350                  this->current_operand);
  1297     /* the data type resulting from this operation... */
  1351     /* the data type resulting from this operation... */
  1298     this->default_variable_name.current_type = this->current_operand_type;
  1352     this->default_variable_name.current_type = this->current_operand_type;
  1299   }
  1353   }
  1302 }
  1356 }
  1303 
  1357 
  1304 void *visit(XORN_operator_c *symbol)	{
  1358 void *visit(XORN_operator_c *symbol)	{
  1305   if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
  1359   if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
  1306       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
  1360       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
  1307     XXX_operator(&(this->default_variable_name),
  1361 	BYTE_operator_result_type();
       
  1362 	XXX_operator(&(this->default_variable_name),
  1308                  // bit by bit exclusive OR !! Also seems to work with boolean types!
  1363                  // bit by bit exclusive OR !! Also seems to work with boolean types!
  1309                  search_expression_type->is_bool_type(this->current_operand_type)?" ^= !":" ^= ~",
  1364                  search_expression_type->is_bool_type(this->current_operand_type)?" ^= !":" ^= ~",
  1310                  this->current_operand);
  1365                  this->current_operand);
  1311     /* the data type resulting from this operation... */
  1366     /* the data type resulting from this operation... */
  1312     this->default_variable_name.current_type = this->current_operand_type;
  1367     this->default_variable_name.current_type = this->current_operand_type;
  1323     this->default_variable_name.current_type = this->current_operand_type;
  1378     this->default_variable_name.current_type = this->current_operand_type;
  1324     return NULL;
  1379     return NULL;
  1325   }
  1380   }
  1326   if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
  1381   if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
  1327       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
  1382       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
  1328     XXX_operator(&(this->default_variable_name), " += ", this->current_operand);
  1383 	NUM_operator_result_type();
       
  1384 	XXX_operator(&(this->default_variable_name), " += ", this->current_operand);
  1329     /* the data type resulting from this operation... */
  1385     /* the data type resulting from this operation... */
  1330     this->default_variable_name.current_type = this->current_operand_type;
  1386     this->default_variable_name.current_type = this->current_operand_type;
  1331     return NULL;
  1387     return NULL;
  1332   }
  1388   }
  1333   ERROR;
  1389   ERROR;
  1342     this->default_variable_name.current_type = this->current_operand_type;
  1398     this->default_variable_name.current_type = this->current_operand_type;
  1343     return NULL;
  1399     return NULL;
  1344   }
  1400   }
  1345   if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
  1401   if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
  1346       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
  1402       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
  1347     XXX_operator(&(this->default_variable_name), " -= ", this->current_operand);
  1403 	NUM_operator_result_type();
       
  1404 	XXX_operator(&(this->default_variable_name), " -= ", this->current_operand);
  1348     /* the data type resulting from this operation... */
  1405     /* the data type resulting from this operation... */
  1349     this->default_variable_name.current_type = this->current_operand_type;
  1406     this->default_variable_name.current_type = this->current_operand_type;
  1350     return NULL;
  1407     return NULL;
  1351   }
  1408   }
  1352   ERROR;
  1409   ERROR;
  1360     /* the data type resulting from this operation... */
  1417     /* the data type resulting from this operation... */
  1361     return NULL;
  1418     return NULL;
  1362   }
  1419   }
  1363   if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
  1420   if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
  1364       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
  1421       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
       
  1422 	NUM_operator_result_type();
  1365     XXX_operator(&(this->default_variable_name), " *= ", this->current_operand);
  1423     XXX_operator(&(this->default_variable_name), " *= ", this->current_operand);
  1366     /* the data type resulting from this operation... */
  1424     /* the data type resulting from this operation... */
  1367     this->default_variable_name.current_type = this->current_operand_type;
  1425     this->default_variable_name.current_type = this->current_operand_type;
  1368     return NULL;
  1426     return NULL;
  1369   }
  1427   }
  1378     /* the data type resulting from this operation... */
  1436     /* the data type resulting from this operation... */
  1379     return NULL;
  1437     return NULL;
  1380   }
  1438   }
  1381   if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
  1439   if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
  1382       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
  1440       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
  1383     XXX_operator(&(this->default_variable_name), " /= ", this->current_operand);
  1441 	NUM_operator_result_type();
       
  1442 	XXX_operator(&(this->default_variable_name), " /= ", this->current_operand);
  1384     /* the data type resulting from this operation... */
  1443     /* the data type resulting from this operation... */
  1385     this->default_variable_name.current_type = this->current_operand_type;
  1444     this->default_variable_name.current_type = this->current_operand_type;
  1386   }
  1445   }
  1387   ERROR;
  1446   ERROR;
  1388   return NULL;
  1447   return NULL;
  1389 }
  1448 }
  1390 
  1449 
  1391 void *visit(MOD_operator_c *symbol)	{
  1450 void *visit(MOD_operator_c *symbol)	{
  1392   if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
  1451   if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
  1393       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
  1452       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
  1394     XXX_operator(&(this->default_variable_name), " %= ", this->current_operand);
  1453 	NUM_operator_result_type();
       
  1454 	XXX_operator(&(this->default_variable_name), " %= ", this->current_operand);
  1395     /* the data type resulting from this operation... */
  1455     /* the data type resulting from this operation... */
  1396     this->default_variable_name.current_type = this->current_operand_type;
  1456     this->default_variable_name.current_type = this->current_operand_type;
  1397   }
  1457   }
  1398   ERROR;
  1458   ERROR;
  1399   return NULL;
  1459   return NULL;