objwriter.cxx

changeset 42
5cd91fd1526c
parent 41
47e686c96d8f
child 43
1b35c9985989
equal deleted inserted replaced
41:47e686c96d8f 42:5cd91fd1526c
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 // First, resolve references
124 for (unsigned int x = 0; x < MainBuffer->writesize; x++) { 124 for (unsigned int u = 0; u < MAX_MARKS; u++) {
125 // Check if this position is a reference 125 ScriptMarkReference* ref = MainBuffer->refs[u];
126 for (unsigned int r = 0; r < MAX_MARKS; r++) { 126 if (!ref)
127 if (MainBuffer->refs[r] && MainBuffer->refs[r]->pos == x) { 127 continue;
128 word ref = static_cast<word> (MainBuffer->marks[MainBuffer->refs[r]->num]->pos); 128
129 printf ("insert reference to mark %d (%ld) refpos = %d\n", MainBuffer->refs[r]->num, ref, refpos); 129 for (unsigned int v = 0; v < sizeof (word); v++) {
130 WriteDataToFile<word> (ref); 130 union_t<word> uni;
131 131 uni.val = static_cast<word> (MainBuffer->marks[ref->num]->pos);
132 // This reference is now used up - no need to keep it anymore. 132 memset (MainBuffer->buffer + ref->pos + v, uni.b[v], 1);
133 delete MainBuffer->refs[r];
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);
146 }
147 } 133 }
148 134 }
135
136 // Then, dump the main buffer to the file
137 for (unsigned int x = 0; x < MainBuffer->writesize; x++)
149 WriteDataToFile<unsigned char> (*(MainBuffer->buffer+x)); 138 WriteDataToFile<unsigned char> (*(MainBuffer->buffer+x));
150 refpos++;
151 }
152 139
153 printf ("-- %u byte%s written to %s\n", numWrittenBytes, PLURAL (numWrittenBytes), filepath.chars()); 140 printf ("-- %u byte%s written to %s\n", numWrittenBytes, PLURAL (numWrittenBytes), filepath.chars());
154 fclose (fp); 141 fclose (fp);
155 } 142 }
156 143

mercurial