/
ImportCache.cpp
445 lines (406 loc) · 12 KB
/
ImportCache.cpp
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
* OpenBOR - http://www.chronocrash.com
* -----------------------------------------------------------------------
* Licensed under the BSD license, see LICENSE in OpenBOR root for details.
*
* Copyright (c) 2004 - 2016 OpenBOR Team
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "Interpreter.hpp"
#include "List.hpp"
// #include "packfile.h"
#include "ImportCache.hpp"
#include "SSABuilder.hpp"
#include "Parser.hpp"
#include "liveness.hpp"
#include "RegAlloc.hpp"
#include "Builtins.hpp"
#include "ExecBuilder.hpp"
#include "pp_parser.h"
//#define IC_DEBUG 1
List<Interpreter*> compiledScripts; // names are lowercased, forward-slashed paths
/**
* Reads a script file into an allocated buffer. Be sure to call free() on the
* returned buffer when you are done with it!
*
* Returns the buffer on success, NULL on failure.
*/
static char *readScript(const char *path)
{
#if 1 // stdio version
char *scriptText = NULL;
int fileSize;
FILE *fp = fopen(path, "rb");
if(!fp) goto error;
if (fseek(fp, 0, SEEK_END) < 0) goto error;
fileSize = ftell(fp);
scriptText = (char*) malloc(fileSize + 1);
if (fseek(fp, 0, SEEK_SET) < 0) goto error;
if ((int)fread(scriptText, 1, fileSize, fp) != fileSize) goto error;
scriptText[fileSize] = 0;
fclose(fp);
return scriptText;
error:
// ideally, this error message would include the name of the file that tried to import this file
printf("Script error: unable to open file '%s' for importing\n", path);
if (scriptText)
{
free(scriptText);
}
if (fp)
{
fclose(fp);
}
return NULL;
#else // packfile I/O version
int handle = openpackfile(path, packfile);
int size;
char *buffer = NULL;
if(handle < 0)
{
goto error;
}
size = seekpackfile(handle, 0, SEEK_END);
if(size < 0)
{
goto error;
}
buffer = malloc(size + 1);
if(buffer == NULL)
{
goto error;
}
if(seekpackfile(handle, 0, SEEK_SET) < 0)
{
goto error;
}
if(readpackfile(handle, buffer, size) < 0)
{
goto error;
}
closepackfile(handle);
buffer[size] = '\0';
return buffer;
error:
// ideally, this error message would include the name of the file that tried to import this file
printf("Script error: unable to open file '%s' for importing\n", path);
if(buffer)
{
free(buffer);
}
if(handle >= 0)
{
closepackfile(handle);
}
return NULL;
#endif
}
bool compile(SSABuilder *func)
{
printf("\n~~~~~ %s (%i params) ~~~~~\n", func->functionName, func->paramCount);
#if 0
printf("Instructions before processing:\n");
func->printInstructionList();
#endif
// optimization passes
func->foldConstantCalls();
func->removeDeadCode();
func->prepareForRegAlloc();
#if DEBUG_RA
printf("\nInstructions after processing:\n");
func->printInstructionList();
printf("\n%i temporaries\n\n", func->temporaries.size());
#endif
computeLiveSets(func);
#if DEBUG_RA
printf("\nLive sets:\n");
foreach_list(func->basicBlockList, BasicBlock*, iter)
{
BasicBlock *block = iter.value();
printf("BB %i: in: ", block->id);
block->liveIn.print();
printf(", out:");
block->liveOut.print();
printf("\n");
}
#endif
LivenessAnalyzer livenessAnalyzer(func);
livenessAnalyzer.computeLiveIntervals();
livenessAnalyzer.coalesce();
livenessAnalyzer.buildInterferenceGraph();
RegAlloc registerAllocator(livenessAnalyzer.values, livenessAnalyzer.uniqueNodes);
registerAllocator.run();
#if DEBUG_RA
for (int i = 0; i < livenessAnalyzer.uniqueNodes; i++)
{
printf("node %i -> $%i\n", livenessAnalyzer.values[i]->id, livenessAnalyzer.values[i]->color);
}
#endif
// assign registers
foreach_list(func->temporaries, Temporary*, iter)
{
Temporary *value = iter.value();
value->reg = livenessAnalyzer.nodeForTemp[value->id]->root()->color;
}
#if DEBUG_RA
// print instruction list again
printf("\n");
func->printInstructionList();
#endif
// give instructions their final indices
int nextIndex = 0;
foreach_list(func->instructionList, Instruction*, iter)
{
Instruction *inst = iter.value();
if (inst->op == OP_BB_START)
inst->block->startIndex = nextIndex;
if (inst->isTrivial())
iter.remove();
else
inst->seqIndex = nextIndex++;
}
// print the final instruction list
#ifdef DEBUG_RA // only print "Final instruction list" if we've already printed non-final instruction lists
printf("\nFinal instruction list:\n");
#endif
foreach_list(func->instructionList, Instruction*, iter)
{
iter.value()->print();
}
printf("\n");
// check for uses of undefined values
foreach_list(func->instructionList, Instruction*, iter)
{
foreach_list(iter.value()->operands, RValue*, rvalue_iter)
{
if (rvalue_iter.value()->isUndefined())
{
printf("Error: possible use of undefined value\n");
return false;
}
}
}
return true;
}
bool link(SSABuilder *func, List<ExecFunction*> *localFunctions, List<Interpreter*> *imports)
{
foreach_list(func->instructionList, Instruction*, iter)
{
Instruction *inst = iter.value();
if (inst->op != OP_CALL) continue;
FunctionCall *call = inst->asFunctionCall();
if (localFunctions->findByName(call->functionName))
{
call->functionRef = localFunctions->retrieve();
//printf("linked call to %s\n", call->functionName);
}
else if ((call->functionRef = ImportList_GetFunctionPointer(imports, call->functionName)))
{
//printf("linked call to %s (imported)\n", call->functionName);
}
else
{
int builtin = getBuiltinIndex(call->functionName);
if (builtin >= 0)
{
call->op = OP_CALL_BUILTIN;
call->builtinRef = builtin;
//printf("linked call to builtin %s\n", call->functionName);
}
else
{
printf("Error: couldn't link %s\n", call->functionName);
return false;
}
}
}
return true;
}
void linkConstants(SSABuilder *func, List<ScriptVariant*> *constants)
{
foreach_list(func->instructionList, Instruction*, instIter)
{
Instruction *inst = instIter.value();
foreach_list(inst->operands, RValue*, srcIter)
{
if (!srcIter.value()->isConstant()) continue;
Constant *c = srcIter.value()->asConstant();
int i = 0;
// try to use an existing constant
foreach_plist(constants, ScriptVariant*, constIter)
{
if (c->constValue.vt == constIter.value()->vt &&
ScriptVariant_IsEqual(&c->constValue, constIter.value()))
{
c->id = i;
break;
}
i++;
}
// constant doesn't exist yet; add it to the list
if (c->id < 0)
{
assert(i == constants->size());
constants->gotoLast();
constants->insertAfter(&c->constValue);
c->id = i;
if (c->constValue.vt == VT_STR)
{
StrCache_Ref(c->constValue.strVal);
}
}
}
}
// get the string cache to free unused string constants
foreach_list(func->constantList, Constant*, constIter)
{
ScriptVariant *var = &constIter.value()->constValue;
if (var->vt == VT_STR)
{
StrCache_Unref(var->strVal);
}
}
}
/**
* Loads and compiles a script file.
*/
Interpreter *compileFile(const char *filename)
{
char *scriptText = readScript(filename);
if (!scriptText) return NULL;
ExecBuilder execBuilder(filename);
pp_context ppContext;
List<Interpreter*> imports;
int numImports;
Parser parser(&ppContext, &execBuilder, scriptText, 1, filename);
parser.parseText();
if (parser.errorFound()) goto error;
execBuilder.allocateExecFunctions();
compiledScripts.gotoLast();
compiledScripts.insertAfter(execBuilder.interpreter, filename);
// get imports
numImports = ppContext.imports.size();
ppContext.imports.gotoFirst();
for (int i = 0; i < numImports; i++)
{
Interpreter *importedScript = ImportCache_ImportFile(ppContext.imports.getName());
if (importedScript == NULL)
{
goto error;
}
printf("imported script %s => %p\n", ppContext.imports.getName(), importedScript);
imports.insertAfter(importedScript);
ppContext.imports.gotoNext();
}
ppContext.clear();
foreach_list(execBuilder.ssaFunctions, SSABuilder*, iter)
{
SSABuilder *func = iter.value();
if (!link(func, &execBuilder.interpreter->functions, &imports)) goto error;
if (!compile(func)) goto error;
linkConstants(func, &execBuilder.constants);
}
execBuilder.buildExecutable();
ralloc_free(parser.memCtx);
free(scriptText);
#if DEBUG_EXEC_BUILDER
execBuilder.printInstructions();
#endif
return execBuilder.interpreter;
error:
printf("Failed to compile script '%s'.\n", filename);
delete execBuilder.interpreter;
if (compiledScripts.findByName(filename))
{
compiledScripts.remove();
}
ralloc_free(parser.memCtx);
free(scriptText);
return NULL;
}
/**
* From a list of interpreters, finds and returns a pointer to the function with
* the specified name in the list. Script files at the *end* of the import list
* have priority, so if the author imports two script files with the same
* function name, the function used will be from the file imported last.
*/
ExecFunction *ImportList_GetFunctionPointer(List<Interpreter*> *list, const char *name)
{
list->gotoLast();
for (int i = list->size(); i > 0; i--)
{
Interpreter *node = list->retrieve();
ExecFunction *func = node->getFunctionNamed(name);
if(func != NULL)
{
#ifdef IC_DEBUG
fprintf(stderr, "importing '%s' from '%s'\n", name, list->getName());
#endif
return func;
}
list->gotoPrevious();
}
return NULL;
}
/**
* Initializes the import cache.
*/
void ImportCache_Init()
{
ImportCache_Clear();
}
/**
* Returns a pointer to the interpreter for the specified script.
*
* If the file has already been imported, this function returns the pointer to
* the existing node. If it hasn't, it imports it and then returns the pointer.
*
* Returns NULL if the file hasn't already been imported and an error occurs
* when compiling it.
*/
Interpreter *ImportCache_ImportFile(const char *path)
{
int i;
char path2[256];
// first convert the path to standard form, lowercase with forward slashes
assert(strlen(path) <= 255);
for(i = strlen(path); i >= 0; i--)
{
if(path[i] == '\\')
{
path2[i] = '/';
}
else if(path[i] >= 'A' && path[i] <= 'Z')
{
path2[i] = path[i] + ('a' - 'A');
}
else
{
path2[i] = path[i];
}
}
#ifdef IC_DEBUG
fprintf(stderr, "ImportCache_ImportFile: '%s' -> '%s'\n", path, path2);
#endif
// find and return interpreter if this file has already been imported
if (compiledScripts.findByName(path2))
return compiledScripts.retrieve();
// otherwise, compile the script and return the newly created interpreter
else
return compileFile(path2);
}
/**
* Frees all of the imported scripts. Called when shutting down the script
* engine.
*/
void ImportCache_Clear()
{
foreach_list(compiledScripts, Interpreter*, iter)
{
delete iter.value();
}
compiledScripts.clear();
}