• R/O
  • SSH
  • HTTPS

jpl: Commit


Commit MetaInfo

Revision485 (tree)
Time2019-03-12 02:38:30
Authorjakobthomsen

Log Message

switch to accurate memory management

Change Summary

Incremental Difference

--- trunk/experimental2/compiler.compiled.c (revision 484)
+++ trunk/experimental2/compiler.compiled.c (revision 485)
@@ -316,55 +316,198 @@
316316
317317 struct stack
318318 {
319- struct full_tree_with_height tree;
320- uint64_t fill;
319+ uint64_t root;
320+ uint64_t size;
321321 };
322322
323-struct stack stack_alloc()
323+struct stack stack_alloc(uint64_t size)
324324 {
325325 struct stack stack;
326- stack.tree.height = 0;
327- stack.tree.root = full_tree_alloc(stack.tree.height);
328- stack.fill = 0;
326+ stack.root = 0;
327+ stack.size = size;
328+ uint64_t height = 0;
329+ if(size)
330+ {
331+ while(!(size & 1))
332+ {
333+ height++;
334+ size >>= 1;
335+ }
336+
337+ stack.root = full_tree_alloc(height);
338+
339+ size >>= 1;
340+ height++;
341+
342+ while(size)
343+ {
344+ if(size & 1)
345+ {
346+ stack.root = pair_alloc_and_set(pair(full_tree_alloc(height), stack.root));
347+ }
348+
349+ height++;
350+ size >>= 1;
351+ }
352+ }
353+
329354 return stack;
330355 }
331356
332357 void stack_free(struct stack *const stack)
333358 {
334- full_tree_free(stack->tree.height, stack->tree.root);
359+ if(stack->size)
360+ {
361+ uint64_t height = 64;
362+ while(height > 0)
363+ {
364+ height--;
365+
366+ uint64_t mask = 1llu << height;
367+ if(stack->size & mask)
368+ {
369+ stack->size &= ~mask;
370+ if(!stack->size)
371+ break;
372+
373+ full_tree_free(height, memory[stack->root].pair.elem0);
374+ stack->root = pair_free_and_get(stack->root).elem1;
375+ }
376+ }
377+
378+ full_tree_free(height, stack->root);
379+ }
335380 }
336381
382+uint64_t count_ones(uint64_t size)
383+{
384+ uint64_t spine = 0;
385+ while(size) // count ones
386+ {
387+ while(!(size & 1)) // skip zeroes
388+ {
389+ size >>= 1; // zero
390+ }
391+
392+ size >>= 1; // one
393+ spine++;
394+ }
395+
396+ return spine;
397+}
398+
337399 void stack_push(struct stack *const stack, uint64_t value)
338400 {
339- // TODO: check for out-of-memory (runtime or compile-time)
340- if(stack->fill >= (1 << stack->tree.height)) // allocate double memory if stack full
341- full_tree_with_height_dup(&stack->tree);
342- memory[full_tree_with_height_access(stack->tree, stack->fill)].value = value;
343- stack->fill++;
401+ uint64_t *ptr = &stack->root;
402+ uint64_t spine = count_ones(stack->size++);
403+
404+ if(!spine)
405+ {
406+ stack->root = value_alloc_and_set(value);
407+ return;
408+ }
409+
410+ while(spine > 1)
411+ {
412+ --spine;
413+ ptr = &memory[*ptr].pair.elem1;
414+ }
415+
416+ *ptr = pair_alloc_and_set(pair(*ptr, value_alloc_and_set(value)));
344417 }
345418
346419 uint64_t stack_pop(struct stack *const stack)
347420 {
348- if(!stack->fill)
421+ if(!stack->size)
349422 {
350- fprintf(stderr, "ERROR: stack_pop UNDERFLOW\\n");
423+ fprintf(stderr, "pop: ERROR, stack empty!\n");
351424 exit(-1);
352425 }
353426
354- stack->fill--;
355- // NOTE: stack is halved only if fill is quarter full
356- // NOTE: varying combinations of fill & height depending (push resp. pull)
357- if((stack->fill << 2) < (1 << stack->tree.height) - 1)
358- full_tree_with_height_half(&stack->tree);
427+ uint64_t *ptr = &stack->root;
428+ uint64_t spine = count_ones(--stack->size);
359429
360- return memory[full_tree_with_height_access(stack->tree, stack->fill)].value;
430+ if(!spine)
431+ return value_free_and_get(*ptr);
432+
433+ while(spine > 1)
434+ {
435+ --spine;
436+ ptr = &memory[*ptr].pair.elem1;
437+ }
438+
439+ uint64_t result = value_free_and_get(memory[*ptr].pair.elem1);
440+ uint64_t sub = memory[*ptr].pair.elem0;
441+ pair_free(*ptr);
442+ *ptr = sub;
443+
444+ return result;
361445 }
362446
447+uint64_t stack_elem_addr(struct stack stack, uint64_t index)
448+{
449+ uint64_t node = stack.root;
450+
451+ if(index < stack.size) // NOTE: this implies stack.size > 0
452+ {
453+ uint64_t last = stack.size - 1;
454+ uint64_t height = 64;
455+
456+ // select full tree
457+ while(height > 0)
458+ {
459+ height--;
460+ uint64_t mask = 1llu << height;
461+
462+ if(last & mask)
463+ {
464+ if(index & mask)
465+ {
466+ node = memory[node].pair.elem1;
467+ }
468+ else
469+ {
470+ node = memory[node].pair.elem0;
471+ break;
472+ }
473+ }
474+ }
475+
476+ // select leaf in full tree
477+ while(height > 0)
478+ {
479+ height--;
480+
481+ uint64_t mask = 1llu << height;
482+ if(index & mask)
483+ {
484+ node = memory[node].pair.elem1;
485+ }
486+ else
487+ {
488+ node = memory[node].pair.elem0;
489+ }
490+ }
491+ }
492+ else
493+ {
494+ fprintf(stderr, "index %d out of range 0..%d\n", (int)index, (int)stack.size);
495+ exit(-1);
496+ }
497+
498+ return node;
499+}
500+
501+uint64_t *stack_elem_ref(struct stack stack, uint64_t index)
502+{
503+ return &memory[stack_elem_addr(stack, index)].value;
504+}
505+
363506 int main()
364507 {
365508 uint64_t tmpaddr = 0; // assign temporary addresses highest first (pre-decrement)
366509 mem_init();
367- uint64_t result = 0; struct stack callstack = stack_alloc();
510+ uint64_t result = 0; struct stack callstack = stack_alloc(0);
368511 stack_push(&callstack, 0); // end marker
369512 uint64_t stackframe = tuple2_alloc(/*main*/1835100526LLU, 0/*arg*/);
370513 while(memory[tuple2_access0(stackframe)].value)
@@ -697,56 +840,199 @@
697840 printf("\n");
698841 printf("struct stack\n");
699842 printf("{\n");
700- printf(" struct full_tree_with_height tree;\n");
701- printf(" uint64_t fill;\n");
843+ printf(" uint64_t root;\n");
844+ printf(" uint64_t size;\n");
702845 printf("};\n");
703846 printf("\n");
704- printf("struct stack stack_alloc()\n");
847+ printf("struct stack stack_alloc(uint64_t size)\n");
705848 printf("{\n");
706849 printf(" struct stack stack;\n");
707- printf(" stack.tree.height = 0;\n");
708- printf(" stack.tree.root = full_tree_alloc(stack.tree.height);\n");
709- printf(" stack.fill = 0;\n");
850+ printf(" stack.root = 0;\n");
851+ printf(" stack.size = size;\n");
852+ printf(" uint64_t height = 0;\n");
853+ printf(" if(size)\n");
854+ printf(" {\n");
855+ printf(" while(!(size & 1))\n");
856+ printf(" {\n");
857+ printf(" height++;\n");
858+ printf(" size >>= 1;\n");
859+ printf(" }\n");
860+ printf("\n");
861+ printf(" stack.root = full_tree_alloc(height);\n");
862+ printf("\n");
863+ printf(" size >>= 1;\n");
864+ printf(" height++;\n");
865+ printf("\n");
866+ printf(" while(size)\n");
867+ printf(" {\n");
868+ printf(" if(size & 1)\n");
869+ printf(" {\n");
870+ printf(" stack.root = pair_alloc_and_set(pair(full_tree_alloc(height), stack.root));\n");
871+ printf(" }\n");
872+ printf("\n");
873+ printf(" height++;\n");
874+ printf(" size >>= 1;\n");
875+ printf(" }\n");
876+ printf(" }\n");
877+ printf("\n");
710878 printf(" return stack;\n");
711879 printf("}\n");
712880 printf("\n");
713881 printf("void stack_free(struct stack *const stack)\n");
714882 printf("{\n");
715- printf(" full_tree_free(stack->tree.height, stack->tree.root);\n");
883+ printf(" if(stack->size)\n");
884+ printf(" {\n");
885+ printf(" uint64_t height = 64;\n");
886+ printf(" while(height > 0)\n");
887+ printf(" {\n");
888+ printf(" height--;\n");
889+ printf("\n");
890+ printf(" uint64_t mask = 1llu << height;\n");
891+ printf(" if(stack->size & mask)\n");
892+ printf(" {\n");
893+ printf(" stack->size &= ~mask;\n");
894+ printf(" if(!stack->size)\n");
895+ printf(" break;\n");
896+ printf("\n");
897+ printf(" full_tree_free(height, memory[stack->root].pair.elem0);\n");
898+ printf(" stack->root = pair_free_and_get(stack->root).elem1;\n");
899+ printf(" }\n");
900+ printf(" }\n");
901+ printf("\n");
902+ printf(" full_tree_free(height, stack->root);\n");
903+ printf(" }\n");
716904 printf("}\n");
717905 printf("\n");
906+ printf("uint64_t count_ones(uint64_t size)\n");
907+ printf("{\n");
908+ printf(" uint64_t spine = 0;\n");
909+ printf(" while(size) // count ones\n");
910+ printf(" {\n");
911+ printf(" while(!(size & 1)) // skip zeroes\n");
912+ printf(" {\n");
913+ printf(" size >>= 1; // zero\n");
914+ printf(" }\n");
915+ printf("\n");
916+ printf(" size >>= 1; // one\n");
917+ printf(" spine++;\n");
918+ printf(" }\n");
919+ printf("\n");
920+ printf(" return spine;\n");
921+ printf("}\n");
922+ printf("\n");
718923 printf("void stack_push(struct stack *const stack, uint64_t value)\n");
719924 printf("{\n");
720- printf(" // TODO: check for out-of-memory (runtime or compile-time)\n");
721- printf(" if(stack->fill >= (1 << stack->tree.height)) // allocate double memory if stack full\n");
722- printf(" full_tree_with_height_dup(&stack->tree);\n");
723- printf(" memory[full_tree_with_height_access(stack->tree, stack->fill)].value = value;\n");
724- printf(" stack->fill++;\n");
925+ printf(" uint64_t *ptr = &stack->root;\n");
926+ printf(" uint64_t spine = count_ones(stack->size++);\n");
927+ printf("\n");
928+ printf(" if(!spine)\n");
929+ printf(" {\n");
930+ printf(" stack->root = value_alloc_and_set(value);\n");
931+ printf(" return;\n");
932+ printf(" }\n");
933+ printf("\n");
934+ printf(" while(spine > 1)\n");
935+ printf(" {\n");
936+ printf(" --spine;\n");
937+ printf(" ptr = &memory[*ptr].pair.elem1;\n");
938+ printf(" }\n");
939+ printf("\n");
940+ printf(" *ptr = pair_alloc_and_set(pair(*ptr, value_alloc_and_set(value)));\n");
725941 printf("}\n");
726942 printf("\n");
727943 printf("uint64_t stack_pop(struct stack *const stack)\n");
728944 printf("{\n");
729- printf(" if(!stack->fill)\n");
945+ printf(" if(!stack->size)\n");
730946 printf(" {\n");
731- printf(" fprintf(stderr, \"ERROR: stack_pop UNDERFLOW\\\\n\");\n");
947+ printf(" fprintf(stderr, \"pop: ERROR, stack empty!\\n\");\n");
732948 printf(" exit(-1);\n");
733949 printf(" }\n");
734950 printf("\n");
735- printf(" stack->fill--;\n");
736- printf(" // NOTE: stack is halved only if fill is quarter full\n");
737- printf(" // NOTE: varying combinations of fill & height depending (push resp. pull)\n");
738- printf(" if((stack->fill << 2) < (1 << stack->tree.height) - 1)\n");
739- printf(" full_tree_with_height_half(&stack->tree);\n");
951+ printf(" uint64_t *ptr = &stack->root;\n");
952+ printf(" uint64_t spine = count_ones(--stack->size);\n");
740953 printf("\n");
741- printf(" return memory[full_tree_with_height_access(stack->tree, stack->fill)].value;\n");
954+ printf(" if(!spine)\n");
955+ printf(" return value_free_and_get(*ptr);\n");
956+ printf("\n");
957+ printf(" while(spine > 1)\n");
958+ printf(" {\n");
959+ printf(" --spine;\n");
960+ printf(" ptr = &memory[*ptr].pair.elem1;\n");
961+ printf(" }\n");
962+ printf("\n");
963+ printf(" uint64_t result = value_free_and_get(memory[*ptr].pair.elem1);\n");
964+ printf(" uint64_t sub = memory[*ptr].pair.elem0;\n");
965+ printf(" pair_free(*ptr);\n");
966+ printf(" *ptr = sub;\n");
967+ printf("\n");
968+ printf(" return result;\n");
742969 printf("}\n");
743970 printf("\n");
971+ printf("uint64_t stack_elem_addr(struct stack stack, uint64_t index)\n");
972+ printf("{\n");
973+ printf(" uint64_t node = stack.root;\n");
974+ printf("\n");
975+ printf(" if(index < stack.size) // NOTE: this implies stack.size > 0\n");
976+ printf(" {\n");
977+ printf(" uint64_t last = stack.size - 1;\n");
978+ printf(" uint64_t height = 64;\n");
979+ printf("\n");
980+ printf(" // select full tree\n");
981+ printf(" while(height > 0)\n");
982+ printf(" {\n");
983+ printf(" height--;\n");
984+ printf(" uint64_t mask = 1llu << height;\n");
985+ printf("\n");
986+ printf(" if(last & mask)\n");
987+ printf(" {\n");
988+ printf(" if(index & mask)\n");
989+ printf(" {\n");
990+ printf(" node = memory[node].pair.elem1;\n");
991+ printf(" }\n");
992+ printf(" else\n");
993+ printf(" {\n");
994+ printf(" node = memory[node].pair.elem0;\n");
995+ printf(" break;\n");
996+ printf(" }\n");
997+ printf(" }\n");
998+ printf(" }\n");
999+ printf("\n");
1000+ printf(" // select leaf in full tree\n");
1001+ printf(" while(height > 0)\n");
1002+ printf(" {\n");
1003+ printf(" height--;\n");
1004+ printf("\n");
1005+ printf(" uint64_t mask = 1llu << height;\n");
1006+ printf(" if(index & mask)\n");
1007+ printf(" {\n");
1008+ printf(" node = memory[node].pair.elem1;\n");
1009+ printf(" }\n");
1010+ printf(" else\n");
1011+ printf(" {\n");
1012+ printf(" node = memory[node].pair.elem0;\n");
1013+ printf(" }\n");
1014+ printf(" }\n");
1015+ printf(" }\n");
1016+ printf(" else\n");
1017+ printf(" {\n");
1018+ printf(" fprintf(stderr, \"index %%d out of range 0..%%d\\n\", (int)index, (int)stack.size);\n");
1019+ printf(" exit(-1);\n");
1020+ printf(" }\n");
1021+ printf("\n");
1022+ printf(" return node;\n");
1023+ printf("}\n");
1024+ printf("\n");
1025+ printf("uint64_t *stack_elem_ref(struct stack stack, uint64_t index)\n");
1026+ printf("{\n");
1027+ printf(" return &memory[stack_elem_addr(stack, index)].value;\n");
1028+ printf("}\n");
1029+ printf("\n");
7441030 printf("int main()\n");
7451031 printf("{\n");
7461032 printf(" uint64_t tmpaddr = 0; // assign temporary addresses highest first (pre-decrement)\n");
7471033 printf(" mem_init();\n");
7481034 printf(" uint64_t result = 0;");
749- printf(" struct stack callstack = stack_alloc();\n");
1035+ printf(" struct stack callstack = stack_alloc(0);\n");
7501036 printf(" stack_push(&callstack, 0); // end marker\n");
7511037 printf(" uint64_t stackframe = tuple2_alloc(/*main*/1835100526LLU, 0/*arg*/);\n");
7521038 printf(" while(memory[tuple2_access0(stackframe)].value)\n");
--- trunk/experimental2/compiler.source.c (revision 484)
+++ trunk/experimental2/compiler.source.c (revision 485)
@@ -324,56 +324,199 @@
324324 printf("\n");
325325 printf("struct stack\n");
326326 printf("{\n");
327- printf(" struct full_tree_with_height tree;\n");
328- printf(" uint64_t fill;\n");
327+ printf(" uint64_t root;\n");
328+ printf(" uint64_t size;\n");
329329 printf("};\n");
330330 printf("\n");
331- printf("struct stack stack_alloc()\n");
331+ printf("struct stack stack_alloc(uint64_t size)\n");
332332 printf("{\n");
333333 printf(" struct stack stack;\n");
334- printf(" stack.tree.height = 0;\n");
335- printf(" stack.tree.root = full_tree_alloc(stack.tree.height);\n");
336- printf(" stack.fill = 0;\n");
334+ printf(" stack.root = 0;\n");
335+ printf(" stack.size = size;\n");
336+ printf(" uint64_t height = 0;\n");
337+ printf(" if(size)\n");
338+ printf(" {\n");
339+ printf(" while(!(size & 1))\n");
340+ printf(" {\n");
341+ printf(" height++;\n");
342+ printf(" size >>= 1;\n");
343+ printf(" }\n");
344+ printf("\n");
345+ printf(" stack.root = full_tree_alloc(height);\n");
346+ printf("\n");
347+ printf(" size >>= 1;\n");
348+ printf(" height++;\n");
349+ printf("\n");
350+ printf(" while(size)\n");
351+ printf(" {\n");
352+ printf(" if(size & 1)\n");
353+ printf(" {\n");
354+ printf(" stack.root = pair_alloc_and_set(pair(full_tree_alloc(height), stack.root));\n");
355+ printf(" }\n");
356+ printf("\n");
357+ printf(" height++;\n");
358+ printf(" size >>= 1;\n");
359+ printf(" }\n");
360+ printf(" }\n");
361+ printf("\n");
337362 printf(" return stack;\n");
338363 printf("}\n");
339364 printf("\n");
340365 printf("void stack_free(struct stack *const stack)\n");
341366 printf("{\n");
342- printf(" full_tree_free(stack->tree.height, stack->tree.root);\n");
367+ printf(" if(stack->size)\n");
368+ printf(" {\n");
369+ printf(" uint64_t height = 64;\n");
370+ printf(" while(height > 0)\n");
371+ printf(" {\n");
372+ printf(" height--;\n");
373+ printf("\n");
374+ printf(" uint64_t mask = 1llu << height;\n");
375+ printf(" if(stack->size & mask)\n");
376+ printf(" {\n");
377+ printf(" stack->size &= ~mask;\n");
378+ printf(" if(!stack->size)\n");
379+ printf(" break;\n");
380+ printf("\n");
381+ printf(" full_tree_free(height, memory[stack->root].pair.elem0);\n");
382+ printf(" stack->root = pair_free_and_get(stack->root).elem1;\n");
383+ printf(" }\n");
384+ printf(" }\n");
385+ printf("\n");
386+ printf(" full_tree_free(height, stack->root);\n");
387+ printf(" }\n");
343388 printf("}\n");
344389 printf("\n");
390+ printf("uint64_t count_ones(uint64_t size)\n");
391+ printf("{\n");
392+ printf(" uint64_t spine = 0;\n");
393+ printf(" while(size) // count ones\n");
394+ printf(" {\n");
395+ printf(" while(!(size & 1)) // skip zeroes\n");
396+ printf(" {\n");
397+ printf(" size >>= 1; // zero\n");
398+ printf(" }\n");
399+ printf("\n");
400+ printf(" size >>= 1; // one\n");
401+ printf(" spine++;\n");
402+ printf(" }\n");
403+ printf("\n");
404+ printf(" return spine;\n");
405+ printf("}\n");
406+ printf("\n");
345407 printf("void stack_push(struct stack *const stack, uint64_t value)\n");
346408 printf("{\n");
347- printf(" // TODO: check for out-of-memory (runtime or compile-time)\n");
348- printf(" if(stack->fill >= (1 << stack->tree.height)) // allocate double memory if stack full\n");
349- printf(" full_tree_with_height_dup(&stack->tree);\n");
350- printf(" memory[full_tree_with_height_access(stack->tree, stack->fill)].value = value;\n");
351- printf(" stack->fill++;\n");
409+ printf(" uint64_t *ptr = &stack->root;\n");
410+ printf(" uint64_t spine = count_ones(stack->size++);\n");
411+ printf("\n");
412+ printf(" if(!spine)\n");
413+ printf(" {\n");
414+ printf(" stack->root = value_alloc_and_set(value);\n");
415+ printf(" return;\n");
416+ printf(" }\n");
417+ printf("\n");
418+ printf(" while(spine > 1)\n");
419+ printf(" {\n");
420+ printf(" --spine;\n");
421+ printf(" ptr = &memory[*ptr].pair.elem1;\n");
422+ printf(" }\n");
423+ printf("\n");
424+ printf(" *ptr = pair_alloc_and_set(pair(*ptr, value_alloc_and_set(value)));\n");
352425 printf("}\n");
353426 printf("\n");
354427 printf("uint64_t stack_pop(struct stack *const stack)\n");
355428 printf("{\n");
356- printf(" if(!stack->fill)\n");
429+ printf(" if(!stack->size)\n");
357430 printf(" {\n");
358- printf(" fprintf(stderr, \"ERROR: stack_pop UNDERFLOW\\\\n\");\n");
431+ printf(" fprintf(stderr, \"pop: ERROR, stack empty!\\n\");\n");
359432 printf(" exit(-1);\n");
360433 printf(" }\n");
361434 printf("\n");
362- printf(" stack->fill--;\n");
363- printf(" // NOTE: stack is halved only if fill is quarter full\n");
364- printf(" // NOTE: varying combinations of fill & height depending (push resp. pull)\n");
365- printf(" if((stack->fill << 2) < (1 << stack->tree.height) - 1)\n");
366- printf(" full_tree_with_height_half(&stack->tree);\n");
435+ printf(" uint64_t *ptr = &stack->root;\n");
436+ printf(" uint64_t spine = count_ones(--stack->size);\n");
367437 printf("\n");
368- printf(" return memory[full_tree_with_height_access(stack->tree, stack->fill)].value;\n");
438+ printf(" if(!spine)\n");
439+ printf(" return value_free_and_get(*ptr);\n");
440+ printf("\n");
441+ printf(" while(spine > 1)\n");
442+ printf(" {\n");
443+ printf(" --spine;\n");
444+ printf(" ptr = &memory[*ptr].pair.elem1;\n");
445+ printf(" }\n");
446+ printf("\n");
447+ printf(" uint64_t result = value_free_and_get(memory[*ptr].pair.elem1);\n");
448+ printf(" uint64_t sub = memory[*ptr].pair.elem0;\n");
449+ printf(" pair_free(*ptr);\n");
450+ printf(" *ptr = sub;\n");
451+ printf("\n");
452+ printf(" return result;\n");
369453 printf("}\n");
370454 printf("\n");
455+ printf("uint64_t stack_elem_addr(struct stack stack, uint64_t index)\n");
456+ printf("{\n");
457+ printf(" uint64_t node = stack.root;\n");
458+ printf("\n");
459+ printf(" if(index < stack.size) // NOTE: this implies stack.size > 0\n");
460+ printf(" {\n");
461+ printf(" uint64_t last = stack.size - 1;\n");
462+ printf(" uint64_t height = 64;\n");
463+ printf("\n");
464+ printf(" // select full tree\n");
465+ printf(" while(height > 0)\n");
466+ printf(" {\n");
467+ printf(" height--;\n");
468+ printf(" uint64_t mask = 1llu << height;\n");
469+ printf("\n");
470+ printf(" if(last & mask)\n");
471+ printf(" {\n");
472+ printf(" if(index & mask)\n");
473+ printf(" {\n");
474+ printf(" node = memory[node].pair.elem1;\n");
475+ printf(" }\n");
476+ printf(" else\n");
477+ printf(" {\n");
478+ printf(" node = memory[node].pair.elem0;\n");
479+ printf(" break;\n");
480+ printf(" }\n");
481+ printf(" }\n");
482+ printf(" }\n");
483+ printf("\n");
484+ printf(" // select leaf in full tree\n");
485+ printf(" while(height > 0)\n");
486+ printf(" {\n");
487+ printf(" height--;\n");
488+ printf("\n");
489+ printf(" uint64_t mask = 1llu << height;\n");
490+ printf(" if(index & mask)\n");
491+ printf(" {\n");
492+ printf(" node = memory[node].pair.elem1;\n");
493+ printf(" }\n");
494+ printf(" else\n");
495+ printf(" {\n");
496+ printf(" node = memory[node].pair.elem0;\n");
497+ printf(" }\n");
498+ printf(" }\n");
499+ printf(" }\n");
500+ printf(" else\n");
501+ printf(" {\n");
502+ printf(" fprintf(stderr, \"index %%d out of range 0..%%d\\n\", (int)index, (int)stack.size);\n");
503+ printf(" exit(-1);\n");
504+ printf(" }\n");
505+ printf("\n");
506+ printf(" return node;\n");
507+ printf("}\n");
508+ printf("\n");
509+ printf("uint64_t *stack_elem_ref(struct stack stack, uint64_t index)\n");
510+ printf("{\n");
511+ printf(" return &memory[stack_elem_addr(stack, index)].value;\n");
512+ printf("}\n");
513+ printf("\n");
371514 printf("int main()\n");
372515 printf("{\n");
373516 printf(" uint64_t tmpaddr = 0; // assign temporary addresses highest first (pre-decrement)\n");
374517 printf(" mem_init();\n");
375518 printf(" uint64_t result = 0;");
376- printf(" struct stack callstack = stack_alloc();\n");
519+ printf(" struct stack callstack = stack_alloc(0);\n");
377520 printf(" stack_push(&callstack, 0); // end marker\n");
378521 printf(" uint64_t stackframe = tuple2_alloc(/*main*/1835100526LLU, 0/*arg*/);\n");
379522 printf(" while(memory[tuple2_access0(stackframe)].value)\n");
Show on old repository browser