Browse Source

Optimized uasm.c:compileIf()

remotes/origin/HEAD
Seth Stubbs 5 months ago
parent
commit
1b179716f7
  1. 6
      src/main.c
  2. 15
      src/uasm.c

6
src/main.c

@ -3,9 +3,11 @@
int main() {
UASTRootNode *tree = UP_parseSource(
"int a = 2 * 4;"
"if (a == 8)\n"
"int a = 2 * 4;\n"
"if (a == 9)\n"
"prntint 0xFFFF;\n"
"else\n"
"prntint a;"
);
UA_genTal(tree, fopen("bin/out.tal", "w"));

15
src/uasm.c

@ -395,17 +395,20 @@ void compileIf(UCompState *state, UASTNode *node) {
if (!tryTypeCast(state, type, TYPE_BOOL))
cErrorNode(state, (UASTNode*)ifNode, "Cannot cast type '%s' to type '%s'", getTypeName(type), getTypeName(TYPE_BOOL));
/* write comparison jump, if the flag is not equal to true, skip the true statements */
fprintf(state->out, "#01 NEQ ;jmp%d JCN2\n", jmpID);
state->pushed -= SIZE_BOOL;
compileAST(state, ifNode->block);
if (ifNode->elseBlock) {
int tmpJmp = jmpID;
/* write comparison jump, if the flag is equal to true, skip the else statements */
fprintf(state->out, ";jmp%d JCN2\n", tmpJmp);
compileAST(state, ifNode->elseBlock);
fprintf(state->out, ";jmp%d JMP2\n", jmpID = state->jmpID++);
/* true block */
fprintf(state->out, "@jmp%d\n", tmpJmp);
compileAST(state, ifNode->elseBlock);
compileAST(state, ifNode->block);
} else {
/* write comparison jump, if the flag is not equal to true, skip the true statements */
fprintf(state->out, "#01 NEQ ;jmp%d JCN2\n", jmpID);
compileAST(state, ifNode->block);
}
fprintf(state->out, "@jmp%d\n", jmpID);

Loading…
Cancel
Save