118 CHECK_FILE (fp, filepath, "writing"); |
118 CHECK_FILE (fp, filepath, "writing"); |
119 |
119 |
120 if (sizeof (unsigned char) != 1) |
120 if (sizeof (unsigned char) != 1) |
121 error ("size of unsigned char isn't 1, but %d!\n", sizeof (unsigned char)); |
121 error ("size of unsigned char isn't 1, but %d!\n", sizeof (unsigned char)); |
122 |
122 |
|
123 unsigned int refpos = 0; |
123 for (unsigned int x = 0; x < MainBuffer->writesize; x++) { |
124 for (unsigned int x = 0; x < MainBuffer->writesize; x++) { |
124 // Check if this position is a reference |
125 // Check if this position is a reference |
125 for (unsigned int r = 0; r < MAX_MARKS; r++) { |
126 for (unsigned int r = 0; r < MAX_MARKS; r++) { |
126 if (MainBuffer->refs[r] && MainBuffer->refs[r]->pos == x) { |
127 if (MainBuffer->refs[r] && MainBuffer->refs[r]->pos == x) { |
127 // All marks need their positions bumped up as the bytecode will gain |
|
128 // 4 more bytes with the written reference. Other references do not |
|
129 // need their positions bumped because they check against mainbuffer |
|
130 // position (x), not written ones. |
|
131 for (unsigned int s = 0; s < MAX_MARKS; s++) |
|
132 if (MainBuffer->marks[s]) |
|
133 MainBuffer->marks[s]->pos += sizeof (word); |
|
134 |
|
135 word ref = static_cast<word> (MainBuffer->marks[MainBuffer->refs[r]->num]->pos); |
128 word ref = static_cast<word> (MainBuffer->marks[MainBuffer->refs[r]->num]->pos); |
|
129 printf ("insert reference to mark %d (%ld) refpos = %d\n", MainBuffer->refs[r]->num, ref, refpos); |
136 WriteDataToFile<word> (ref); |
130 WriteDataToFile<word> (ref); |
137 |
131 |
138 // This reference is now used up - no need to keep it anymore. |
132 // This reference is now used up - no need to keep it anymore. |
139 delete MainBuffer->refs[r]; |
133 delete MainBuffer->refs[r]; |
140 MainBuffer->refs[r] = NULL; |
134 MainBuffer->refs[r] = NULL; |
|
135 |
|
136 // All marks still ahead need their positions bumped up as the bytecode |
|
137 // will gain 4 more bytes with the written reference. Other references |
|
138 // do not need their positions bumped because they check against mainbuffer |
|
139 // position (x), not written ones. |
|
140 for (unsigned int s = 0; s < MAX_MARKS; s++) |
|
141 if (MainBuffer->marks[s] && MainBuffer->marks[s]->pos > refpos) { |
|
142 printf ("mark %u bumped\n", s); |
|
143 MainBuffer->marks[s]->pos += sizeof (word); |
|
144 } |
|
145 refpos += sizeof (word); |
141 } |
146 } |
142 } |
147 } |
143 |
148 |
144 WriteDataToFile<unsigned char> (*(MainBuffer->buffer+x)); |
149 WriteDataToFile<unsigned char> (*(MainBuffer->buffer+x)); |
|
150 refpos++; |
145 } |
151 } |
146 |
152 |
147 printf ("-- %u byte%s written to %s\n", numWrittenBytes, PLURAL (numWrittenBytes), filepath.chars()); |
153 printf ("-- %u byte%s written to %s\n", numWrittenBytes, PLURAL (numWrittenBytes), filepath.chars()); |
148 fclose (fp); |
154 fclose (fp); |
149 } |
155 } |