-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
410 lines (348 loc) · 9.49 KB
/
main.c
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
//
// Copyright (c) 2007 Tridium, Inc.
// Licensed under the Academic Free License version 3.0
//
// History:
// 3 Mar 07 Brian Frank Creation
//
#include<stdio.h>
#include<sys/types.h>
#include "./svm/sedona.h"
#include "./platform/sedonaPlatform.h"
#ifdef USE_STANDARD_MAIN
// includes
#include <stdio.h>
#include <stdlib.h>
#include "svm/errorcodes.h"
//needed to pass version info from gcc command line as a string value (only needed for QNX)
#define VER1(x) #x
#define VER(x) VER1(x)
#define VER VER_(PLAT_BUILD_VERSION)
// sys::Sys forward
int64_t sys_Sys_ticks(SedonaVM* vm, Cell* params);
void sys_Sys_sleep(SedonaVM* vm, Cell* params);
int64_t yieldNs = 0;
// forwards
static int printUsage(const char* exe);
static int printVersion();
static int loadFile(const char* filename, uint8_t** addr, size_t* size);
static void checkStaged(const char* filename);
static void onAssertFailure(const char* location, uint16_t linenum);
static int commonVmSetup(SedonaVM* vm, const char* scodeFile);
static int runInPlatformMode();
static int runInStandaloneMode(const char* filename, int vmArgc, char* vmArgv[]);
// auto-generated by sedonac in "nativetable.c"
extern NativeMethod* nativeTable[]; // map to natives
////////////////////////////////////////////////////////////////
// Main
////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
int i;
bool runAsPlatform = FALSE;
char* filename = NULL;
int optCount = 0;
// parse arguments
for (i=1; i<argc; ++i)
{
char* arg = argv[i];
if (arg[0] == '-')
{
if (strcmp(arg, "--?") == 0) return printUsage(argv[0]);
if (strcmp(arg, "--ver") == 0) return printVersion();
if (strcmp(arg, "--plat") == 0) runAsPlatform = TRUE;
else if (strncmp(arg, "--home=", 7) == 0)
{
char* home = arg+7;
if (strlen(arg) < 8) return printUsage(argv[0]);
if (chdir(home) != 0) return printUsage(argv[0]);
optCount++;
}
}
else
{
if (filename == NULL)
filename = arg;
}
}
printVersion();
if (runAsPlatform)
{
return runInPlatformMode();
}
else
{
if (filename == NULL)
{
printUsage(argv[0]);
return ERR_INPUT_FILE_NOT_FOUND;
}
return runInStandaloneMode(filename, argc - (2 + optCount), argv + (2+optCount));
}
}
////////////////////////////////////////////////////////////////
// Platform VM
////////////////////////////////////////////////////////////////
static int runInPlatformMode()
{
int result = 0;
const char* scode = "kits.scode";
const char* app = "app.sab";
SedonaVM vm;
bool quit = FALSE;
printf("Running SVM in Platform Mode\n");
do
{
checkStaged(scode);
checkStaged(app);
if ((result = commonVmSetup(&vm, scode)) != 0)
return result;
vm.args = (const char**)&app;
vm.argsLen = 1;
result = vmRun(&vm);
while ((result == ERR_HIBERNATE) || (result == ERR_YIELD))
{
// Simulate hibernate and yield
if (result == ERR_HIBERNATE)
printf("-- Simulated hibernate --\n");
else
{
sys_Sys_sleep(NULL, (Cell*)&yieldNs);
yieldNs = 0;
}
result = vmResume(&vm);
}
if (result != 0)
{
if (result == ERR_RESTART)
printf("Restarting VM\n\n");
else
{
printf("Cannot run VM (%d)\n", result);
quit = TRUE;
}
}
else
{
printf("Quitting\n");
quit = TRUE;
}
} while (!quit);
return result;
}
////////////////////////////////////////////////////////////////
// Standalone VM
////////////////////////////////////////////////////////////////
static int runInStandaloneMode(const char* filename, int vmArgc, char* vmArgv[])
{
int result;
SedonaVM vm;
int64_t t1, t2;
if ((result = commonVmSetup(&vm, filename)) != 0)
return result;
// setup arguments
vm.args = (const char**)(vmArgv);
vm.argsLen = vmArgc;
// run the VM
t1 = sys_Sys_ticks(NULL, NULL);
result = vmRun(&vm);
while ((result == ERR_HIBERNATE) || (result == ERR_YIELD))
{
// Simulate hibernate and yield
if (result == ERR_HIBERNATE)
{
printf("-- Simulated hibernate --\n");
}
else
{
printf("Simulate yield %I64d ", yieldNs);
sys_Sys_sleep(NULL, (Cell*)&yieldNs);
yieldNs = 0;
}
result = vmResume(&vm);
}
// done
if (result != 0)
{
printf("Cannot run VM (%d)\n", result);
return result;
}
t2 = sys_Sys_ticks(NULL, NULL);
printf("\n");
printf("VM Completed\n");
#ifdef _WIN32
printf("Total Time = %I64d ms\n", (t2-t1)/1000000ll);
#else
printf("Total Time = %lld ms\n", (t2-t1)/1000000ll);
#endif
printf("Assert Successes = %d\n", vm.assertSuccesses);
printf("Assert Failures = %d\n", vm.assertFailures);
printf("\n");
if (vm.assertFailures == 0)
{
printf("--\n");
printf("-- All tests passed\n");
printf("--\n");
return 0;
}
else
{
printf("--\n");
printf("-- %d TESTS FAILED!!!\n", vm.assertFailures);
printf("--\n");
return vm.assertFailures;
}
return result;
}
////////////////////////////////////////////////////////////////
// Common VM Setup
////////////////////////////////////////////////////////////////
/**
*
* This function initialize the following fields of the VM.
* 1) stackMaxSize
* 2) stackBaseAddr
* 3) onAssertFailure callback
* 4) nativeTable
* 5) call function pointer
*
* The following fields will still need to be initialized before
* the VM can be run.
* 1) args
* 2) argsLen
*/
static int commonVmSetup(SedonaVM* vm, const char* scodeFile)
{
int result;
// load scode
result = loadFile(scodeFile, (uint8_t**)&(vm->codeBaseAddr), &(vm->codeSize));
if (result != 0)
{
printf("Cannot load input file (%d): %s\n", result, scodeFile);
return result;
}
// alloc stack (hardcoded for now)
vm->stackMaxSize = 16384;
vm->stackBaseAddr = (uint8_t*)malloc(vm->stackMaxSize);
if (vm->stackBaseAddr == NULL)
{
printf("Cannot malloc stack segments\n");
return ERR_MALLOC_STACK;
}
// setup callbacks
vm->onAssertFailure = onAssertFailure;
// setup native method table
vm->nativeTable = nativeTable;
// setup call function pointer
#ifdef VM_DEBUG_MODE
vm->call = vmCallDebug;
#else
vm->call = vmCall;
#endif
return 0;
}
////////////////////////////////////////////////////////////////
// Print Usage
////////////////////////////////////////////////////////////////
static int printUsage(const char* exe)
{
printf("usage:\n");
printf(" %s [options] <scode file> [<sab file>] [<Sedona main args>]\n", exe);
printf(" %s [options] --plat\n", exe);
printf("options:\n");
printf(" --? dump usage\n");
printf(" --ver dump version\n");
printf(" --home=d set current working directory\n");
printf(" --plat run in platform mode. 'kits.scode[.stage]' and 'app.sab[.stage]'\n");
printf(" must be present in the working directory\n");
return 0;
}
////////////////////////////////////////////////////////////////
// Print Version
////////////////////////////////////////////////////////////////
#ifndef PLAT_BUILD_VERSION
#error Must set PLAT_BUILD_VERSION
#endif
static int printVersion()
{
#ifdef IS_BIG_ENDIAN
const char* endian = "big";
#else
const char* endian = "little";
#endif
printf("\n");
#ifdef __QNX__
printf("Sedona VM %s\n", VER(PLAT_BUILD_VERSION));
#else
printf("Sedona VM %s\n", PLAT_BUILD_VERSION);
#endif
printf("platformId: %s\n", PLATFORM_ID);
printf("buildDate: %s %s\n", __DATE__, __TIME__);
printf("endian: %s\n", endian);
printf("blockSize supported: 4/16/32/64/128\n");
printf("refSize: %d\n", sizeof(void*));
printf("\n");
return 0;
}
////////////////////////////////////////////////////////////////
// Load File
////////////////////////////////////////////////////////////////
static int loadFile(const char* filename, uint8_t** paddr, size_t* psize)
{
size_t result;
FILE* file;
size_t size;
uint8_t* addr;
// open file
file = fopen(filename, "rb");
if (file == NULL) return ERR_INPUT_FILE_NOT_FOUND;
// seek to end to get file size
result = fseek(file, 0, SEEK_END);
if (result != 0) return ERR_CANNOT_READ_INPUT_FILE;
size = ftell(file);
rewind(file);
// allocate memory for image
addr = (uint8_t*)malloc(size);
if (addr == NULL)
return ERR_MALLOC_IMAGE;
// read file into memory
result = fread(addr, 1, size, file);
if (result != size) return ERR_CANNOT_READ_INPUT_FILE;
// success
*paddr = addr;
*psize = size;
fclose(file);
return 0;
}
//////////////////////////////////////////////////////////////////////////
// Staged Files
//////////////////////////////////////////////////////////////////////////
/**
* Checks if <filename>.stage exists. If so it is renamed to <filename>.
*/
static void checkStaged(const char* filename)
{
FILE* file;
size_t len;
char* stageName;
len = strlen(filename);
stageName = (char*)malloc(len + strlen(".stage") + 1);
strcpy(stageName, filename);
strcat(stageName, ".stage");
if ((file = fopen(stageName, "rb")) != NULL)
{
fclose(file);
printf("Moving %s to %s\n", stageName, filename);
remove(filename);
rename(stageName, filename);
}
free(stageName);
}
//////////////////////////////////////////////////////////////////////////
// VM Callbacks
//////////////////////////////////////////////////////////////////////////
static void onAssertFailure(const char* location, uint16_t linenum)
{
printf("ASSERT FAILURE: %s [Line %d]\n", location, linenum);
}
#endif