-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcc_compiledscript.cpp
More file actions
308 lines (290 loc) · 9.58 KB
/
Copy pathcc_compiledscript.cpp
File metadata and controls
308 lines (290 loc) · 9.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cc_compiledscript.h"
#include "script_common.h" // macro definitions
#include "cc_symboltable.h" // symbolTable
#include "cc_options.h" // ccGetOption
#include "cc_error.h"
void ccCompiledScript::write_cmd(int cmdd) {
write_code(cmdd);
}
void ccCompiledScript::write_cmd1(int cmdd,int param) {
write_code(cmdd);
write_code(param);
}
void ccCompiledScript::write_cmd2(int cmdd,int param,int param2) {
write_code(cmdd);
write_code(param);
write_code(param2);
}
void ccCompiledScript::write_cmd3(int cmdd,int param,int param2,int param3) {
write_code(cmdd);
write_code(param);
write_code(param2);
write_code(param3);
}
void ccCompiledScript::push_reg(int regg) {
write_cmd1(SCMD_PUSHREG,regg);
cur_sp += 4;
}
void ccCompiledScript::pop_reg(int regg) {
write_cmd1(SCMD_POPREG,regg);
cur_sp -= 4;
}
ccCompiledScript::ccCompiledScript() {
init();
ax_val_type = 0;
ax_val_scope = 0;
}
ccCompiledScript::~ccCompiledScript() {
shutdown();
}
int ccCompiledScript::add_global(int siz,const char*vall) {
// printf("Add global size %d at %d\n",siz,globaldatasize);
// if (remove_any_import (vall)) return -2;
globaldata = (char*)realloc(globaldata, globaldatasize + siz);
if (vall != NULL)
memcpy(&globaldata[globaldatasize],vall,siz);
else memset(&globaldata[globaldatasize],0,siz);
int toret = globaldatasize;
globaldatasize += siz;
return toret;
}
int ccCompiledScript::add_string(const char*strr) {
size_t len = strlen(strr);
strings = (char*)realloc(strings, stringssize + len + 1);
char *write_ptr = strings + stringssize;
for (size_t src = 0; src <= len; ++src) {
char ch = strr[src];
if (ch=='\\') {
src++;
ch = strr[src];
if (ch == 'n') {ch = '\n';}
else if (ch == 'r') {ch = '\r';}
else if (ch == '[') { // pass through as \[
*write_ptr = '\\';
write_ptr++;
}
}
*write_ptr = ch;
write_ptr++;
}
int toret = stringssize;
stringssize += write_ptr - (strings + stringssize);
return toret;
}
void ccCompiledScript::add_fixup(int32_t locc, char ftype) {
fixuptypes = (char*)realloc(fixuptypes, numfixups + 5);
fixups = (int32_t*)realloc(fixups, (numfixups * sizeof(int32_t)) + 10);
fixuptypes[numfixups] = ftype;
fixups[numfixups] = locc;
numfixups++;
}
void ccCompiledScript::fixup_previous(char ftype) {
add_fixup(codesize-1,ftype);
}
int ccCompiledScript::add_new_function(const char*namm, int *idx) {
if (numfunctions >= MAX_FUNCTIONS) return -1;
// if (remove_any_import (namm)) return -2;
functions[numfunctions] = (char*)malloc(strlen(namm)+20);
strcpy(functions[numfunctions],namm);
funccodeoffs[numfunctions] = codesize;
funcnumparams[numfunctions] = 0;
// code = (long*)realloc(code, codesize + 5000);
if (idx)
*idx = numfunctions;
numfunctions++;
return codesize;
}
int ccCompiledScript::add_new_import(const char*namm)
{
if (numimports >= importsCapacity)
{
importsCapacity += 1000;
imports = (char**)realloc(imports, sizeof(char*) * importsCapacity);
}
imports[numimports] = (char*)malloc(strlen(namm)+12);
strcpy(imports[numimports],namm);
numimports++;
return numimports-1;
}
int ccCompiledScript::remove_any_import (const char*namm, SymbolDef *oldSym) {
// Remove any import with the specified name
int i, sidx;
sidx = sym.find(namm);
if (sidx < 0)
return 0;
if ((sym.entries[sidx].flags & SFLG_IMPORTED) == 0)
return 0;
// if this import has been referenced, flag an error
if (sym.entries[sidx].flags & SFLG_ACCESSED) {
cc_error("Already referenced name as import; you must define it before using it");
return -1;
}
// if they set the No Override Imports flag, don't allow it
if (ccGetOption(SCOPT_NOIMPORTOVERRIDE)) {
cc_error("Variable '%s' is already imported", namm);
return -1;
}
if (oldSym) {
// Copy the import declaration to a backup struct
// This allows a type comparison to be done
// strip the imported flag, since it the real def won't be
oldSym->flags = sym.entries[sidx].flags & ~SFLG_IMPORTED;
oldSym->stype = sym.entries[sidx].stype;
oldSym->sscope = sym.entries[sidx].sscope;
// Return size may have been unknown at the time of forward declaration. Check the actual return type for those cases.
if(sym.entries[sidx].stype == SYM_FUNCTION && sym.entries[sidx].ssize == 0) {
oldSym->ssize = sym.entries[sym.entries[sidx].funcparamtypes[0] & ~(STYPE_POINTER | STYPE_DYNARRAY)].ssize;
} else {
oldSym->ssize = sym.entries[sidx].ssize;
}
oldSym->arrsize = sym.entries[sidx].arrsize;
if (sym.entries[sidx].stype == SYM_FUNCTION) {
// <= because of return type
for (i = 0; i <= sym.entries[sidx].get_num_args(); i++) {
oldSym->funcparamtypes[i] = sym.entries[sidx].funcparamtypes[i];
oldSym->funcParamDefaultValues[i] = sym.entries[sidx].funcParamDefaultValues[i];
oldSym->funcParamHasDefaultValues[i] = sym.entries[sidx].funcParamHasDefaultValues[i];
}
}
}
// remove its type so that it can be declared
sym.entries[sidx].stype = 0;
sym.entries[sidx].flags = 0;
// check also for a number-of-parameters appended version
char appended[200];
sprintf(appended, "%s^", namm);
int applen = strlen(appended);
for (i = 0; i < numimports; i++) {
if (strcmp(imports[i], namm) == 0) {
// Just null the name of the import
// DO NOT remove the import from the list, as some other
// import indexes might already be referenced by the code
// compiled so far.
imports[i][0] = 0;
}
else if (strncmp(imports[i], appended, applen) == 0) {
imports[i][0] = 0;
}
}
return 0;
}
int ccCompiledScript::add_new_export(const char*namm,int etype,long eoffs, int numArgs)
{
if (numexports >= exportsCapacity)
{
exportsCapacity += 1000;
exports = (char**)realloc(exports, sizeof(char*) * exportsCapacity);
export_addr = (int32_t*)realloc(export_addr, sizeof(int32_t) * exportsCapacity);
}
if (eoffs >= 0x00ffffff) {
cc_error("export offset too high; script data size too large?");
return -1;
}
char *newName = (char*)malloc(strlen(namm)+20);
strcpy(newName, namm);
// mangle the name for functions to record parameters
if (etype == EXPORT_FUNCTION) {
char tempAddition[20];
sprintf(tempAddition, "$%d", numArgs);
strcat(newName, tempAddition);
}
// Check if it's already exported
for (int aa = 0; aa < numexports; aa++) {
if (strcmp(exports[aa], newName) == 0) {
free(newName);
return aa;
}
}
exports[numexports] = newName;
export_addr[numexports] = eoffs | ((long)etype << 24L);
numexports++;
return numexports-1;
}
void ccCompiledScript::flush_line_numbers() {
if (next_line) {
int linum = next_line;
next_line = 0;
if(ccGetOption(SCOPT_LINENUMBERS)) {
write_code(SCMD_LINENUM);
write_code(linum);
}
}
}
void ccCompiledScript::write_code(int32_t byy) {
flush_line_numbers();
if (codesize >= codeallocated - 2) {
codeallocated += 500;
code = (int32_t*)realloc(code,codeallocated*sizeof(int32_t));
}
code[codesize] = byy;
codesize++;
}
const char* ccCompiledScript::start_new_section(const char *name) {
if ((numSections == 0) ||
(codesize != sectionOffsets[numSections - 1]))
{
if (numSections >= capacitySections)
{
capacitySections += 100;
sectionNames = (char**)realloc(sectionNames, sizeof(char*) * capacitySections);
sectionOffsets = (int32_t*)realloc(sectionOffsets, sizeof(int32_t) * capacitySections);
}
sectionNames[numSections] = (char*)malloc(strlen(name) + 1);
strcpy(sectionNames[numSections], name);
sectionOffsets[numSections] = codesize;
numSections++;
}
else
{
// nothing was in the last section, so overwrite it with this new one
free(sectionNames[numSections - 1]);
sectionNames[numSections - 1] = (char*)malloc(strlen(name) + 1);
strcpy(sectionNames[numSections - 1], name);
}
return sectionNames[numSections - 1];
}
void ccCompiledScript::init() {
globaldata = NULL;
globaldatasize = 0;
code = NULL;
codesize = 0;
codeallocated = 0;
numfunctions = 0;
strings = NULL;
stringssize = 0;
cur_sp=0;
fixups = NULL;
fixuptypes = NULL;
numfixups = 0;
numimports = 0;
numexports = 0;
numSections = 0;
importsCapacity = 0;
imports = NULL;
exportsCapacity = 0;
exports = NULL;
export_addr = NULL;
capacitySections = 0;
sectionNames = NULL;
sectionOffsets = NULL;
next_line = 0;
ax_val_type = 0;
ax_val_scope = 0;
memset(functions, 0, sizeof(functions));
memset(funccodeoffs, 0, sizeof(funccodeoffs));
memset(funcnumparams, 0, sizeof(funcnumparams));
}
// free the extra bits that ccScript doesn't have
void ccCompiledScript::free_extra() {
int aa;
for (aa=0;aa<numfunctions;aa++) {
free(functions[aa]);
}
numfunctions=0;
}
void ccCompiledScript::shutdown() {
free_extra();
}