static void print_ident(Parser* p, SsaVar* var)
{
Symbol* s = symbol_getbyid(p, var->symid);
- printf("%s.%lu", s->name, var->symver);
+ char* name = (s->name[0] == '$' ? "" : s->name);
+ bool is_global = (s->global || s->class == SYM_PROC) && (s->class != SYM_TYPE);
+ char scope = (is_global ? '@' : '%');
+ printf("%c%s.%lu", scope, name, var->symver);
}
-static void print_dest(Parser* p, SsaNode* node)
-{
- if (node->mode != MODE_CONTROL)
- {
- print_ident(p, &(node->dest));
- printf(" = ");
- }
-}
-
-static void print_opcode(SsaNode* node)
-{
- int op = node->code;
- if (op < 256)
- {
- printf(" %c ", op);
- }
- else if (op == RETURN)
- {
- printf(" return ");
- }
- else if (op == IF)
- {
- printf("if ");
- }
- else if (op == EQ)
- {
- printf(" == ");
- }
- else if (op == NEQ)
- {
- printf(" != ");
- }
- else if (op == LTEQ)
- {
- printf(" <= ");
- }
- else if (op == GTEQ)
- {
- printf(" >= ");
- }
- else
- {
- printf("%d\n", op);
- }
-}
+//static void print_dest(Parser* p, SsaNode* node)
+//{
+// if (node->mode != MODE_CONTROL)
+// {
+// print_ident(p, &(node->dest));
+// printf(" = ");
+// }
+//}
+//
+//static void print_opcode(SsaNode* node)
+//{
+// int op = node->code;
+// if (op < 256)
+// {
+// printf(" %c ", op);
+// }
+// else if (op == RETURN)
+// {
+// printf(" return ");
+// }
+// else if (op == IF)
+// {
+// printf("if ");
+// }
+// else if (op == EQ)
+// {
+// printf(" == ");
+// }
+// else if (op == NEQ)
+// {
+// printf(" != ");
+// }
+// else if (op == LTEQ)
+// {
+// printf(" <= ");
+// }
+// else if (op == GTEQ)
+// {
+// printf(" >= ");
+// }
+// else
+// {
+// printf("%d\n", op);
+// }
+//}
+//
+//void print_op(Parser* p, SsaNode* expr)
+//{
+// (void)p;
+// switch(expr->code)
+// {
+// case IDENT:
+// print_ident(p, &(expr->left.var));
+// break;
+//
+// case BOOL:
+// printf("load %%Bool, %d", ssa_asbool(expr));
+// break;
+//
+// case INT:
+// printf("internal constant %%Int %lld", ssa_asint(expr));
+// break;
+//
+// case REAL:
+// printf("load %%Real, %f", ssa_asreal(expr));
+// break;
+//
+// case IF:
+// printf("if ");
+// print_ident(p, &(expr->dest));
+// printf(" then block%lu else block%lu", expr->left.block->id, expr->right.block->id);
+// break;
+//
+// case RETURN:
+// printf("return ");
+// print_ident(p, &(expr->dest));
+// break;
+//
+// default:
+// if (expr->mode == MODE_VAR)
+// {
+// print_ident(p, &(expr->left.var));
+// }
+// else if (expr->mode == MODE_UNOP)
+// {
+// print_opcode(expr);
+// print_ident(p, &(expr->left.var));
+// }
+// else if (expr->mode == MODE_BINOP)
+// {
+// print_ident(p, &(expr->left.var));
+// print_opcode(expr);
+// print_ident(p, &(expr->right.var));
+// }
+// else
+// {
+// printf("??? M:%d C:%d\n", expr->mode, expr->code);
+// }
+// break;
+// }
+//}
+//
void print_op(Parser* p, SsaNode* expr)
{
- (void)p;
- switch(expr->code)
+ switch(expr->mode)
{
- case IDENT:
- print_ident(p, &(expr->left.var));
+ case MODE_CONTROL:
+ break;
+
+ case MODE_UNOP:
+ printf(" ");
+ print_ident(p, &(expr->dest));
+ printf(" = \n");
break;
- case BOOL:
- printf("(Bool)%d", ssa_asbool(expr));
+ case MODE_BINOP:
+ printf(" ");
+ print_ident(p, &(expr->dest));
+ printf(" = \n");
break;
- case INT:
- printf("(Int)%lld", ssa_asint(expr));
+ case MODE_BINOP_LC:
+ printf(" %%");
+ print_ident(p, &(expr->dest));
+ printf(" = add ");
+ emit_type(expr->type);
+ print_ident(p, &(expr->dest));
+ printf("\n");
break;
- case REAL:
- printf("(Real)%f", ssa_asreal(expr));
+ case MODE_BINOP_RC:
+ printf(" ");
+ print_ident(p, &(expr->dest));
+ printf(" = add ");
+ emit_type(expr->type);
+ printf(" ");
+ print_ident(p, &(expr->left.var));
+ printf(", ");
+ printf("\n");
break;
- case IF:
- printf("if ");
+ case MODE_CONST:
+ printf(" ");
print_ident(p, &(expr->dest));
- printf(" then block%lu else block%lu", expr->left.block->id, expr->right.block->id);
+ printf(" = \n");
break;
- case RETURN:
- printf("return ");
+ case MODE_VAR:
+ printf(" ");
print_ident(p, &(expr->dest));
+ printf(" = \n");
break;
default:
- if (expr->mode == MODE_VAR)
- {
- print_ident(p, &(expr->left.var));
- }
- else if (expr->mode == MODE_UNOP)
- {
- print_opcode(expr);
- print_ident(p, &(expr->left.var));
- }
- else if (expr->mode == MODE_BINOP)
- {
- print_ident(p, &(expr->left.var));
- print_opcode(expr);
- print_ident(p, &(expr->right.var));
- }
- else
- {
- printf("??? M:%d C:%d\n", expr->mode, expr->code);
- }
+ printf("OP ????\n");
break;
}
}
printf("{\n");
for (SsaBlock* curr = sorted; curr; curr = curr->next)
{
- printf("L%lu:\n", curr->id);
+ if (curr->phis || curr->head)
+ {
+ printf("L%lu:\n", curr->id);
+ }
+
/* print the phis */
for (SsaPhi* phi = curr->phis; phi; phi = phi->next)
{
/* print the instructions */
for (SsaNode* node = curr->head; node; node = node->next)
{
- printf(" ");
- print_dest(p, node);
print_op(p, node);
- puts("");
}
}
printf("}\n");
}
else
{
- left = load(p, left);
- right = load(p, right);
- node = ssa_node(op, MODE_BINOP);
- node->type = left->type;
- node->left.var = left->dest;
- node->right.var = right->dest;
+ if (left->mode == MODE_CONST)
+ {
+ right = load(p, right);
+ node = ssa_node(op, MODE_BINOP_LC);
+ node->left = left->left;
+ node->right.var = right->dest;
+ }
+ else if (right->mode == MODE_CONST)
+ {
+ left = load(p, left);
+ node = ssa_node(op, MODE_BINOP_RC);
+ node->left.var = left->dest;
+ node->right = right->left;
+ }
+ else
+ {
+ left = load(p, left);
+ right = load(p, right);
+ node = ssa_node(op, MODE_BINOP);
+ node->left.var = left->dest;
+ node->right.var = right->dest;
+ }
- /* handle comparison operators */
+ /* set the result type appropriately */
+ node->type = left->type;
switch (op)
{
case EQ: