|
42 | 42 | outum |
43 | 43 | outc %delim |
44 | 44 | } |
| 45 | + |
| 46 | +; copies of list members, with debug information printing |
| 47 | +%void_method{ list_clear_debug, 1, 1, ; next=null |
| 48 | + %print_spaced( ; list struct idx |
| 49 | + outur |
| 50 | + outc ':' |
| 51 | + ) |
| 52 | + %seg:set(%local, 0, 0) |
| 53 | + %while { %load(%this), ; THIS != null |
| 54 | + %seg:load(%this, %list_node:next) ; new_next <- THIS.next <- null |
| 55 | + str 0 |
| 56 | + %stack:pushr() ; new_next |
| 57 | + %if { %seg:load(%local, 0), ; old_next != null |
| 58 | + %stack:dup_over(1) |
| 59 | + outur ; what free called at |
| 60 | + outc 32 |
| 61 | + %call(free) |
| 62 | + } |
| 63 | + %seg:pop(%local, 0) ; next = new_next |
| 64 | + %storer(%this) ; THIS = new_next |
| 65 | + } |
| 66 | + outc '\n' |
| 67 | +} |
| 68 | +%macro list_debug_deinit() { |
| 69 | + %stack:pushh() |
| 70 | + %call(list_clear_debug) |
| 71 | +} |
| 72 | +%macro list_debug_destructor() { |
| 73 | + ldm |
| 74 | + str 0 |
| 75 | + %stack:pushr() |
| 76 | + movr |
| 77 | + %list_debug_deinit() |
| 78 | + %print_endl { |
| 79 | + %stack:top() ; emit free argument |
| 80 | + outc 'F' |
| 81 | + outum |
| 82 | + } |
| 83 | + %list:dealloc() |
| 84 | +} |
| 85 | +; ------------------------------------ |
| 86 | + |
45 | 87 | %macro populateFirstList() { ; initialize array at local:0 |
46 | 88 | %callListMethodWithArgs(list_insert_after, 0, %stack:push(5)) |
47 | 89 | %callListMethodWithArgs(list_insert_after, 0, %stack:push(6)) |
48 | 90 | %callListMethodWithArgs(list_insert_back, 0, %stack:push(7)) |
49 | 91 | } |
50 | | - |
51 | | -%void_func{ main, 0, 6, ; 2 * sizeof(list) + sizeof(ptr) + sizeof(array) |
| 92 | +%macro initLocalLists() { ; initialize local arrays |
52 | 93 | %seg:local(0, %list:init()) ; first 2 lists live in locals |
53 | 94 | %seg:local(1, %list:init()) ; NOTE: testing of different schemes |
54 | 95 | %seg:local(2, %list:construct_here()) ; third lives in heap (here is ptr to that struct) |
55 | 96 | %seg:local(3, %array:init()) |
56 | | - |
| 97 | +} |
| 98 | +%void_func{ main, 0, 6, ; 2 * sizeof(list) + sizeof(ptr) + sizeof(array) |
| 99 | + %initLocalLists() |
57 | 100 | %populateFirstList() |
58 | 101 |
|
59 | 102 | ; A.insert_after(list[1], 2) |
|
82 | 125 |
|
83 | 126 | ; TODO splice |
84 | 127 |
|
85 | | - ; set elems to their idx |
| 128 | + ; set elems to their idx, emit node addresses |
| 129 | + %print_spaced( outc 'A' ) |
| 130 | + %print_spaced( outc '&' ) |
86 | 131 | %seg:point(%this, %local, 0) |
87 | 132 | %list:foreach { |
| 133 | + %print_spaced( outuh ) |
88 | 134 | %stack:top() |
89 | 135 | %seg:store(%this, %list_node:value) |
90 | 136 | } |
| 137 | + outc '\n' |
91 | 138 | %call(print_lists) |
| 139 | + |
| 140 | + ; list destructing |
| 141 | + %callListMethod(list_clear_debug, 0) ; A.clear() |
| 142 | + %seg:load(%local, 0) |
| 143 | + %print_endl( ; see list was deinitialized |
| 144 | + outc 'D' |
| 145 | + outum |
| 146 | + ) |
| 147 | + %callListMethod(list_clear_debug, 1) ; B.clear() |
| 148 | + %seg:move(%local, 2) ; empty C.destructor() |
| 149 | + %list_debug_destructor() |
| 150 | + %seg:load(%local, 2) |
| 151 | + %print_endl( |
| 152 | + outc 'C' |
| 153 | + outum |
| 154 | + ) |
| 155 | + %callWith1Arg(malloc, 1) ; change the new location |
| 156 | + %callWith1Arg(malloc, 1) |
| 157 | + |
| 158 | + %initLocalLists() |
| 159 | + %callListMethodWithArgs(list_insert_after, 2, %stack:push(69)) |
| 160 | + %seg:move(%local, 2) ; nonempty C.destructor() |
| 161 | + %print_endl( |
| 162 | + outc 'V' |
| 163 | + outum |
| 164 | + ) |
| 165 | + %list_debug_destructor() |
| 166 | + %seg:load(%local, 2) |
| 167 | + %print_endl( |
| 168 | + outc 'D' |
| 169 | + outum |
| 170 | + ) |
92 | 171 | } |
93 | 172 |
|
94 | 173 | %call(main) |
|
0 commit comments