72 void ObjWriter::WriteString (str s) { |
72 void ObjWriter::WriteString (str s) { |
73 WriteString (s.chars()); |
73 WriteString (s.chars()); |
74 } |
74 } |
75 |
75 |
76 void ObjWriter::WriteBuffer (DataBuffer* buf) { |
76 void ObjWriter::WriteBuffer (DataBuffer* buf) { |
77 for (unsigned int x = 0; x < buf->writesize; x++) { |
77 GetCurrentBuffer()->Merge (buf); |
78 unsigned char c = *(buf->buffer+x); |
|
79 Write<unsigned char> (c); |
|
80 } |
|
81 } |
78 } |
82 |
79 |
83 void ObjWriter::WriteBuffers () { |
80 void ObjWriter::WriteBuffers () { |
84 // If there was no mainloop defined, write a dummy one now. |
81 // If there was no mainloop defined, write a dummy one now. |
85 if (!g_GotMainLoop) { |
82 if (!g_GotMainLoop) { |
91 for (int i = 0; i < 2; i++) { |
88 for (int i = 0; i < 2; i++) { |
92 DataBuffer* buf = (!i) ? OnEnterBuffer : MainLoopBuffer; |
89 DataBuffer* buf = (!i) ? OnEnterBuffer : MainLoopBuffer; |
93 WriteBuffer (buf); |
90 WriteBuffer (buf); |
94 |
91 |
95 // Clear the buffer afterwards for potential next state |
92 // Clear the buffer afterwards for potential next state |
96 delete buf; |
|
97 buf = new DataBuffer; |
93 buf = new DataBuffer; |
98 } |
94 } |
99 |
95 |
100 // Next state definitely has no mainloop yet |
96 // Next state definitely has no mainloop yet |
101 g_GotMainLoop = false; |
97 g_GotMainLoop = false; |
126 |
122 |
127 for (unsigned int x = 0; x < MainBuffer->writesize; x++) { |
123 for (unsigned int x = 0; x < MainBuffer->writesize; x++) { |
128 // Check if this position is a reference |
124 // Check if this position is a reference |
129 for (unsigned int r = 0; r < MAX_MARKS; r++) { |
125 for (unsigned int r = 0; r < MAX_MARKS; r++) { |
130 if (MainBuffer->refs[r] && MainBuffer->refs[r]->pos == x) { |
126 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 |
131 word ref = static_cast<word> (MainBuffer->marks[MainBuffer->refs[r]->num]->pos); |
135 word ref = static_cast<word> (MainBuffer->marks[MainBuffer->refs[r]->num]->pos); |
132 printf ("insert position %ld at script pos %u\n", |
|
133 ref, x); |
|
134 WriteDataToFile<word> (ref); |
136 WriteDataToFile<word> (ref); |
135 |
137 |
136 // This reference is now used up |
138 // This reference is now used up - no need to keep it anymore. |
137 delete MainBuffer->refs[r]; |
139 delete MainBuffer->refs[r]; |
138 MainBuffer->refs[r] = NULL; |
140 MainBuffer->refs[r] = NULL; |
139 |
|
140 // All other references need their positions bumped up as the |
|
141 // bytecode gained 4 more bytes with the written reference |
|
142 for (unsigned int s = 0; s < MAX_MARKS; s++) |
|
143 if (MainBuffer->refs[s]) |
|
144 MainBuffer->refs[s]->pos += sizeof (word); |
|
145 } |
141 } |
146 } |
142 } |
147 |
143 |
148 WriteDataToFile<unsigned char> (*(MainBuffer->buffer+x)); |
144 WriteDataToFile<unsigned char> (*(MainBuffer->buffer+x)); |
149 } |
145 } |