Revision | 535 (tree) |
---|---|
Time | 2019-03-26 01:33:06 |
Author | jakobthomsen |
factor-out emit-init
@@ -283,6 +283,88 @@ | ||
283 | 283 | |
284 | 284 | break; |
285 | 285 | } |
286 | + case 0x82892F92992F000LLU: | |
287 | + { | |
288 | + | |
289 | + const uint64_t memsize = memory[memory[state.stackbase + 0]]; | |
290 | + const uint64_t heapsize = memory[memory[state.stackbase + 1]]; | |
291 | + const uint64_t fndefs = memory[memory[state.stackbase + 2]]; | |
292 | + printf("void init(struct state *state, uint64_t *memory)\n"); | |
293 | + printf("{\n"); | |
294 | + | |
295 | + uint64_t maindef_id = 0; | |
296 | + uint64_t maindef_arg_count = 0; | |
297 | + uint64_t maindef_body_size = 0; | |
298 | + | |
299 | + { | |
300 | + uint64_t defref = fndefs; | |
301 | + while(defref) | |
302 | + { | |
303 | + //printid(stderr, memory[defref + 1]); | |
304 | + if(memory[defref + 1] == 0xA1C929000000000LLU) | |
305 | + { | |
306 | + maindef_id = memory[defref + 1]; | |
307 | + maindef_arg_count = memory[memory[defref] + 1]; | |
308 | + maindef_body_size = memory[memory[memory[defref]] + 1]; | |
309 | + break; | |
310 | + } | |
311 | + defref = memory[defref + 0]; | |
312 | + defref = memory[defref + 0]; | |
313 | + defref = memory[defref + 0]; | |
314 | + } | |
315 | + if(!defref) | |
316 | + { | |
317 | + fprintf(stderr, "no such function! "); | |
318 | + printid(stderr, 0xA1C929000000000LLU); | |
319 | + fprintf(stderr, "\n"); | |
320 | + exit(-1); | |
321 | + } | |
322 | + } | |
323 | + | |
324 | + if(maindef_arg_count != 1) | |
325 | + { | |
326 | + fprintf(stderr, "function "); | |
327 | + printid(stderr, maindef_id); | |
328 | + fprintf(stderr, " needs exactly one argument (address of result) but has %llu\n", (unsigned long long)maindef_arg_count); | |
329 | + exit(-1); | |
330 | + } | |
331 | + | |
332 | + uint64_t total_size = maindef_arg_count + maindef_body_size + 4/* END return-addr, END stackbase, skip null addr*/; | |
333 | + | |
334 | + if(memsize < total_size) | |
335 | + { | |
336 | + fprintf(stderr, "out of memory calling "); | |
337 | + printid(stderr, maindef_id); | |
338 | + fprintf(stderr, " - need %llu chunks but memory-size is only %llu\n", (unsigned long long)total_size, (unsigned long long)memsize); | |
339 | + exit(-1); | |
340 | + } | |
341 | + | |
342 | + fprintf(stderr, "CALL "); | |
343 | + printid(stderr, 0xA1C929000000000LLU); | |
344 | + fprintf(stderr, ": %llu\n", (unsigned long long)memsize); | |
345 | + | |
346 | + printf(" state->stackbase = 0;\n"); | |
347 | + printf(" memory[state->stackbase++] = 0;\n"); // dummy - zero used to mark invalid address | |
348 | + printf(" memory[state->stackbase++] = 2 * %lluLLU;\n", (unsigned long long)heapsize); // store address of FREELIST (only for memory-management) | |
349 | + printf(" for(uint64_t i = 0; i < %lluLLU; i++)\n", (unsigned long long)heapsize); // initialize globals free-list | |
350 | + printf(" {\n"); | |
351 | + printf(" memory[state->stackbase] = state->stackbase - 2;\n"); // free-list: each chunk contains link to chunk below (lowest one points to zero which means end-of-list) | |
352 | + printf(" state->stackbase++;\n"); | |
353 | + printf(" memory[state->stackbase] = 0;\n"); // dummy element | |
354 | + printf(" state->stackbase++;\n"); | |
355 | + printf(" }\n"); | |
356 | + printf(" memory[state->stackbase++] = 0;\n"); // PREVIOUS stack-base (before calling main): dummy value (never used) | |
357 | + printf(" memory[state->stackbase++] = 0;\n"); // end-marker (stop program on returning here) | |
358 | + printf(" memory[state->stackbase] = 2 + 2 * %lluLLU;\n", (unsigned long long)heapsize); // address where main places result | |
359 | + printf(" state->addr = 0x%0llXLLU;\n", (unsigned long long)maindef_id); | |
360 | + | |
361 | + printf("}\n"); | |
362 | + | |
363 | + state.addr = memory[--state.stackbase]; | |
364 | + state.stackbase = memory[state.stackbase - 1]; | |
365 | + | |
366 | + break; | |
367 | + } | |
286 | 368 | case 0x929BEDA80000000LLU: |
287 | 369 | { |
288 | 370 |
@@ -1245,121 +1327,34 @@ | ||
1245 | 1327 | { |
1246 | 1328 | |
1247 | 1329 | printf("\n"); |
1248 | - printf("void init(struct state *state, uint64_t *memory)\n"); | |
1249 | - printf("{\n"); | |
1250 | - | |
1251 | - uint64_t maindef_id = 0; | |
1252 | - uint64_t maindef_arg_count = 0; | |
1253 | - uint64_t maindef_body_size = 0; | |
1254 | - | |
1255 | - { | |
1256 | - uint64_t defref = memory[state.stackbase + 4]; | |
1257 | - while(defref) | |
1258 | - { | |
1259 | - //printid(stderr, memory[defref + 1]); | |
1260 | - if(memory[defref + 1] == 0xA1C929000000000LLU) | |
1261 | - { | |
1262 | - maindef_id = memory[defref + 1]; | |
1263 | - maindef_arg_count = memory[memory[defref] + 1]; | |
1264 | - maindef_body_size = memory[memory[memory[defref]] + 1]; | |
1265 | - break; | |
1266 | - } | |
1267 | - defref = memory[defref + 0]; | |
1268 | - defref = memory[defref + 0]; | |
1269 | - defref = memory[defref + 0]; | |
1270 | - } | |
1271 | - if(!defref) | |
1272 | - { | |
1273 | - fprintf(stderr, "no such function! "); | |
1274 | - printid(stderr, 0xA1C929000000000LLU); | |
1275 | - fprintf(stderr, "\n"); | |
1276 | - exit(-1); | |
1277 | - } | |
1278 | - } | |
1279 | - | |
1280 | - if(maindef_arg_count != 1) | |
1281 | - { | |
1282 | - fprintf(stderr, "function "); | |
1283 | - printid(stderr, maindef_id); | |
1284 | - fprintf(stderr, " needs exactly one argument (address of result) but has %llu\n", (unsigned long long)maindef_arg_count); | |
1285 | - exit(-1); | |
1286 | - } | |
1287 | - | |
1288 | - uint64_t total_size = maindef_arg_count + maindef_body_size + 4/* END return-addr, END stackbase, skip null addr*/; | |
1289 | - | |
1290 | - if(memory[state.stackbase + 1]/*memsize*/ < total_size) | |
1291 | - { | |
1292 | - fprintf(stderr, "out of memory calling "); | |
1293 | - printid(stderr, maindef_id); | |
1294 | - fprintf(stderr, " - need %llu chunks but memory-size is only %llu\n", (unsigned long long)total_size, (unsigned long long)memory[state.stackbase + 1]/*memsize*/); | |
1295 | - exit(-1); | |
1296 | - } | |
1297 | - | |
1298 | - fprintf(stderr, "CALL "); | |
1299 | - printid(stderr, 0xA1C929000000000LLU); | |
1300 | - fprintf(stderr, ": %llu\n", (unsigned long long)memory[state.stackbase + 1]/*memsize*/); | |
1301 | - | |
1302 | - printf(" state->stackbase = 0;\n"); | |
1303 | - printf(" memory[state->stackbase++] = 0;\n"); // dummy - zero used to mark invalid address | |
1304 | - printf(" memory[state->stackbase++] = 2 * %lluLLU;\n", (unsigned long long)memory[state.stackbase + 2]/*heapsize*/); // store address of FREELIST (only for memory-management) | |
1305 | - printf(" for(uint64_t i = 0; i < %lluLLU; i++)\n", (unsigned long long)memory[state.stackbase + 2]/*heapsize*/); // initialize globals free-list | |
1306 | - printf(" {\n"); | |
1307 | - printf(" memory[state->stackbase] = state->stackbase - 2;\n"); // free-list: each chunk contains link to chunk below (lowest one points to zero which means end-of-list) | |
1308 | - printf(" state->stackbase++;\n"); | |
1309 | - printf(" memory[state->stackbase] = 0;\n"); // dummy element | |
1310 | - printf(" state->stackbase++;\n"); | |
1311 | - printf(" }\n"); | |
1312 | - printf(" memory[state->stackbase++] = 0;\n"); // PREVIOUS stack-base (before calling main): dummy value (never used) | |
1313 | - printf(" memory[state->stackbase++] = 0;\n"); // end-marker (stop program on returning here) | |
1314 | - printf(" memory[state->stackbase] = 2 + 2 * %lluLLU;\n", (unsigned long long)memory[state.stackbase + 2]/*heapsize*/); // address where main places result | |
1315 | - printf(" state->addr = 0x%0llXLLU;\n", (unsigned long long)maindef_id); | |
1316 | - | |
1317 | - printf("}\n"); | |
1318 | - printf("\n"); | |
1319 | - | |
1330 | + | |
1320 | 1331 | memory[state.stackbase + 5 + 0] = state.stackbase; |
1321 | 1332 | memory[state.stackbase + 5 + 1] = 0xFFFFFFFFFFFFFFF5LLU; |
1322 | - memory[state.stackbase + 7] = state.stackbase + 4; | |
1333 | + memory[state.stackbase + 7] = state.stackbase + 1; | |
1334 | + memory[state.stackbase + 8] = state.stackbase + 2; | |
1335 | + memory[state.stackbase + 9] = state.stackbase + 4; | |
1323 | 1336 | state.stackbase += 7; |
1324 | - state.addr = 0xB60AEAB6F7E086ELLU; | |
1337 | + state.addr = 0x82892F92992F000LLU; | |
1325 | 1338 | |
1326 | 1339 | break; |
1327 | 1340 | } |
1328 | 1341 | case 0xFFFFFFFFFFFFFFF5LLU: |
1329 | 1342 | { |
1330 | - list_free(memory, &memory[state.stackbase + 4]); | |
1331 | 1343 | |
1332 | - memory[state.stackbase + 5] = 0x0LLU; | |
1333 | - memory[state.stackbase + 5] = 579; | |
1344 | + printf("\n"); | |
1334 | 1345 | |
1335 | - memory[state.stackbase + 6] = 0x1C8LLU; | |
1346 | + memory[state.stackbase + 5 + 0] = state.stackbase; | |
1347 | + memory[state.stackbase + 5 + 1] = 0xFFFFFFFFFFFFFFF4LLU; | |
1348 | + memory[state.stackbase + 7] = state.stackbase + 4; | |
1349 | + state.stackbase += 7; | |
1350 | + state.addr = 0xB60AEAB6F7E086ELLU; | |
1336 | 1351 | |
1337 | - memory[state.stackbase + 7] = 0x0LLU; | |
1338 | - | |
1339 | - memory[state.stackbase + 8 + 0] = state.stackbase; | |
1340 | - memory[state.stackbase + 8 + 1] = 0xFFFFFFFFFFFFFFF4LLU; | |
1341 | - memory[state.stackbase + 10] = state.stackbase + 7; | |
1342 | - memory[state.stackbase + 11] = state.stackbase + 5; | |
1343 | - memory[state.stackbase + 12] = state.stackbase + 6; | |
1344 | - state.stackbase += 10; | |
1345 | - state.addr = 0xBB0740000000000LLU; | |
1346 | - | |
1347 | 1352 | break; |
1348 | 1353 | } |
1349 | 1354 | case 0xFFFFFFFFFFFFFFF4LLU: |
1350 | 1355 | { |
1356 | + list_free(memory, &memory[state.stackbase + 4]); | |
1351 | 1357 | |
1352 | - memory[state.stackbase + 8 + 0] = state.stackbase; | |
1353 | - memory[state.stackbase + 8 + 1] = 0xFFFFFFFFFFFFFFF3LLU; | |
1354 | - memory[state.stackbase + 10] = state.stackbase + 7; | |
1355 | - state.stackbase += 10; | |
1356 | - state.addr = 0xB60AEAB6FA6D000LLU; | |
1357 | - | |
1358 | - break; | |
1359 | - } | |
1360 | - case 0xFFFFFFFFFFFFFFF3LLU: | |
1361 | - { | |
1362 | - | |
1363 | 1358 | fprintf(stderr, "done\n"); |
1364 | 1359 | memory[memory[state.stackbase + 0]] = 0; |
1365 | 1360 |
@@ -93,6 +93,85 @@ | ||
93 | 93 | ` |
94 | 94 | } |
95 | 95 | |
96 | +emitinit(3) 0 | |
97 | +{ // memsize, heapsize, fndefs | |
98 | + ` | |
99 | + const uint64_t memsize = memory[memory[state.stackbase + 0]]; | |
100 | + const uint64_t heapsize = memory[memory[state.stackbase + 1]]; | |
101 | + const uint64_t fndefs = memory[memory[state.stackbase + 2]]; | |
102 | + printf("void init(struct state *state, uint64_t *memory)\n"); | |
103 | + printf("{\n"); | |
104 | + | |
105 | + uint64_t maindef_id = 0; | |
106 | + uint64_t maindef_arg_count = 0; | |
107 | + uint64_t maindef_body_size = 0; | |
108 | + | |
109 | + { | |
110 | + uint64_t defref = fndefs; | |
111 | + while(defref) | |
112 | + { | |
113 | + //printid(stderr, memory[defref + 1]); | |
114 | + if(memory[defref + 1] == 0xA1C929000000000LLU) | |
115 | + { | |
116 | + maindef_id = memory[defref + 1]; | |
117 | + maindef_arg_count = memory[memory[defref] + 1]; | |
118 | + maindef_body_size = memory[memory[memory[defref]] + 1]; | |
119 | + break; | |
120 | + } | |
121 | + defref = memory[defref + 0]; | |
122 | + defref = memory[defref + 0]; | |
123 | + defref = memory[defref + 0]; | |
124 | + } | |
125 | + if(!defref) | |
126 | + { | |
127 | + fprintf(stderr, "no such function! "); | |
128 | + printid(stderr, 0xA1C929000000000LLU); | |
129 | + fprintf(stderr, "\n"); | |
130 | + exit(-1); | |
131 | + } | |
132 | + } | |
133 | + | |
134 | + if(maindef_arg_count != 1) | |
135 | + { | |
136 | + fprintf(stderr, "function "); | |
137 | + printid(stderr, maindef_id); | |
138 | + fprintf(stderr, " needs exactly one argument (address of result) but has %llu\n", (unsigned long long)maindef_arg_count); | |
139 | + exit(-1); | |
140 | + } | |
141 | + | |
142 | + uint64_t total_size = maindef_arg_count + maindef_body_size + 4/* END return-addr, END stackbase, skip null addr*/; | |
143 | + | |
144 | + if(memsize < total_size) | |
145 | + { | |
146 | + fprintf(stderr, "out of memory calling "); | |
147 | + printid(stderr, maindef_id); | |
148 | + fprintf(stderr, " - need %llu chunks but memory-size is only %llu\n", (unsigned long long)total_size, (unsigned long long)memsize); | |
149 | + exit(-1); | |
150 | + } | |
151 | + | |
152 | + fprintf(stderr, "CALL "); | |
153 | + printid(stderr, 0xA1C929000000000LLU); | |
154 | + fprintf(stderr, ": %llu\n", (unsigned long long)memsize); | |
155 | + | |
156 | + printf(" state->stackbase = 0;\n"); | |
157 | + printf(" memory[state->stackbase++] = 0;\n"); // dummy - zero used to mark invalid address | |
158 | + printf(" memory[state->stackbase++] = 2 * %lluLLU;\n", (unsigned long long)heapsize); // store address of FREELIST (only for memory-management) | |
159 | + printf(" for(uint64_t i = 0; i < %lluLLU; i++)\n", (unsigned long long)heapsize); // initialize globals free-list | |
160 | + printf(" {\n"); | |
161 | + printf(" memory[state->stackbase] = state->stackbase - 2;\n"); // free-list: each chunk contains link to chunk below (lowest one points to zero which means end-of-list) | |
162 | + printf(" state->stackbase++;\n"); | |
163 | + printf(" memory[state->stackbase] = 0;\n"); // dummy element | |
164 | + printf(" state->stackbase++;\n"); | |
165 | + printf(" }\n"); | |
166 | + printf(" memory[state->stackbase++] = 0;\n"); // PREVIOUS stack-base (before calling main): dummy value (never used) | |
167 | + printf(" memory[state->stackbase++] = 0;\n"); // end-marker (stop program on returning here) | |
168 | + printf(" memory[state->stackbase] = 2 + 2 * %lluLLU;\n", (unsigned long long)heapsize); // address where main places result | |
169 | + printf(" state->addr = 0x%0llXLLU;\n", (unsigned long long)maindef_id); | |
170 | + | |
171 | + printf("}\n"); | |
172 | + ` | |
173 | +} | |
174 | + | |
96 | 175 | intro() 0 |
97 | 176 | { |
98 | 177 | print("#include <ctype.h>\n") |
@@ -738,94 +817,14 @@ | ||
738 | 817 | print(" }\n") |
739 | 818 | print("\n") |
740 | 819 | verifyheap(#2) |
741 | -` | |
742 | -printf("\n"); | |
743 | -printf("void init(struct state *state, uint64_t *memory)\n"); | |
744 | -printf("{\n"); | |
745 | - | |
746 | -uint64_t maindef_id = 0; | |
747 | -uint64_t maindef_arg_count = 0; | |
748 | -uint64_t maindef_body_size = 0; | |
749 | - | |
750 | -{ | |
751 | - uint64_t defref = memory[state.stackbase + 4]; | |
752 | - while(defref) | |
753 | - { | |
754 | - //printid(stderr, memory[defref + 1]); | |
755 | - if(memory[defref + 1] == 0xA1C929000000000LLU) | |
756 | - { | |
757 | - maindef_id = memory[defref + 1]; | |
758 | - maindef_arg_count = memory[memory[defref] + 1]; | |
759 | - maindef_body_size = memory[memory[memory[defref]] + 1]; | |
760 | - break; | |
761 | - } | |
762 | - defref = memory[defref + 0]; | |
763 | - defref = memory[defref + 0]; | |
764 | - defref = memory[defref + 0]; | |
765 | - } | |
766 | - if(!defref) | |
767 | - { | |
768 | - fprintf(stderr, "no such function! "); | |
769 | - printid(stderr, 0xA1C929000000000LLU); | |
770 | - fprintf(stderr, "\n"); | |
771 | - exit(-1); | |
772 | - } | |
773 | -} | |
774 | - | |
775 | -if(maindef_arg_count != 1) | |
776 | -{ | |
777 | - fprintf(stderr, "function "); | |
778 | - printid(stderr, maindef_id); | |
779 | - fprintf(stderr, " needs exactly one argument (address of result) but has %llu\n", (unsigned long long)maindef_arg_count); | |
780 | - exit(-1); | |
781 | -} | |
782 | - | |
783 | -uint64_t total_size = maindef_arg_count + maindef_body_size + 4/* END return-addr, END stackbase, skip null addr*/; | |
784 | - | |
785 | -if(memory[state.stackbase + 1]/*memsize*/ < total_size) | |
786 | -{ | |
787 | - fprintf(stderr, "out of memory calling "); | |
788 | - printid(stderr, maindef_id); | |
789 | - fprintf(stderr, " - need %llu chunks but memory-size is only %llu\n", (unsigned long long)total_size, (unsigned long long)memory[state.stackbase + 1]/*memsize*/); | |
790 | - exit(-1); | |
791 | -} | |
792 | - | |
793 | -fprintf(stderr, "CALL "); | |
794 | -printid(stderr, 0xA1C929000000000LLU); | |
795 | -fprintf(stderr, ": %llu\n", (unsigned long long)memory[state.stackbase + 1]/*memsize*/); | |
796 | - | |
797 | -printf(" state->stackbase = 0;\n"); | |
798 | -printf(" memory[state->stackbase++] = 0;\n"); // dummy - zero used to mark invalid address | |
799 | -printf(" memory[state->stackbase++] = 2 * %lluLLU;\n", (unsigned long long)memory[state.stackbase + 2]/*heapsize*/); // store address of FREELIST (only for memory-management) | |
800 | -printf(" for(uint64_t i = 0; i < %lluLLU; i++)\n", (unsigned long long)memory[state.stackbase + 2]/*heapsize*/); // initialize globals free-list | |
801 | -printf(" {\n"); | |
802 | -printf(" memory[state->stackbase] = state->stackbase - 2;\n"); // free-list: each chunk contains link to chunk below (lowest one points to zero which means end-of-list) | |
803 | -printf(" state->stackbase++;\n"); | |
804 | -printf(" memory[state->stackbase] = 0;\n"); // dummy element | |
805 | -printf(" state->stackbase++;\n"); | |
806 | -printf(" }\n"); | |
807 | -printf(" memory[state->stackbase++] = 0;\n"); // PREVIOUS stack-base (before calling main): dummy value (never used) | |
808 | -printf(" memory[state->stackbase++] = 0;\n"); // end-marker (stop program on returning here) | |
809 | -printf(" memory[state->stackbase] = 2 + 2 * %lluLLU;\n", (unsigned long long)memory[state.stackbase + 2]/*heapsize*/); // address where main places result | |
810 | -printf(" state->addr = 0x%0llXLLU;\n", (unsigned long long)maindef_id); | |
811 | - | |
812 | -printf("}\n"); | |
813 | -printf("\n"); | |
814 | -` | |
820 | + print("\n") | |
821 | + emitinit(#1 #2 #4) | |
822 | + print("\n") | |
815 | 823 | ==5== |
816 | 824 | reportdefs(#4) |
817 | 825 | free(#4) // free fndef list |
818 | 826 | ==5== |
819 | -let # = 0 | |
820 | -#5 = 579 | |
821 | -==6== | |
822 | -let # = 456 | |
823 | -==7== | |
824 | -let # = 0 | |
825 | -==8== | |
826 | -sub(#7 #5 #6) | |
827 | -reportnr(#7) | |
828 | -==8== | |
829 | -report("done\n"); | |
827 | + report("done\n"); | |
828 | +==5== | |
830 | 829 | *#0=0 |
831 | 830 | } |