backend

Unnamed repository; edit this file 'description' to name the repository.
git clone https://git.deepztream.com/backend
Log | Files | Refs | Submodules

test.c (4425B)


      1 #include <stdbool.h>
      2 
      3 #define DLIB_IMPLEMENTATION
      4 #include "dlib.h"
      5 
      6 #include "backend.h"
      7 
      8 #include "phi.c"
      9 
     10 #include "ir_backend.c"
     11 #include "x86_64-codegen.c"
     12 
     13 int
     14 main(int argc, char **argv)
     15 {
     16   (void) argc;
     17   (void) argv;
     18   size_t i;
     19   Sb sb = {0};
     20   Ir_block_list func_body = {0};
     21   Ir_block ir_block = {0};
     22   /* da_append(&ir_block, ((Ir) { .kind = IR_LABEL, .label_id = 0})); */
     23   da_append(&ir_block, ((Ir) { .kind = IR_ASSIGN, .result = { .kind = ARG_TEMP_STORAGE, .type = TYPE_I32, .temp_id = 0}, .assign = {.kind = ARG_INT_LITERAL, .i64 = 0}}));
     24   da_append(&ir_block, ((Ir) { .kind = IR_JUMP, .jump = {.label_id = 1}}));
     25 
     26   da_append(&func_body, ir_block);
     27   ir_block = (Ir_block) {0};
     28 
     29   /* da_append(&ir_block, ((Ir) { .kind = IR_LABEL, .label_id = 1})); */
     30   da_append(&ir_block, ((Ir) { .kind = IR_PHI, .result = { .kind = ARG_TEMP_STORAGE, .type = TYPE_I32, .temp_id = 3 }, .phi_list = {0}}));
     31   da_append(&ir_block.items[ir_block.count - 1].phi_list, ((Phi_entry) { .label_id = 0, .temp_id = 0 }));
     32   da_append(&ir_block.items[ir_block.count - 1].phi_list, ((Phi_entry) { .label_id = 2, .temp_id = 4 }));
     33   da_append(&ir_block, ((Ir) { .kind = IR_PHI, .result = { .kind = ARG_TEMP_STORAGE, .type = TYPE_I32, .temp_id = 5 }, .phi_list = {0}}));
     34   da_append(&ir_block.items[ir_block.count - 1].phi_list, ((Phi_entry) { .label_id = 0, .temp_id = 0 }));
     35   da_append(&ir_block.items[ir_block.count - 1].phi_list, ((Phi_entry) { .label_id = 2, .temp_id = 2 }));
     36   da_append(&ir_block, ((Ir) { .kind = IR_BINOP, .result = { .kind = ARG_TEMP_STORAGE, .type = TYPE_I8, .temp_id = 1}, .binop = {.op = OP_LT, .op_type = TYPE_I32, .left = {.kind = ARG_TEMP_STORAGE, .temp_id = 3}, .right = { .kind = ARG_INT_LITERAL, .i64 = 11}}}));
     37   da_append(&ir_block, ((Ir) { .kind = IR_BRANCH, .branch = {.cond = {.kind = ARG_TEMP_STORAGE, .temp_id = 1}, .true_label_id = 2, .false_label_id = 3}}));
     38 
     39   da_append(&func_body, ir_block);
     40   ir_block = (Ir_block) {0};
     41 
     42   /* da_append(&ir_block, ((Ir) { .kind = IR_LABEL, .label_id = 2})); */
     43   da_append(&ir_block, ((Ir) { .kind = IR_CALL, .result = {.kind = ARG_TEMP_STORAGE, .type = TYPE_I32, .temp_id = 2}, .call = { .name = {.data = "printf", .count = 6}, .args = {0}}}));
     44   da_append(&ir_block.items[ir_block.count - 1].call.args, ((Ir_arg) {.kind = ARG_CSTRING_LITERAL, .type = TYPE_PTR, .sv = sv_literal("%i\n")}));
     45   da_append(&ir_block.items[ir_block.count - 1].call.args, ((Ir_arg) {.kind = ARG_TEMP_STORAGE, .type = TYPE_I32, .temp_id = 3}));
     46   da_append(&ir_block, ((Ir) { .kind = IR_BINOP, .result = { .kind = ARG_TEMP_STORAGE, .type = TYPE_I32, .temp_id = 4}, .binop = {.op = OP_ADD, .op_type = TYPE_I32, .left = {.kind = ARG_TEMP_STORAGE, .temp_id = 3}, .right = {.kind = ARG_INT_LITERAL, .i64 = 1}}}));
     47   da_append(&ir_block, ((Ir) { .kind = IR_JUMP, .jump = {.label_id = 1}}));
     48 
     49   da_append(&func_body, ir_block);
     50   ir_block = (Ir_block) {0};
     51 
     52   /* da_append(&ir_block, ((Ir) { .kind = IR_LABEL, .label_id = 3})); */
     53   da_append(&ir_block, ((Ir) { .kind = IR_RETURN, .ret = {.kind = ARG_TEMP_STORAGE, .type = TYPE_I32, .temp_id = 5}}));
     54 
     55   da_append(&func_body, ir_block);
     56   ir_block = (Ir_block) {0};
     57 
     58   Ir_program prog = {0};
     59   da_append(&prog.functions, ((Ir_function) {.name = {.data = "main", .count = 4}, .return_type = TYPE_I32, .body = func_body}));
     60   da_append(&prog.functions.items[0].argument_types, TYPE_I32);
     61   da_append(&prog.functions.items[0].argument_types, TYPE_PTR);
     62 
     63   da_append(&prog.externs, ((Ir_extern) { .symbol = { .data = "printf", .count = 6 }}));
     64 
     65   if (!ir_backend.compile(prog, &sb)) {
     66     return 1;
     67   }
     68 
     69   fprintf(stdout, "[[[ IR Representation ]]]\n");
     70   sb_write(1, sb);
     71   sb.count = 0;
     72 
     73   for (i = 0; i < prog.functions.count; i++) {
     74     remove_phi_nodes(prog.functions.items[i]);
     75   }
     76 
     77   if (!x86_64_backend.compile(prog, &sb)) {
     78     fprintf(stdout, "\n[[[ x86_64-nasm Representation ]]]\n");
     79     sb_write(1, sb);
     80     return 1;
     81   }
     82 
     83   fprintf(stdout, "\n[[[ x86_64-nasm Representation ]]]\n");
     84   sb_write(1, sb);
     85 
     86   int out_fd = open("output.nasm", O_WRONLY | O_CREAT | O_TRUNC, 0644);
     87   if (out_fd < 0) {
     88     return 1;
     89   }
     90   sb_write(out_fd, sb);
     91   sb.count = 0;
     92 
     93   /* print_temp_count(prog); */
     94 
     95   /* remove_phi_nodes(prog.functions.items[0]); */
     96   /* fprintf(stderr, "--------Removed Phi nodes---------\n"); */
     97   /* if (!ir_backend.compile(prog, &sb)) { */
     98   /*   return 1; */
     99   /* } */
    100   /* sb_write(1, sb); */
    101 
    102   return 0;
    103 }
    104