diff --git a/.cache/clangd/index/Ast.h.972FDEB9D69C1FA0.idx b/.cache/clangd/index/Ast.h.972FDEB9D69C1FA0.idx deleted file mode 100644 index 8b0712b42f189ba141b654c3bea0261134af3038..0000000000000000000000000000000000000000 Binary files a/.cache/clangd/index/Ast.h.972FDEB9D69C1FA0.idx and /dev/null differ diff --git a/.cache/clangd/index/c_container_auxiliary.h.74B84C4580B86FE7.idx b/.cache/clangd/index/c_container_auxiliary.h.74B84C4580B86FE7.idx deleted file mode 100644 index 903b424fd24dc34b46331a803f8399988ecd3b84..0000000000000000000000000000000000000000 Binary files a/.cache/clangd/index/c_container_auxiliary.h.74B84C4580B86FE7.idx and /dev/null differ diff --git a/.cache/clangd/index/instruction.h.A7443C88AA52BD19.idx b/.cache/clangd/index/instruction.h.A7443C88AA52BD19.idx deleted file mode 100644 index 4942c1415e7ff0ec5d780322e380a30627ff994c..0000000000000000000000000000000000000000 Binary files a/.cache/clangd/index/instruction.h.A7443C88AA52BD19.idx and /dev/null differ diff --git a/.cache/clangd/index/symbol_table.h.C7B050053B2E0C9E.idx b/.cache/clangd/index/symbol_table.h.C7B050053B2E0C9E.idx deleted file mode 100644 index f7748d8f16f7edbc69a236305d6ab48c3908521a..0000000000000000000000000000000000000000 Binary files a/.cache/clangd/index/symbol_table.h.C7B050053B2E0C9E.idx and /dev/null differ diff --git a/.cache/clangd/index/type.h.2053C30E54962E1C.idx b/.cache/clangd/index/type.h.2053C30E54962E1C.idx deleted file mode 100644 index 24d6ae62a6ed4cb15a958bf89ad6341e2aa7d24d..0000000000000000000000000000000000000000 Binary files a/.cache/clangd/index/type.h.2053C30E54962E1C.idx and /dev/null differ diff --git a/.cache/clangd/index/use.h.81C5AB5C894B73D4.idx b/.cache/clangd/index/use.h.81C5AB5C894B73D4.idx deleted file mode 100644 index 0f10fbb5973bd2fa19114e7398a400d16b5d1aff..0000000000000000000000000000000000000000 Binary files a/.cache/clangd/index/use.h.81C5AB5C894B73D4.idx and /dev/null differ diff --git a/.cache/clangd/index/user.h.98822C6CF43ECF2A.idx b/.cache/clangd/index/user.h.98822C6CF43ECF2A.idx deleted file mode 100644 index 46f62cf2699dab7746cccc8e5224977227439f5d..0000000000000000000000000000000000000000 Binary files a/.cache/clangd/index/user.h.98822C6CF43ECF2A.idx and /dev/null differ diff --git a/.cache/clangd/index/value.h.2BDF482654E2DB58.idx b/.cache/clangd/index/value.h.2BDF482654E2DB58.idx deleted file mode 100644 index 97f2e42ec22fde80341212256eb35efd12caeb35..0000000000000000000000000000000000000000 Binary files a/.cache/clangd/index/value.h.2BDF482654E2DB58.idx and /dev/null differ diff --git a/llvm_demo b/llvm_demo index 97ed1a68cc46f7ebea823ee0b1fcc60162f023a7..88f54aaa73ffe83e4349ba89bbf1f89dabdd569a 100755 Binary files a/llvm_demo and b/llvm_demo differ diff --git a/main.c b/main.c index 82f3ea994fab5306dc7d927c68174a2d898aeb7c..46c58e39d98fb267ce0e154b8da1f00d12bfc210 100644 --- a/main.c +++ b/main.c @@ -11,9 +11,6 @@ #include "cds.h" #include "symbol_table.h" -#include "print_format.h" -#include "interface_zzq.h" - extern List *ins_list; extern List *func_list; extern List *global_var_list; @@ -138,6 +135,7 @@ int main(int argc, char **argv) { AllInit(); + printf("%%begin the pass\n"); char *choose_case = NULL; if (argc == 2) { @@ -155,21 +153,20 @@ int main(int argc, char **argv) { #ifdef PARSER freopen(tty_path, "w", stdout); - // freopen("./output/out.txt", "w", stdout); + freopen("./output/out.txt", "w", stdout); print_ins_pass(ins_list); - // delete_return_deadcode_pass(ins_list); + delete_return_deadcode_pass(ins_list); ins_toBBlock_pass(ins_list); print_ins_pass(global_var_list); - TranslateInit(); //翻译全局å˜é‡è¡¨ translate_global_variable_list(global_var_list); - + ListFirst(func_list, false); void *element; while (ListNext(func_list, &element)) { diff --git a/output/out.txt b/output/out.txt index 87ee0b63301a9d2483fe62ba62ce324f7c7f0a52..15b006c5cdceb3b9588397afd922a460bcf10e31 100644 --- a/output/out.txt +++ b/output/out.txt @@ -1,43 +1,503 @@ - 0: FuncLabelOP ifElseIf null - 0: LabelOP ifElseIfentry null - 0: AllocateOP point1 null - 0: StoreOP (null) point1, 5 - 0: AllocateOP point2 null - 0: StoreOP (null) point2, 10 - 0: LoadOP temp1 point1 - 0: EqualOP temp2 temp1, 6 - 0: GotoWithConditionOP true:ifElseIflabel2 false:ifElseIflabel1 temp2 - 0: LabelOP ifElseIflabel2 null - 0: LoadOP temp3 point1 - 0: ReturnOP return temp3 - 0: GotoOP goto ifElseIflabel3 null - 0: LabelOP ifElseIflabel1 null - 0: LoadOP temp4 point2 - 0: EqualOP temp5 temp4, 10 - 0: GotoWithConditionOP true:ifElseIflabel5 false:ifElseIflabel4 temp5 - 0: LabelOP ifElseIflabel5 null - 0: StoreOP (null) point1, 25 - 0: GotoOP goto ifElseIflabel6 null - 0: LabelOP ifElseIflabel4 null - 0: LoadOP temp6 point2 - 0: EqualOP temp7 temp6, 10 - 0: GotoWithConditionOP true:ifElseIflabel8 false:ifElseIflabel7 temp7 - 0: LabelOP ifElseIflabel8 null - 0: LoadOP temp8 point1 - 0: AddOP temp9 temp8, 15 - 0: StoreOP (null) point1, temp9 - 0: GotoOP goto ifElseIflabel9 null - 0: LabelOP ifElseIflabel7 null - 0: LoadOP temp10 point1 - 0: AddOP temp11 temp10, 100 - 0: StoreOP (null) point1, temp11 - 0: GotoOP goto ifElseIflabel9 null - 96: LabelOP ifElseIflabel9 null - 0: GotoOP goto ifElseIflabel6 null - 0: LabelOP ifElseIflabel6 null - 0: GotoOP goto ifElseIflabel3 null - 0: LabelOP ifElseIflabel3 null - 0: LoadOP temp12 point1 - 0: ReturnOP return temp12 - 0: FuncEndOP func_label_end1 null -All over! + 0: FuncLabelOP ifElseIf null + 0: LabelOP ifElseIfentry null + 0: AllocateOP point1 null + 0: StoreOP (null) point1, 5 + 0: AllocateOP point2 null + 0: StoreOP (null) point2, 10 + 0: LoadOP temp1 point1 + 0: EqualOP temp2 temp1, 6 + 0: LoadOP temp3 point2 + 0: EqualOP temp4 temp3, 11 + 0: LogicOrOP temp5 temp2, temp4 + 0: GotoWithConditionOP true:ifElseIflabel2 false:ifElseIflabel1 temp5 + 0: LabelOP ifElseIflabel2 null + 0: LoadOP temp6 point1 + 0: ReturnOP return temp6 + 0: GotoOP goto ifElseIflabel3 null + 0: LabelOP ifElseIflabel1 null + 0: LoadOP temp7 point2 + 0: EqualOP temp8 temp7, 10 + 0: LoadOP temp9 point1 + 0: EqualOP temp10 temp9, 1 + 0: LogicAndOP temp11 temp8, temp10 + 0: GotoWithConditionOP true:ifElseIflabel5 false:ifElseIflabel4 temp11 + 0: LabelOP ifElseIflabel5 null + 0: StoreOP (null) point1, 25 + 0: GotoOP goto ifElseIflabel6 null + 0: LabelOP ifElseIflabel4 null + 0: LoadOP temp12 point2 + 0: EqualOP temp13 temp12, 10 + 0: LoadOP temp14 point1 + 0: EqualOP temp15 temp14, -5 + 0: LogicAndOP temp16 temp13, temp15 + 0: GotoWithConditionOP true:ifElseIflabel8 false:ifElseIflabel7 temp16 + 0: LabelOP ifElseIflabel8 null + 0: LoadOP temp17 point1 + 0: AddOP temp18 temp17, 15 + 0: StoreOP (null) point1, temp18 + 0: GotoOP goto ifElseIflabel9 null + 0: LabelOP ifElseIflabel7 null + 0: LoadOP temp19 point1 + 0: NegativeOP temp20 temp19 + 0: StoreOP (null) point1, temp20 + 0: GotoOP goto ifElseIflabel9 null + 0: LabelOP ifElseIflabel9 null + 0: GotoOP goto ifElseIflabel6 null + 0: LabelOP ifElseIflabel6 null + -134490336: GotoOP goto ifElseIflabel3 null + -134490336: LabelOP ifElseIflabel3 null + 0: LoadOP temp21 point1 + 0: ReturnOP return temp21 + -134490336: FuncEndOP func_label_end1 null + 0: FuncLabelOP main null + 0: LabelOP mainentry null + 0: ReturnOP return 0 + -134490336: FuncEndOP func_label_end2 null +ifElseIf +ifElseIfentry edge is ifElseIflabel1 ifElseIflabel2 +ifElseIflabel2 edge is ifElseIfend_label +ifElseIfend_label edge is +ifElseIflabel1 edge is ifElseIflabel4 ifElseIflabel5 +ifElseIflabel5 edge is ifElseIflabel6 +ifElseIflabel6 edge is ifElseIflabel3 +ifElseIflabel3 edge is ifElseIfend_label +ifElseIflabel4 edge is ifElseIflabel7 ifElseIflabel8 +ifElseIflabel8 edge is ifElseIflabel9 +ifElseIflabel9 edge is ifElseIflabel6 +ifElseIflabel7 edge is ifElseIflabel9 +ifElseIflabel2, +ifElseIfend_label, +ifElseIflabel1,ifElseIflabel5,ifElseIflabel6,ifElseIflabel3,ifElseIflabel4,ifElseIflabel8,ifElseIflabel9,ifElseIflabel7, +ifElseIflabel5, +ifElseIflabel6,ifElseIflabel3, +ifElseIflabel3, +ifElseIflabel4,ifElseIflabel8,ifElseIflabel9,ifElseIflabel7, +ifElseIflabel8, +ifElseIflabel9, +ifElseIflabel7, + +æ‰“å°æ¯ä¸ªèŠ‚ç‚¹çš„ç«‹å³æ”¯é…节点 +idom(ifElseIflabel2) = ifElseIfentry +idom(ifElseIfend_label) = ifElseIfentry +idom(ifElseIflabel1) = ifElseIfentry +idom(ifElseIflabel5) = ifElseIflabel1 +idom(ifElseIflabel6) = ifElseIflabel1 +idom(ifElseIflabel3) = ifElseIflabel6 +idom(ifElseIflabel4) = ifElseIflabel1 +idom(ifElseIflabel8) = ifElseIflabel4 +idom(ifElseIflabel9) = ifElseIflabel4 +idom(ifElseIflabel7) = ifElseIflabel4 + +æ‰“å°æ”¯é…æ ‘ä¸çš„层级关系 +ifElseIfentry: ifElseIflabel2,ifElseIfend_label,ifElseIflabel1, +ifElseIflabel1: ifElseIflabel5,ifElseIflabel6,ifElseIflabel4, +ifElseIflabel4: ifElseIflabel8,ifElseIflabel9,ifElseIflabel7, +ifElseIflabel7: +ifElseIflabel9: +ifElseIflabel8: +ifElseIflabel6: ifElseIflabel3, +ifElseIflabel3: +ifElseIflabel5: +ifElseIfend_label: +ifElseIflabel2: + +æ‰“å°æ¯ä¸ªåŸºæœ¬å—的支é…边界节点 +cur node ifElseIflabel2's dom frontier is +cur node ifElseIfend_label's dom frontier is +cur node ifElseIflabel1's dom frontier is +cur node ifElseIflabel5's dom frontier is +cur node ifElseIflabel6's dom frontier is +cur node ifElseIflabel3's dom frontier is +cur node ifElseIflabel4's dom frontier is +cur node ifElseIflabel8's dom frontier is +cur node ifElseIflabel9's dom frontier is +cur node ifElseIflabel7's dom frontier is + + labelID number: opcode name use + address:0x555555593f40 ifElseIfentry: + 0: LabelOP ifElseIfentry null + 0: AllocateOP point2 null + 0: AllocateOP point1 null + 0: StoreOP (null) point1, 5 + 0: StoreOP (null) point2, 10 + 0: LoadOP temp1 point1 + 0: EqualOP temp2 temp1, 6 + 0: LoadOP temp3 point2 + 0: EqualOP temp4 temp3, 11 + 0: LogicOrOP temp5 temp2, temp4 + 0: GotoWithConditionOP true:ifElseIflabel2 false:ifElseIflabel1 temp5 + + address:0x555555596970 ifElseIflabel2: + 0: LabelOP ifElseIflabel2 null + 0: LoadOP temp6 point1 + 0: ReturnOP return temp6 + + address:0x555555596cc0 ifElseIfend_label: + 0: LabelOP ifElseIfend_label null + 0: PhiFuncOp %phi_var1 size: 0 + -134490336: FuncEndOP func_label_end1 null + + address:0x555555596890 ifElseIflabel1: + 0: LabelOP ifElseIflabel1 null + 0: LoadOP temp7 point2 + 0: EqualOP temp8 temp7, 10 + 0: LoadOP temp9 point1 + 0: EqualOP temp10 temp9, 1 + 0: LogicAndOP temp11 temp8, temp10 + 0: GotoWithConditionOP true:ifElseIflabel5 false:ifElseIflabel4 temp11 + + address:0x555555597900 ifElseIflabel5: + 0: LabelOP ifElseIflabel5 null + 0: StoreOP (null) point1, 25 + 0: GotoOP goto ifElseIflabel6 null + + address:0x555555597d00 ifElseIflabel6: + 0: LabelOP ifElseIflabel6 null + 0: PhiFuncOp %phi_var2 size: 0 + -134490336: GotoOP goto ifElseIflabel3 null + + address:0x555555596bb0 ifElseIflabel3: + -134490336: LabelOP ifElseIflabel3 null + 0: LoadOP temp21 point1 + 0: ReturnOP return temp21 + + address:0x555555597820 ifElseIflabel4: + 0: LabelOP ifElseIflabel4 null + 0: LoadOP temp12 point2 + 0: EqualOP temp13 temp12, 10 + 0: LoadOP temp14 point1 + 0: EqualOP temp15 temp14, -5 + 0: LogicAndOP temp16 temp13, temp15 + 0: GotoWithConditionOP true:ifElseIflabel8 false:ifElseIflabel7 temp16 + + address:0x55555559a200 ifElseIflabel8: + 0: LabelOP ifElseIflabel8 null + 0: LoadOP temp17 point1 + 0: AddOP temp18 temp17, 15 + 0: StoreOP (null) point1, temp18 + 0: GotoOP goto ifElseIflabel9 null + + address:0x55555559a880 ifElseIflabel9: + 0: LabelOP ifElseIflabel9 null + 0: PhiFuncOp %phi_var3 size: 0 + 0: GotoOP goto ifElseIflabel6 null + + address:0x55555559a120 ifElseIflabel7: + 0: LabelOP ifElseIflabel7 null + 0: LoadOP temp19 point1 + 0: NegativeOP temp20 temp19 + 0: StoreOP (null) point1, temp20 + 0: GotoOP goto ifElseIflabel9 null + + + +begin rename pass and delete alloca,store,load instruction! +rename pass over +delete alloca,store,load instruction over + labelID number: opcode name use + address:0x555555593f40 ifElseIfentry: + 0: LabelOP ifElseIfentry null + 0: EqualOP temp2 5, 6 + 0: EqualOP temp4 10, 11 + 0: LogicOrOP temp5 temp2, temp4 + 0: GotoWithConditionOP true:ifElseIflabel2 false:ifElseIflabel1 temp5 + + address:0x555555596970 ifElseIflabel2: + 0: LabelOP ifElseIflabel2 null + 0: ReturnOP return 5 + + address:0x555555596cc0 ifElseIfend_label: + 0: LabelOP ifElseIfend_label null + 0: PhiFuncOp %phi_var1 size: 2 bblock: ifElseIflabel2 value: 5, bblock: ifElseIflabel3 value: %phi_var2, + -134490336: FuncEndOP func_label_end1 null + + address:0x555555596890 ifElseIflabel1: + 0: LabelOP ifElseIflabel1 null + 0: EqualOP temp8 10, 10 + 0: EqualOP temp10 5, 1 + 0: LogicAndOP temp11 temp8, temp10 + 0: GotoWithConditionOP true:ifElseIflabel5 false:ifElseIflabel4 temp11 + + address:0x555555597900 ifElseIflabel5: + 0: LabelOP ifElseIflabel5 null + 0: GotoOP goto ifElseIflabel6 null + + address:0x555555597d00 ifElseIflabel6: + 0: LabelOP ifElseIflabel6 null + 0: PhiFuncOp %phi_var2 size: 2 bblock: ifElseIflabel5 value: 25, bblock: ifElseIflabel9 value: %phi_var3, + -134490336: GotoOP goto ifElseIflabel3 null + + address:0x555555596bb0 ifElseIflabel3: + -134490336: LabelOP ifElseIflabel3 null + 0: ReturnOP return %phi_var2 + + address:0x555555597820 ifElseIflabel4: + 0: LabelOP ifElseIflabel4 null + 0: EqualOP temp13 10, 10 + 0: EqualOP temp15 5, -5 + 0: LogicAndOP temp16 temp13, temp15 + 0: GotoWithConditionOP true:ifElseIflabel8 false:ifElseIflabel7 temp16 + + address:0x55555559a200 ifElseIflabel8: + 0: LabelOP ifElseIflabel8 null + 0: AddOP temp18 5, 15 + 0: GotoOP goto ifElseIflabel9 null + + address:0x55555559a880 ifElseIflabel9: + 0: LabelOP ifElseIflabel9 null + 0: PhiFuncOp %phi_var3 size: 2 bblock: ifElseIflabel7 value: temp20, bblock: ifElseIflabel8 value: temp18, + 0: GotoOP goto ifElseIflabel6 null + + address:0x55555559a120 ifElseIflabel7: + 0: LabelOP ifElseIflabel7 null + 0: NegativeOP temp20 5 + 0: GotoOP goto ifElseIflabel9 null + + + + + + + labelID number: opcode name use + address:0x555555593f40 ifElseIfentry: + 0: LabelOP ifElseIfentry null + 0: EqualOP temp2 5, 6 + 0: EqualOP temp4 10, 11 + 0: LogicOrOP temp5 temp2, temp4 + 0: GotoWithConditionOP true:ifElseIflabel2 false:ifElseIflabel1 temp5 + + address:0x555555596970 ifElseIflabel2: + 0: LabelOP ifElseIflabel2 null + 0: AssignOP %phi_var1 5 + 0: ReturnOP return 5 + + address:0x555555596cc0 ifElseIfend_label: + 0: LabelOP ifElseIfend_label null + -134490336: FuncEndOP func_label_end1 null + + address:0x555555596890 ifElseIflabel1: + 0: LabelOP ifElseIflabel1 null + 0: EqualOP temp8 10, 10 + 0: EqualOP temp10 5, 1 + 0: LogicAndOP temp11 temp8, temp10 + 0: GotoWithConditionOP true:ifElseIflabel5 false:ifElseIflabel4 temp11 + + address:0x555555597900 ifElseIflabel5: + 0: LabelOP ifElseIflabel5 null + 0: AssignOP %phi_var2 25 + 0: GotoOP goto ifElseIflabel6 null + + address:0x555555597d00 ifElseIflabel6: + 0: LabelOP ifElseIflabel6 null + -134490336: GotoOP goto ifElseIflabel3 null + + address:0x555555596bb0 ifElseIflabel3: + -134490336: LabelOP ifElseIflabel3 null + 0: AssignOP %phi_var1 %phi_var2 + 0: ReturnOP return %phi_var2 + + address:0x555555597820 ifElseIflabel4: + 0: LabelOP ifElseIflabel4 null + 0: EqualOP temp13 10, 10 + 0: EqualOP temp15 5, -5 + 0: LogicAndOP temp16 temp13, temp15 + 0: GotoWithConditionOP true:ifElseIflabel8 false:ifElseIflabel7 temp16 + + address:0x55555559a200 ifElseIflabel8: + 0: LabelOP ifElseIflabel8 null + 0: AddOP temp18 5, 15 + 0: AssignOP %phi_var3 temp18 + 0: GotoOP goto ifElseIflabel9 null + + address:0x55555559a880 ifElseIflabel9: + 0: LabelOP ifElseIflabel9 null + 0: AssignOP %phi_var2 %phi_var3 + 0: GotoOP goto ifElseIflabel6 null + + address:0x55555559a120 ifElseIflabel7: + 0: LabelOP ifElseIflabel7 null + 0: NegativeOP temp20 5 + 0: AssignOP %phi_var3 temp20 + 0: GotoOP goto ifElseIflabel9 null + + + +ifElseIfentry live def add temp2 +ifElseIfentry live def add temp4 +ifElseIflabel2 live def add %phi_var1 +ifElseIflabel1 live def add temp8 +ifElseIflabel1 live def add temp10 +ifElseIflabel5 live def add %phi_var2 +ifElseIflabel3 live use add %phi_var2 +ifElseIflabel3 live def add %phi_var1 +ifElseIflabel4 live def add temp13 +ifElseIflabel4 live def add temp15 +ifElseIflabel8 live def add temp18 +ifElseIflabel8 live def add %phi_var3 +ifElseIflabel9 live use add %phi_var3 +ifElseIflabel9 live def add %phi_var2 +ifElseIflabel7 live def add temp20 +ifElseIflabel7 live def add %phi_var3 + + + + labelID number: opcode name use + address:0x555555593f40 ifElseIfentry: + 0: LabelOP ifElseIfentry null + 1: EqualOP temp2 5, 6 + 2: EqualOP temp4 10, 11 + 3: LogicOrOP temp5 temp2, temp4 + 4: GotoWithConditionOP true:ifElseIflabel2 false:ifElseIflabel1 temp5 + + address:0x555555596970 ifElseIflabel2: + 5: LabelOP ifElseIflabel2 null + 6: AssignOP %phi_var1 5 + 7: ReturnOP return 5 + + address:0x555555596cc0 ifElseIfend_label: + 8: LabelOP ifElseIfend_label null + 9: FuncEndOP func_label_end1 null + + address:0x555555596890 ifElseIflabel1: + 10: LabelOP ifElseIflabel1 null + 11: EqualOP temp8 10, 10 + 12: EqualOP temp10 5, 1 + 13: LogicAndOP temp11 temp8, temp10 + 14: GotoWithConditionOP true:ifElseIflabel5 false:ifElseIflabel4 temp11 + + address:0x555555597900 ifElseIflabel5: + 15: LabelOP ifElseIflabel5 null + 16: AssignOP %phi_var2 25 + 17: GotoOP goto ifElseIflabel6 null + + address:0x555555597d00 ifElseIflabel6: + 18: LabelOP ifElseIflabel6 null + 19: GotoOP goto ifElseIflabel3 null + + address:0x555555596bb0 ifElseIflabel3: + 20: LabelOP ifElseIflabel3 null + 21: AssignOP %phi_var1 %phi_var2 + 22: ReturnOP return %phi_var2 + + address:0x555555597820 ifElseIflabel4: + 23: LabelOP ifElseIflabel4 null + 24: EqualOP temp13 10, 10 + 25: EqualOP temp15 5, -5 + 26: LogicAndOP temp16 temp13, temp15 + 27: GotoWithConditionOP true:ifElseIflabel8 false:ifElseIflabel7 temp16 + + address:0x55555559a200 ifElseIflabel8: + 28: LabelOP ifElseIflabel8 null + 29: AddOP temp18 5, 15 + 30: AssignOP %phi_var3 temp18 + 31: GotoOP goto ifElseIflabel9 null + + address:0x55555559a880 ifElseIflabel9: + 32: LabelOP ifElseIflabel9 null + 33: AssignOP %phi_var2 %phi_var3 + 34: GotoOP goto ifElseIflabel6 null + + address:0x55555559a120 ifElseIflabel7: + 35: LabelOP ifElseIflabel7 null + 36: NegativeOP temp20 5 + 37: AssignOP %phi_var3 temp20 + 38: GotoOP goto ifElseIflabel9 null + + + + val:temp2 begin:1 end:3 + val:temp4 begin:2 end:3 + val:temp5 begin:3 end:4 + val:temp8 begin:11 end:13 + val:temp10 begin:12 end:13 + val:temp11 begin:13 end:14 + val:%phi_var2 begin:16 end:22 + val:temp13 begin:24 end:26 + val:temp15 begin:25 end:26 + val:temp16 begin:26 end:27 + val:temp18 begin:29 end:30 + val:%phi_var3 begin:30 end:33 + val:temp20 begin:36 end:37 + + var:%phi_var2 address:R1 + var:%phi_var3 address:R2 + var:temp10 address:R2 + var:temp11 address:R3 + var:temp13 address:R1 + var:temp15 address:R2 + var:temp16 address:R3 + var:temp18 address:R1 + var:temp20 address:R1 + var:temp2 address:R1 + var:temp4 address:R2 + var:temp5 address:R3 + var:temp8 address:R1 + main +mainentry edge is mainend_label +mainend_label edge is +mainend_label, + +æ‰“å°æ¯ä¸ªèŠ‚ç‚¹çš„ç«‹å³æ”¯é…节点 +idom(mainend_label) = mainentry + +æ‰“å°æ”¯é…æ ‘ä¸çš„层级关系 +mainentry: mainend_label, +mainend_label: + +æ‰“å°æ¯ä¸ªåŸºæœ¬å—的支é…边界节点 +cur node mainend_label's dom frontier is + + labelID number: opcode name use + address:0x555555598b00 mainentry: + 0: LabelOP mainentry null + 0: ReturnOP return 0 + + address:0x555555598c20 mainend_label: + -134490336: LabelOP mainend_label null + -134490336: FuncEndOP func_label_end2 null + + + +begin rename pass and delete alloca,store,load instruction! +rename pass over +delete alloca,store,load instruction over + labelID number: opcode name use + address:0x555555598b00 mainentry: + 0: LabelOP mainentry null + 0: ReturnOP return 0 + + address:0x555555598c20 mainend_label: + -134490336: LabelOP mainend_label null + -134490336: FuncEndOP func_label_end2 null + + + + + + + labelID number: opcode name use + address:0x555555598b00 mainentry: + 0: LabelOP mainentry null + 0: ReturnOP return 0 + + address:0x555555598c20 mainend_label: + -134490336: LabelOP mainend_label null + -134490336: FuncEndOP func_label_end2 null + + + + + + + labelID number: opcode name use + address:0x555555598b00 mainentry: + 0: LabelOP mainentry null + 1: ReturnOP return 0 + + address:0x555555598c20 mainend_label: + 2: LabelOP mainend_label null + 3: FuncEndOP func_label_end2 null + + + +All over! diff --git a/src/Pass.c b/src/Pass.c index 437a5cf7a808c065c46eab264bde0de796ece3c9..ce8be2fb16d5fe454578757c72cab0fe2e58d5dc 100644 --- a/src/Pass.c +++ b/src/Pass.c @@ -7,12 +7,6 @@ #include <stdio.h> #include <string.h> -#include "interface_zzq.h" -#include "memory_manager.h" -#include "variable_map.h" -#include "dependency.h" -#include "symbol_table.h" - typedef struct _dom_tree { List *child; HeadNode *bblock_node; // 分管的basicblock @@ -39,6 +33,10 @@ typedef struct _node_pair { HeadNode *value; } node_pair; +static const int REGISTER_NUM = 3; + +typedef enum _LOCATION { R1 = 1, R2, R3, MEMORY } LOCATION; + char *op_string[] = { "DefaultOP", "AddOP", "SubOP", "ModOP", "MulOP", "DivOP", "EqualOP", "NotEqualOP", @@ -53,6 +51,7 @@ char *op_string[] = { "GotoOP", "CallOP", "LabelOP", "FuncLabelOP", "FuncEndOP", "PhiFuncOp", "InitArgO"}; +static char *location_string[] = {"null", "R1", "R2", "R3", "M"}; extern List *func_list; @@ -633,7 +632,7 @@ void rename_pass_help_new(HashMap *rename_var_stack_hashmap, // éåŽ†é‚»æŽ¥è¾¹é›†åˆ ä¿®æ”¹phi函数ä¸çš„傿•° node_pair *neighbor_bblock = NULL; - HashMapNext(cur_bblock->bblock_node->edge_list); + HashMapFirst(cur_bblock->bblock_node->edge_list); while ((neighbor_bblock = (node_pair *)HashMapNext( cur_bblock->bblock_node->edge_list)) != NULL) { void *neighbor_bblock_ins = NULL; @@ -868,10 +867,10 @@ void insert_copies_help(HashMap *insert_copies_stack_hashmap, HashMap *num_of_var_def, dom_tree *cur_bblock) { // Pass One:Initialize the data structures HashSet *copy_set = NULL; - hashset_init(&(copy_set)); + hashset_init(©_set); HashSet *worklist = NULL; - hashset_init(&(worklist)); + hashset_init(&worklist); // 伪代ç ä¸çš„map HashMap *var_replace_hashmap = NULL; @@ -885,7 +884,7 @@ void insert_copies_help(HashMap *insert_copies_stack_hashmap, // For all successors s of block node_pair *neighbor_bblock = NULL; - HashMapNext(cur_bblock->bblock_node->edge_list); + HashMapFirst(cur_bblock->bblock_node->edge_list); while ((neighbor_bblock = (node_pair *)HashMapNext( cur_bblock->bblock_node->edge_list)) != NULL) { void *neighbor_bblock_ins = NULL; @@ -1627,28 +1626,30 @@ void delete_return_deadcode_pass(List *self) { while (i != ListSize(self)) { ListGetAt(self, i, &element); switch (((Instruction *)element)->opcode) { - case GotoOP: - HashSetAdd(reach_label, - ((Value *)element)->pdata->no_condition_goto.goto_location); - i++; - while (ListGetAt(self, i, &element) && - (((Instruction *)element)->opcode == GotoOP || - ((Instruction *)element)->opcode == GotoWithConditionOP)) { - ListRemove(self, i); - } - break; - case GotoWithConditionOP: - HashSetAdd(reach_label, - ((Value *)element)->pdata->condition_goto.true_goto_location); - HashSetAdd(reach_label, - ((Value *)element)->pdata->condition_goto.false_goto_location); - i++; - while (ListGetAt(self, i, &element) && - (((Instruction *)element)->opcode == GotoOP || - ((Instruction *)element)->opcode == GotoWithConditionOP)) { - ListRemove(self, i); - } - break; + // case GotoOP: + // HashSetAdd(reach_label, + // ((Value *)element)->pdata->no_condition_goto.goto_location); + // i++; + // while (ListGetAt(self, i, &element) && + // (((Instruction *)element)->opcode == GotoOP || + // ((Instruction *)element)->opcode == GotoWithConditionOP)) { + // ListRemove(self, i); + // } + // break; + // case GotoWithConditionOP: + // HashSetAdd(reach_label, + // ((Value + // *)element)->pdata->condition_goto.true_goto_location); + // HashSetAdd(reach_label, + // ((Value + // *)element)->pdata->condition_goto.false_goto_location); + // i++; + // while (ListGetAt(self, i, &element) && + // (((Instruction *)element)->opcode == GotoOP || + // ((Instruction *)element)->opcode == GotoWithConditionOP)) { + // ListRemove(self, i); + // } + // break; case ReturnOP: i++; while (ListGetAt(self, i, &element) && @@ -1657,17 +1658,17 @@ void delete_return_deadcode_pass(List *self) { ListRemove(self, i); } break; - case LabelOP: - while (!HashSetFind(reach_label, (Value *)element) && - strcmp(((Value *)element)->name, "entry")) { - ListRemove(self, i); - while (ListGetAt(self, i, &element) && - (((Instruction *)element)->opcode != LabelOP)) { - ListRemove(self, i); - } - } - i++; - break; + // case LabelOP: + // while (!HashSetFind(reach_label, (Value *)element) && + // strcmp(((Value *)element)->name, "entry")) { + // ListRemove(self, i); + // while (ListGetAt(self, i, &element) && + // (((Instruction *)element)->opcode != LabelOP)) { + // ListRemove(self, i); + // } + // } + // i++; + // break; default: i++; break; @@ -1798,128 +1799,54 @@ void line_scan_register_allocation(ALGraph *self_cfg, Function *self_func, printf("\n"); } -char *location_string[] = {"null", "R1", "R2", "R3","R4","R5","R6","R7", "R8","M"}; - -int REGISTER_NUM = 8; - void register_replace(ALGraph *self_cfg, Function *self_func, HashMap *var_location) { - Pair *ptr_pair; - //å˜é‡å˜å‚¨ä½ç½®æ˜ 射表 - //| a | b | c | - //| M | R0 | ALLOCATE_R1 | - - //é历å˜é‡çš„寄å˜å™¨åˆ†é…ç»“æžœå¹¶æ‰“å° HashMapFirst(var_location); while ((ptr_pair = HashMapNext(var_location)) != NULL) { printf("\tvar:%s\taddress:%s\n ", (char *)ptr_pair->key, location_string[*((LOCATION *)ptr_pair->value)]); } - Label(self_func->label->name); - - //第一次functioné历,é历所有的å˜é‡è®¡ç®—æ ˆå¸§å¤§å°å¹¶å°†å˜é‡å…¨éƒ¨æ·»åŠ åˆ°å˜é‡ä¿¡æ¯è¡¨ - //é历æ¯ä¸€ä¸ªblockçš„list - size_t totalLocalVariableSize = 0; - HashMap* VariableInfoMap = NULL; - - //翻译å‰åˆå§‹åŒ– - //2023-5-3 åˆå§‹åŒ–å‰ç§»åˆ°è¿™ä¸ªä½ç½®ï¼Œå› 为分é…å†…å˜æ—¶æœ‰å¯èƒ½éœ€è¦ä¸ºæ•°ç»„é¦–åœ°å€æä¾›å˜æ”¾çš„寄å˜å™¨ - InitBeforeFunction(); - - - //è®¡ç®—æ ˆå¸§å¤§å° - for (int i = 0; i < self_cfg->node_num; i++) { - int iter_num = 0; - ListFirst((self_cfg->node_set)[i]->bblock_head->inst_list,false); - totalLocalVariableSize += traverse_list_and_count_total_size_of_var((self_cfg->node_set)[i]->bblock_head->inst_list,0); - } - //2023-5-22 这决定了局部å˜é‡åŒºé—´çš„累计å移值 - currentPF.fp_offset -= totalLocalVariableSize; - - //åˆå§‹åŒ–å‡½æ•°æ ˆå¸§ - new_stack_frame_init(totalLocalVariableSize); - //设置当å‰å‡½æ•°æ ˆå¸§ - set_stack_frame_status(0,totalLocalVariableSize/4); - - //å˜é‡ä¿¡æ¯è¡¨è½¬æ¢ - for (int i = 0; i < self_cfg->node_num; i++) { - int iter_num = 0; - ListFirst((self_cfg->node_set)[i]->bblock_head->inst_list,false); - traverse_list_and_allocate_for_variable((self_cfg->node_set)[i]->bblock_head->inst_list,var_location,&VariableInfoMap); - } - - //统计当å‰å‡½æ•°ä½¿ç”¨çš„æ‰€æœ‰R4-R12的通用寄å˜å™¨ - //剿 å·²ç»å®Œæˆå¯„å˜å™¨åˆ†é… - size_t used_reg_size = 0; - count_register_change_from_R42R12(VariableInfoMap,currentPF.used_reg,&used_reg_size); - //ä¿å˜çŽ°åœº - bash_push_pop_instruction_list("PUSH",currentPF.used_reg); - - - Instruction* element = NULL; - //第二次functioné历,为æ¯ä¸€å¥Instruction安æ’一个map - for (int i = 0; i < self_cfg->node_num; i++) { - int iter_num = 0; - ListFirst((self_cfg->node_set)[i]->bblock_head->inst_list,false); - while(ListNext((self_cfg->node_set)[i]->bblock_head->inst_list,&element)) - ins_deepSet_varMap(element,VariableInfoMap); - } - - //为数组分é…空间并装载到基å€å˜å‚¨ä½ç½® - //剿 所有的指令都分é…了å˜é‡ä¿¡æ¯è¡¨ - for (int i = 0; i < self_cfg->node_num; i++) { - int iter_num = 0; - ListFirst((self_cfg->node_set)[i]->bblock_head->inst_list,false); - traverse_and_load_arrayBase_to_recorded_place((self_cfg->node_set)[i]->bblock_head->inst_list); - } - - //2023-5-22 è¿™å†³å®šäº†çŽ°åœºä¿æŠ¤åŒºåŸŸFPçš„å移值 - currentPF.fp_offset -= used_reg_size; - - //å¾—çŸ¥å‚æ•°ä¸ªæ•° - //åœ¨å‚æ•°ä¸ªæ•°å°äºŽ4的情况下,å¯ä»¥æš‚æ—¶ä¸äºˆè€ƒè™‘ - - //执行期间使指针å˜åŠ¨ç”Ÿæ•ˆ - update_sp_value(); - //使当å‰R7与SPä¿æŒä¸€è‡´ - general_data_processing_instructions(MOV,fp,nullop,sp,NONESUFFIX,false); - - //ä¼ é€’å‚æ•° - move_parameter_to_recorded_place(VariableInfoMap); - - //将形å¼å‚数装载到对应的寄å˜å™¨/内å˜åˆ†é…ä½ç½® - - // VarInfo testVarInfo; - // VarInfo* testVarInfoPtr; - // HashMapPut(VariableInfoMap,"name",&testVarInfo); - // testVarInfoPtr = HashMapGet(VariableInfoMap,"name"); - - //由于å˜é‡çš„寄å˜å™¨å’Œå†…å˜åœ°å€éƒ½æ˜¯é™æ€çš„,为å˜é‡å®šä¹‰å…¨å±€å›ºå®šçš„寄å˜å™¨å’Œå†…å˜ä½ç½® - //å°†zzqæä¾›çš„å˜é‡å®šä½è¡¨è½¬æ¢ä¸ºå˜é‡ä¿¡æ¯è¡¨ - //interface_cvt_zzq_register_allocate_map_to_variable_info_map(var_location,VariableInfoMap); - - - //打å°å˜é‡ä¿¡æ¯è¡¨ // for (int i = 0; i < self_cfg->node_num; i++) { - // print_list_info_map((self_cfg->node_set)[i]->bblock_head->inst_list,0,true); + // int iter_num = 0; + // while (iter_num < + // ListSize((self_cfg->node_set)[i]->bblock_head->inst_list)) { + // Instruction *element = NULL; + // ListGetAt((self_cfg->node_set)[i]->bblock_head->inst_list, iter_num, + // &element); + // if (element->opcode < 19) { + // for (int j = 0; j < ((User *)element)->num_oprands; j++) { + // Value *cur_handle = user_get_operand_use((User *)element, j)->Val; + // // R replace name + + // // M store load + // } + + // if (((Instruction *)element)->opcode < RETURN_USED) { + // if (HashMapGet(var_location, ((Value *)element)->name)) { + // LOCATION cur_var_location = *( + // (LOCATION *)HashMapGet(var_location, ((Value + // *)element)->name)); + // if (cur_var_location == MEMORY) { + // // 将当å‰è¯å¥æ”¹æˆstoreè¯å¥ + // } else { + // char *temp_str = strdup(location_string[*((LOCATION + // *)HashMapGet( + // var_location, ((Value *)element)->name))]); + // free(((Value *)element)->name); + // ((Value *)element)->name = temp_str; + // } + // } else { + // ListRemove((self_cfg->node_set)[i]->bblock_head->inst_list, + // iter_num); + // continue; + // } + // } + // } + // iter_num++; + // } // } - - - //第三次functioné历,翻译æ¯ä¸€ä¸ªlist - for (int i = 0; i < self_cfg->node_num; i++) { - ListFirst((self_cfg->node_set)[i]->bblock_head->inst_list,false); - traverse_list_and_translate_all_instruction((self_cfg->node_set)[i]->bblock_head->inst_list,0); - } - - //æ¢å¤å½“å‰å‡½æ•°æ ˆå¸§ - reset_stack_frame_status(); - //æ¢å¤çŽ°åœº - bash_push_pop_instruction_list("POP",currentPF.used_reg); - //退出函数 - bash_push_pop_instruction("POP",&fp,&pc,END); - } void bblock_to_dom_graph_pass(Function *self) { @@ -2021,5 +1948,3 @@ void bblock_to_dom_graph_pass(Function *self) { register_replace(graph_for_dom_tree, self, var_location); } - - diff --git a/src/c_container_auxiliary.c b/src/c_container_auxiliary.c index cd5537fba003eecf6725c7aa31caa778c571d9f4..b819aa2f6e31f733b34fb5876015f4253bc299f1 100644 --- a/src/c_container_auxiliary.c +++ b/src/c_container_auxiliary.c @@ -1,164 +1,199 @@ -#include "c_container_auxiliary.h" - -Stack* stack_ast_pre = NULL; - -Stack* stack_symbol_table = NULL; - -Stack* stack_else_label = NULL; - -Stack* stack_then_label = NULL; - -// whileæ¡ä»¶ä¸ºfalse时所è¦è·³è½¬çš„labelæ ˆ -Stack* stack_while_then_label = NULL; - -// while循环头(æ¡ä»¶åˆ¤æ–) -Stack* stack_while_head_label = NULL; - -Stack* stack_param = NULL; - -// 哈希表的åˆå§‹åŒ– 用于查找调用的函数å和对应的函数label -HashMap* func_hashMap = NULL; - -List* ins_list = NULL; - -List* func_list = NULL; - -List* global_var_list = NULL; - -// 用于生æˆcfg图通过labelçš„åå—æŸ¥è¯¢å¯¹åº”çš„bblock -HashMap* bblock_hashmap = NULL; - -HashSet* bblock_pass_hashset = NULL; - -// 用于block的链状结构转æ¢ä¸ºCFG图结构 -// dfs判æ–一个节点是å¦ç»è¿‡å¦‚æžœç»è¿‡è¿˜éœ€è¦å–出节点建立边 -// (char *name,HeadNode *node) -HashMap* bblock_to_dom_graph_hashmap = NULL; - -// å‡ºæ ˆ/é“¾è¡¨åˆ é™¤ ä¼´éšè¿è¡Œçš„函数 我真他妈 心æ€å´©äº† å¦è‰ºä¸ç²¾ -void CleanObject(void* element) {} - -unsigned HashKey(void* key) { return HashDjb2((char*)key); } - -// 直接使用地å€è¿›è¡Œhash计算 -unsigned HashKeyAddress(void* key) { return HashDjb2(key); } - -// 设置一个hash_mapçš„æ¯”è¾ƒæ–¹å¼ ç¤ºä¾‹ä¸ºé€šè¿‡å—ç¬¦ä¸²æŽ’åº -// 将键类型强转ä½å—ç¬¦ä¸²ç„¶åŽæ¥æ¯”较 -int CompareKey(void* lhs, void* rhs) { - // printf("compare %s and %s\n", (char*)lhs, (char*)lhs); - return strcmp((char*)lhs, (char*)lhs); -} - -// å› ä¸ºæ•°æ®ç»“构心æ€å†ä¸€æ¬¡å´©æŽ‰ -int CompareKeyAddress(void* lhs, void* rhs) { - // ä¿è¯å†…容相åŒçš„åŒæ—¶åœ°å€ç›¸åŒ 内容相åŒçš„先决æ¡ä»¶ - return lhs != rhs; -} - -void CleanHashMapKey(void* key) { free(key); } - -void CleanHashMapKeyNotFree(void* key) {} - -void CleanHashSetKey(void* key) {} - -void CleanValue(void* value) {} - -void CleanIntValue(void* value) { free((int*)value); } - -void hashset_init(HashSet** self) { - *self = HashSetInit(); - HashSetSetHash(*self, HashKeyAddress); - HashSetSetCompare(*self, CompareKeyAddress); - HashSetSetCleanKey(*self, CleanHashSetKey); -} - -void hashset_init_string(HashSet** self) { - *self = HashSetInit(); - HashSetSetHash(*self, HashKey); - HashSetSetCompare(*self, CompareKey); - HashSetSetCleanKey(*self, CleanHashMapKey); -} - -void hashmap_init(HashMap** self) { - *self = HashMapInit(); - HashMapSetHash(*self, HashKey); - HashMapSetCompare(*self, CompareKey); - HashMapSetCleanKey(*self, CleanHashMapKey); - HashMapSetCleanValue(*self, CleanValue); -} - -void hashmap_init_address(HashMap** self) { - *self = HashMapInit(); - HashMapSetHash(*self, HashKeyAddress); - HashMapSetCompare(*self, CompareKeyAddress); - HashMapSetCleanKey(*self, CleanHashSetKey); - HashMapSetCleanValue(*self, CleanValue); -} - -void list_copy(List* dest, List* src) { - void* element; - ListFirst(src, false); - while (ListNext(src, &element) != NULL) { - ListPushBack(dest, element); - } -} - -void AllInit() { - // åˆå§‹åŒ–指令链表 - ins_list = ListInit(); - ListSetClean(ins_list, CleanObject); - - // åˆå§‹åŒ–全局å˜é‡é“¾æ¡ - global_var_list = ListInit(); - ListSetClean(global_var_list, CleanObject); - - // åˆå§‹åŒ–函数链表 - func_list = ListInit(); - ListSetClean(func_list, CleanObject); - - // åˆå§‹åŒ–å‰ç½®astèŠ‚ç‚¹æ ˆ - stack_ast_pre = StackInit(); - StackSetClean(stack_ast_pre, CleanObject); - - // åˆå§‹åŒ–符å·è¡¨æ ˆ - stack_symbol_table = StackInit(); - StackSetClean(stack_symbol_table, CleanObject); - - // åˆå§‹åŒ–elseçš„gotoæ ˆ - stack_else_label = StackInit(); - StackSetClean(stack_else_label, CleanObject); - - // whileæ¡ä»¶ä¸ºfalse时所è¦è·³è½¬çš„labelæ ˆ - stack_then_label = StackInit(); - StackSetClean(stack_then_label, CleanObject); - - // while循环头(æ¡ä»¶åˆ¤æ–) - stack_while_head_label = StackInit(); - StackSetClean(stack_while_head_label, CleanObject); - - // åˆå§‹åŒ–thençš„gotoæ ˆ - stack_while_then_label = StackInit(); - StackSetClean(stack_while_then_label, CleanObject); - - stack_param = StackInit(); - StackSetClean(stack_param, CleanObject); - - func_hashMap = HashMapInit(); - // 哈希表的åˆå§‹åŒ– 用于查找调用的函数å和对应的函数label - HashMapSetHash(func_hashMap, HashKey); - HashMapSetCompare(func_hashMap, CompareKey); - HashMapSetCleanKey(func_hashMap, CleanHashMapKey); - HashMapSetCleanValue(func_hashMap, CleanValue); - - hashmap_init(&bblock_hashmap); - - bblock_to_dom_graph_hashmap = HashMapInit(); - // 用于é历生æˆcfg图结构时候查找bblock - HashMapSetHash(bblock_to_dom_graph_hashmap, HashKey); - HashMapSetCompare(bblock_to_dom_graph_hashmap, CompareKey); - HashMapSetCleanKey(bblock_to_dom_graph_hashmap, CleanHashMapKey); - HashMapSetCleanValue(bblock_to_dom_graph_hashmap, CleanValue); - - hashset_init(&bblock_pass_hashset); -} +#include "c_container_auxiliary.h" +#include "instruction.h" + +Stack *stack_ast_pre = NULL; + +Stack *stack_symbol_table = NULL; + +Stack *stack_else_label = NULL; + +Stack *stack_then_label = NULL; + +// whileæ¡ä»¶ä¸ºfalse时所è¦è·³è½¬çš„labelæ ˆ +Stack *stack_while_then_label = NULL; + +// while循环头(æ¡ä»¶åˆ¤æ–) +Stack *stack_while_head_label = NULL; + +Stack *stack_param = NULL; + +// 哈希表的åˆå§‹åŒ– 用于查找调用的函数å和对应的函数label +HashMap *func_hashMap = NULL; + +List *ins_list = NULL; + +List *func_list = NULL; + +List *global_var_list = NULL; + +// 用于生æˆcfg图通过labelçš„åå—æŸ¥è¯¢å¯¹åº”çš„bblock +HashMap *bblock_hashmap = NULL; + +HashSet *bblock_pass_hashset = NULL; + +// 用于block的链状结构转æ¢ä¸ºCFG图结构 +// dfs判æ–一个节点是å¦ç»è¿‡å¦‚æžœç»è¿‡è¿˜éœ€è¦å–出节点建立边 +// (char *name,HeadNode *node) +HashMap *bblock_to_dom_graph_hashmap = NULL; + +// å‡ºæ ˆ/é“¾è¡¨åˆ é™¤ ä¼´éšè¿è¡Œçš„函数 我真他妈 心æ€å´©äº† å¦è‰ºä¸ç²¾ +void CleanObject(void *element) {} + +unsigned HashKey(void *key) { return HashDjb2((char *)key); } + +// 直接使用地å€è¿›è¡Œhash计算 +unsigned HashKeyAddress(void *key) { return HashDjb2(key); } + +// 设置一个hash_mapçš„æ¯”è¾ƒæ–¹å¼ ç¤ºä¾‹ä¸ºé€šè¿‡å—ç¬¦ä¸²æŽ’åº +// 将键类型强转ä½å—ç¬¦ä¸²ç„¶åŽæ¥æ¯”较 +int CompareKey(void *lhs, void *rhs) { + // printf("compare %s and %s\n", (char*)lhs, (char*)lhs); + return strcmp((char *)lhs, (char *)lhs); +} + +// å› ä¸ºæ•°æ®ç»“构心æ€å†ä¸€æ¬¡å´©æŽ‰ +int CompareKeyAddress(void *lhs, void *rhs) { + // ä¿è¯å†…容相åŒçš„åŒæ—¶åœ°å€ç›¸åŒ 内容相åŒçš„先决æ¡ä»¶ + return lhs != rhs; +} + +void CleanHashMapKey(void *key) { free(key); } + +void CleanHashMapKeyNotFree(void *key) {} + +void CleanHashSetKey(void *key) {} + +void CleanValue(void *value) {} + +void CleanIntValue(void *value) { free((int *)value); } + +void hashset_init(HashSet **self) { + *self = HashSetInit(); + HashSetSetHash(*self, HashKeyAddress); + HashSetSetCompare(*self, CompareKeyAddress); + HashSetSetCleanKey(*self, CleanHashSetKey); +} + +void hashset_init_string(HashSet **self) { + *self = HashSetInit(); + HashSetSetHash(*self, HashKey); + HashSetSetCompare(*self, CompareKey); + HashSetSetCleanKey(*self, CleanHashMapKey); +} + +void hashmap_init(HashMap **self) { + *self = HashMapInit(); + HashMapSetHash(*self, HashKey); + HashMapSetCompare(*self, CompareKey); + HashMapSetCleanKey(*self, CleanHashMapKey); + HashMapSetCleanValue(*self, CleanValue); +} + +void hashmap_init_address(HashMap **self) { + *self = HashMapInit(); + HashMapSetHash(*self, HashKeyAddress); + HashMapSetCompare(*self, CompareKeyAddress); + HashMapSetCleanKey(*self, CleanHashSetKey); + HashMapSetCleanValue(*self, CleanValue); +} + +void list_copy(List *dest, List *src) { + void *element; + ListFirst(src, false); + while (ListNext(src, &element)) { + ListPushBack(dest, element); + } +} + +typedef struct sys_fun_info { + char *func_name; + TypeID return_type; + int param_num; +} sys_func_info; + +void system_func_init() { + sys_func_info sysy[20] = {{"getint", IntegerTyID, 0}, + {"getch", IntegerTyID, 0}, + {"getarray", IntegerTyID, 1}, + {"getfloat", FloatTyID, 0}, + {"getfarray", IntegerTyID, 1}, + {"putint", VoidTyID, 1}, + {"putch", VoidTyID, 1}, + {"putarray", VoidTyID, 2}, + {"putfloat", VoidTyID, 1}, + {"putfarray", VoidTyID, 2}, + {NULL, VoidTyID, 0}}; + for (int i = 0;; i++) { + if (sysy[i].func_name == NULL) + return; + Value *func_label_ins = (Value *)ins_new_no_operator_v2(FuncLabelOP); + // æ·»åŠ å˜é‡çš„åå— + func_label_ins->name = strdup(sysy[i].func_name); + func_label_ins->VTy->TID = FuncLabelTyID; + func_label_ins->pdata->symtab_func_pdata.return_type = sysy[i].return_type; + func_label_ins->pdata->symtab_func_pdata.param_num = sysy[i].param_num; + + // 将函数的<name,label>æ’入函数表 + HashMapPut(func_hashMap, strdup(sysy[i].func_name), func_label_ins); + } +} + +void AllInit() { + // åˆå§‹åŒ–指令链表 + ins_list = ListInit(); + ListSetClean(ins_list, CleanObject); + + // åˆå§‹åŒ–全局å˜é‡é“¾æ¡ + global_var_list = ListInit(); + ListSetClean(global_var_list, CleanObject); + + // åˆå§‹åŒ–函数链表 + func_list = ListInit(); + ListSetClean(func_list, CleanObject); + + // åˆå§‹åŒ–å‰ç½®astèŠ‚ç‚¹æ ˆ + stack_ast_pre = StackInit(); + StackSetClean(stack_ast_pre, CleanObject); + + // åˆå§‹åŒ–符å·è¡¨æ ˆ + stack_symbol_table = StackInit(); + StackSetClean(stack_symbol_table, CleanObject); + + // åˆå§‹åŒ–elseçš„gotoæ ˆ + stack_else_label = StackInit(); + StackSetClean(stack_else_label, CleanObject); + + // whileæ¡ä»¶ä¸ºfalse时所è¦è·³è½¬çš„labelæ ˆ + stack_then_label = StackInit(); + StackSetClean(stack_then_label, CleanObject); + + // while循环头(æ¡ä»¶åˆ¤æ–) + stack_while_head_label = StackInit(); + StackSetClean(stack_while_head_label, CleanObject); + + // åˆå§‹åŒ–thençš„gotoæ ˆ + stack_while_then_label = StackInit(); + StackSetClean(stack_while_then_label, CleanObject); + + stack_param = StackInit(); + StackSetClean(stack_param, CleanObject); + + func_hashMap = HashMapInit(); + // 哈希表的åˆå§‹åŒ– 用于查找调用的函数å和对应的函数label + HashMapSetHash(func_hashMap, HashKey); + HashMapSetCompare(func_hashMap, CompareKey); + HashMapSetCleanKey(func_hashMap, CleanHashMapKey); + HashMapSetCleanValue(func_hashMap, CleanValue); + system_func_init(); + + hashmap_init(&bblock_hashmap); + + bblock_to_dom_graph_hashmap = HashMapInit(); + // 用于é历生æˆcfg图结构时候查找bblock + HashMapSetHash(bblock_to_dom_graph_hashmap, HashKey); + HashMapSetCompare(bblock_to_dom_graph_hashmap, CompareKey); + HashMapSetCleanKey(bblock_to_dom_graph_hashmap, CleanHashMapKey); + HashMapSetCleanValue(bblock_to_dom_graph_hashmap, CleanValue); + + hashset_init(&bblock_pass_hashset); +}