annotate s-code-ppc.c @ 2:1ee4fa9364c7

*** empty log message ***
author kono
date Fri, 27 Oct 2006 17:09:50 +0900
parents
children a2f98ca2d764
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
1 /*
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
2 Very Simple Code Generator for Mac OS X (PPC)
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
3 $Id$
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
4 */
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
5
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
6 #include "s-compile.h"
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
7
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
8 int label = 0;
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
9 char *comments = "#####";
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
10
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
11 static
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
12 char *intro[] = {
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
13 " .section __TEXT,__text,regular,pure_instructions",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
14 " .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
15 " .machine ppc",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
16 " .text",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
17 " .align 2",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
18 " .globl _main",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
19 " _main:",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
20 " mflr r0",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
21 " bcl 20,31,"L00000000001$pb"",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
22 " "L00000000001$pb":",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
23 " mflr r10",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
24 " mtlr r0",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
25 NULL
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
26 };
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
27
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
28 static
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
29 char *ending[] = {
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
30 " .cstring",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
31 " .align 2",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
32 " LC0:",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
33 " .ascii "= %d\12\0"",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
34 " .text",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
35 " .align 2",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
36 " .globl _print",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
37 " _print:",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
38 " mflr r0",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
39 " stw r31,-4(r1)",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
40 " stw r0,8(r1)",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
41 " stwu r1,-80(r1)",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
42 " bcl 20,31,"L00000000002$pb"",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
43 " "L00000000002$pb":",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
44 " mflr r31",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
45 " mr r4,r3",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
46 " addis r3,r31,ha16(LC0-"L00000000002$pb")",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
47 " la r3,lo16(LC0-"L00000000002$pb")(r3)",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
48 " bl L_printf$stub",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
49 " addi r1,r1,80",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
50 " lwz r0,8(r1)",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
51 " mtlr r0",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
52 " lwz r31,-4(r1)",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
53 " mtlr r0",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
54 " lwz r31,-4(r1)",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
55 " blr",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
56 " .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
57 " .align 5",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
58 " L_printf$stub:",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
59 " .indirect_symbol _printf",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
60 " mflr r0",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
61 " bcl 20,31,"L00000000001$spb"",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
62 " "L00000000001$spb":",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
63 " mflr r11",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
64 " addis r11,r11,ha16(L_printf$lazy_ptr-"L00000000001$spb")",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
65 " mtlr r0",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
66 " lwzu r12,lo16(L_printf$lazy_ptr-"L00000000001$spb")(r11)",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
67 " mtctr r12",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
68 " bctr",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
69 " .lazy_symbol_pointer",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
70 " L_printf$lazy_ptr:",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
71 " .indirect_symbol _printf",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
72 " .long dyld_stub_binding_helper",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
73 " .subsections_via_symbols",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
74 "\t.align 2\n",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
75 ".comm _variable,192\n",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
76 NULL
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
77 };
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
78
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
79 void
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
80 emit_push()
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
81 {
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
82 printf("\tstwu r3,-4(r1)\n");
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
83 }
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
84
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
85 void
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
86 emit_compare()
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
87 {
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
88 printf("\tlwz r4,(r1)\n");
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
89 printf("\taddis r1,r1,4\n");
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
90 cmpw cr7,r3,r4
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
91 mfcr r3
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
92 rlwinm r3,r3,30,1
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
93
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
94 }
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
95
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
96 void
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
97 emit_store(assign)
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
98 int assign;
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
99 {
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
100 slwi r3,r3,2
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
101 addis r2,r10,ha16(L_variable$non_lazy_ptr-"L00000000002$pb")
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
102 lwz r2,lo16(L_variable$non_lazy_ptr-"L00000000002$pb")(r2)
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
103 stwx r4,r3,r2
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
104
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
105 }
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
106
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
107 static
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
108 char *opcode[] = {
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
109 "",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
110 "sub",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
111 "add",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
112 "mul",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
113 "div",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
114 "",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
115 "",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
116 "",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
117 "",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
118 "sub",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
119 "div",
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
120 };
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
121
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
122 void
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
123 emit_calc(enum opcode op)
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
124 {
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
125 printf("\tlwz r4,(r1)\n");
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
126 printf("\taddis r1,r1,4\n");
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
127 if(op==O_DIV) {
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
128 printf("\tdiv r4,r3,r3\n");
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
129 } else if(op==O_SUB) {
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
130 printf("\tsub r4,r3,r3\n");
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
131 } else {
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
132 printf("\t%s r4,r3,r3\n",opcode[op]);
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
133 }
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
134 }
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
135
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
136 void
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
137 emit_value(d)
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
138 int d;
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
139 {
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
140 printf("\tmovl $%d,%%eax\n",d);
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
141 }
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
142
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
143 void
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
144 emit_load(d)
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
145 int d;
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
146 {
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
147 printf("\tmovl _variable+%d,%%eax\n",d*4);
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
148 }
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
149
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
150 void
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
151 emit_comment()
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
152 {
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
153 if (before < ptr) {
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
154 putchar('#'); putchar('#'); putchar(' ');
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
155 while (before < ptr) {
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
156 if(*before && *before!='\n') {
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
157 putchar(*before);
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
158 }
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
159 before++;
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
160 }
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
161 putchar('\n');
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
162 }
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
163 }
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
164
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
165 void
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
166 emit_print()
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
167 {
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
168 printf("\tbl _print\n");
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
169 }
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
170
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
171
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
172 void
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
173 emit_intro()
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
174 {
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
175 char **iptr;
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
176 for(iptr=intro;*iptr;iptr++) {
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
177 printf("%s",*iptr);
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
178 }
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
179 }
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
180
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
181 void
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
182 emit_ending()
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
183 {
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
184 char **iptr;
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
185 for(iptr=ending;*iptr;iptr++) {
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
186 printf("%s",*iptr);
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
187 }
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
188 }
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
189
1ee4fa9364c7 *** empty log message ***
kono
parents:
diff changeset
190 /* end */