Imported Upstream version 2.9.0
[debian/cc1111] / src / izt / gen_generic.c
1 #include "izt.h"
2 #include "gen.h"
3
4 static void _setupPointer(REG *reg, asmop *into, int offset)
5 {
6     iemit("; _setupPointer for %s", reg->name);
7 }
8
9 void izt_putAop(asmop *into, const char *sz, int size, int offset)
10 {
11     wassert(offset == 0);
12
13     switch (into->type) {
14     case AOP_TYPE_REG:
15         iemit("mov %s,%s", into->u.reg->name, sz);
16         break;
17     case AOP_TYPE_CARRY:
18         // Should support.
19         wassert(0);
20         break;
21
22     case AOP_TYPE_SCRATCH_PTR:
23         _setupPointer(izt_port->scratch, into, offset);
24         iemit("mov a,%s", sz);
25         iemit("mov %s,a", izt_port->scratch->name);
26         break;
27
28     case AOP_TYPE_STACK:
29         iemit("mov a,%s", sz);
30         iemit("mov (%s+%d),a", izt_port->base_ptr->name, into->u.stack);
31         break;
32
33     case AOP_TYPE_LITERAL:
34     case AOP_TYPE_IMMEDIATE:
35     default:
36         wassert(0);
37     }
38 }
39
40 char *izt_getAop(asmop *from, int size, int offset)
41 {
42     return "blah";
43 }
44
45 /** Perform a generic move operation.
46  */
47 static void _mov(asmop *into, asmop *from)
48 {
49     int size = into->size;
50     izt_putAop(into, izt_getAop(from, size, 0), size, 0);
51 }
52
53 static void _genLabel(iCode *ic)
54 {
55     iemit("!tlabeldef", IC_LABEL(ic)->key + 100);
56 }
57
58 static void _genGoto(iCode *ic)
59 {
60     iemit("jp !tlabel", IC_LABEL(ic)->key+100);
61 }
62
63 static void _genFunction(iCode *ic)
64 {
65     symbol *sym = OP_SYMBOL(IC_LEFT(ic));
66
67     // Create the function header
68     iemit("!functionheader", sym->name);
69     iemit("!functionlabeldef", sym->rname);
70
71     if (sym->stack) {
72         iemit("!enterx", sym->stack);
73     }
74   else
75     {
76       iemit ("!enter");
77     }
78 }
79
80 static void _genEndFunction(iCode *ic)
81 {
82     //    symbol *sym = OP_SYMBOL(IC_LEFT(ic));
83
84     /* PENDING: calleeSave */
85
86     iemit("!leave");
87     iemit("ret");
88 }
89
90 static void
91 _genReturn (iCode * ic)
92 {
93     if (IC_LEFT(ic)) {
94         // Has a return value.  Load it up.
95         izt_bindAop(IC_LEFT(ic), ic);
96         _mov(izt_getAopForReturn(AOP_SIZE(IC_LEFT(ic))), AOP(IC_LEFT(ic)));
97     }
98
99   if (ic->next && ic->next->op == LABEL && IC_LABEL (ic->next) == returnLabel)
100     {
101       // The next label is the return label.  Dont bother emitting a jump.
102     }
103   else
104     {
105       iemit ("jp !tlabel", returnLabel->key + 100);
106     }
107 }
108
109 static EMITTER _base_emitters[] = {
110     { LABEL,            _genLabel },
111     { GOTO,             _genGoto },
112     { FUNCTION,         _genFunction },
113     { RETURN,           _genReturn },
114     { ENDFUNCTION,      _genEndFunction },
115     { 0,                NULL }
116 };
117
118 void
119 izt_initBaseEmitters (hTab ** into)
120 {
121   izt_addEmittersToHTab (into, _base_emitters);
122 }