【正文】
\ endl。 recursive_gen_code(out, root)。 if (root != ) out root : endl。 out \tinvoke ExitProcess, 0 endl。 out end root endl。 } 打印程序頭部 void tree::gen_header(ostream amp。out) { out \ endl。 out \ flat, stdcall endl。 out \toption casemap :none endl。 out endl。 out \tinclude \\masm32\\include\\ endl。 out \tinclude \\masm32\\include\\ endl。 out \tinclude \\masm32\\include\\ endl。 out \tinclude \\masm32\\include\\ endl。 out endl。 out \tincludelib \\masm32\\lib\\ endl。 out \tincludelib \\masm32\\lib\\ endl。 out \tincludelib \\masm32\\lib\\ endl。 } 變量聲明 void tree::gen_decl(ostream amp。out, Node *t) { out endl endl \ endl。 for (。 tkind == DECL_NODE。 t = tsibling) { for (Node *p = tchildren[1]。 p。 p = psibling) if (ptype == Integer) out \t\t_ (p) DWORD 0 endl。 else if (ptype == Char) out \t\t_ (p) BYTE 0 endl。 } 變量聲明(續(xù)) for (int i = 0。 i temp_var_seq。 i++) { out \t\tt i DWORD 0 endl。 } out \t\tbuffer BYTE 128 dup(0) endl。 out \t\tLF BYTE 13, 10, 0 endl。 } 遞歸代碼生成函數(shù)入口 void tree::recursive_gen_code(ostream amp。out, Node *t) { if (tkind == STMT_NODE) { stmt_gen_code(out, t)。 } else if (tkind == EXPR_NODE amp。amp。 (tkind_kind == OP_EXPR || tkind_kind == NOT_EXPR)) { expr_gen_code(out, t)。 } } 表達式代碼生成 void tree::expr_gen_code(ostream amp。out, Node *t) { Node *e1 = tchildren[0]。 Node *e2 = tchildren[1]。 switch (t) { case PLUS: out \tMOV eax, 。 if (e1kind_kind == ID_EXPR) out _ (e1)。 else if (e1kind_kind == CONST_EXPR) out e1。 else out t e1temp_var。 out endl。 out \tADD eax, 。 表達式代碼生成(續(xù)) if (e2kind_kind == ID_EXPR) out _ (e2)。 else if (e2kind_kind == CONST_EXPR) out e2。 else out t e2temp_var。 out endl。 out \tMOV t ttemp_var , eax endl。 break。 表達式代碼生成(續(xù)) case LT: out \tMOV eax, 。 if (e1kind_kind == ID_EXPR) out _ (e1)。 else if (e1kind_kind == CONST_EXPR) out e1。 else out t e1temp_var。 out endl。 out \tCMP eax, 。 if (e2kind_kind == ID_EXPR) out _ (e2)。 else if (e2kind_kind == CONST_EXPR) out e2。 else out t e2temp_var。 out endl。 out \tjl t endl。 out \tjmp t endl。 break。 語句代碼生成 void tree::stmt_gen_code(ostream amp。out, Node *t) { if (tkind_kind == COMP_STMT) { for (int i = 0。 tchildren[i]。 i++) { recursive_gen_code(out, tchildren[i])。 for (Node *p = tchildren[i]sibling。 p。 p = psibling) recursive_gen_code(out, p)。 } } 語句代碼生成(續(xù)) else if (tkind_kind == WHILE_STMT) { if (t != ) out t : endl。 recursive_gen_code(out, tchildren[0])。 recursive_gen_code(out, tchildren[1])。 out \tjmp t endl。