Used Variables instead of Options, in SConstruct
[mcc.git] / icode.c
blob72c97127e5f014af458a6b1ae71c6a6585f3bba9
1 #include "icode.h"
2 #include "errors.h"
3 #include "stdlib.h"
4 #include "stdio.h"
5 #include "string.h"
7 static int block_id = 0;
8 static int op_id = 0;
10 static const char *icop_strs[] = {
11 "nop",
12 "add",
13 "sub",
14 "mul",
17 void icblock_create(struct icblock *bl)
19 bl->o_first = bl->o_last = NULL;
20 bl->id = ++block_id;
23 void icblock_destroy(struct icblock *bl)
25 struct icop *op, *op_next;
26 op = bl->o_first;
27 while (op){
28 op_next = op->next;
29 free(op);
30 op = op_next;
34 static void icop_dump(struct icop *op)
36 int i;
37 printf(" %d %s", op->id, icop_strs[op->op]);
38 for (i=0; i<2; ++i){
39 if (op->operands[i]){
40 printf("%s%d", (i==0)?" ":",", op->operands[i]->id);
43 printf("\n");
46 void icblock_dump(struct icblock *bl)
48 struct icop *op = bl->o_first;
49 printf("block%d:\n", bl->id);
50 while (op){
51 icop_dump(op);
52 op = op->next;
56 static struct icop *icop_create(void)
58 // TODO: create a pool of these?
59 struct icop *op = emalloc(sizeof *op);
60 memset(op, 0, sizeof *op);
61 op->id = ++op_id;
62 return op;
65 struct icop *icblock_append(struct icblock *bl)
67 struct icop *op = icop_create();
68 op->prev = bl->o_last;
69 op->next = NULL;
70 *(bl->o_last ? &bl->o_last->next : &bl->o_first) = op;
71 bl->o_last = op;
72 return op;