forked from emigonza/JwiK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvmfile.c
422 lines (390 loc) · 10.9 KB
/
vmfile.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
411
412
413
414
415
416
417
418
419
420
421
422
// Java VM for small microcontrollers
//
// (c) 2012, Digital Six Laboratories LLC
// All Rights Reserved
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial
// portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Original work based on NanoVM (http://www.harbaum.org/till/nanovm/index.shtml) for AVR microcontroller
//=========================================================================================================
//
// vmfile.c
//
// Virtual machine file management API. The file is stored in flash and this module provides access to and
// from the file.
#include "vmtypes.h"
//#include "config.h"
#include "vmerror.h"
#include "vmfile.h"
#include "vm.h"
//#include "eeprom.h"
#include "vmfeatures.h"
#include "vmheap.h"
#include "vmstack.h"
static uint8_t vmFile[CODESIZE];
//#include "vmDefaultApp.h"
uint8_t vmFileConstantCount;
// *****************************************************************************************************************8
// Internal Functions
uint16_t CopyCodeToRAM(uint8_t *dst, uint8_t *src, uint16_t len)
{
uint16_t i=0;
for(i=0;i<len;i++)
{
*(dst+i) = FlashReadByte(src+i);
}
return i;
}
// *****************************************************************************************************************8
// Public API
// void VmFileRead(void *dst, void *src, uint16_t len)
//
// Read 'len' bytes from 'src' to 'dst'
//
// Args:
// void *dst - pointer to buffer that is the target of the copy
// void *src - pointer to the source bytes to copy
// uint16_t len - number of bytes to copy
// Returns:
// Nothing
void VmFileRead(void *dst, void *src, uint16_t len)
{
src = VMFILLADDR(src); // remove marker (if present)
CopyCodeToRAM((uint8_t *)dst, (PGMPTR)src, len);
}
// void VmFileRead08(void *addr)
//
// Read a byte from 'addr'
//
// Args:
// void *addr - address to read the byte from
// Returns:
// nothing
uint8_t VmFileRead08(void *addr)
{
uint8_t val;
addr = VMFILLADDR(addr); // remove marker (if present)
CopyCodeToRAM((uint8_t*)&val, (PGMPTR)addr, 1);
return val;
}
// void VmFileRead16(void *addr)
//
// Read a int from 'addr'
//
// Args:
// void *addr - address to read the int from
// Returns:
// nothing
uint16_t VmFileRead16(void *addr)
{
uint16_t val;
addr = VMFILLADDR(addr); // remove marker (if present)
CopyCodeToRAM((uint8_t*)&val, (PGMPTR)addr, 2);
return val;
}
// void VmFileRead32(void *addr)
//
// Read a dword from 'addr'
//
// Args:
// void *addr - address to read the dword from
// Returns:
// nothing
uint32_t VmFileRead32(void *addr)
{
uint32_t val;
addr = VMFILLADDR(addr); // remove marker (if present)
CopyCodeToRAM((uint8_t*)&val, (PGMPTR)addr, 4);
return val;
}
// void VmWrite08(void *addr)
//
// Write a byte to'addr'
//
// Args:
// void *addr - address to write byte to
// Returns:
// nothing
void VmFileWrite08(void *addr, uint8_t dataToWrite)
{
// TODO: write VmFileWrite08
}
// void VmFileInitializeWrite(void)
//
// Initialize the write process. This is where you would unlock flash or whatever you
// have to do at the beginning of a file write.
//
// Args:
// Returns:
// nothing
void VmFileInitializeWrite(void)
{
// TODO: write VmFileInitializeWrite
}
// void VmFileFinalizeWrite(void)
//
// Finalize the write process. This is where you would lock flash or whatever you
// have to do at the end of a file write.
//
// Args:
// Returns:
// nothing
void VmFileFinalizeWrite(void)
{
// TODO: write VmFileFinalizeWrite
}
// void VmFileInitialize(void)
//
// Initialize the VM File.
//
// Args:
// Returns:
// True if everything is OK, false if there was a problem
BOOL VmFileInitialize(void)
{
uint16_t t;
uint32_t features = VmFileRead32(&((tVmFileHeader*)vmFile)->magic_feature);
if ((features&NVM_MAGIC_FEAUTURE)!=(features|NVMFILE_MAGIC))
{
HandleError(kErrorVmFileBadJuJu);
return FALSE;
}
if(VmFileRead08(&((tVmFileHeader*)vmFile)->version) != NVMFILE_VERSION)
{
HandleError(kErrorVmFileBadVersion);
return FALSE;
}
t = VmFileRead16(&((tVmFileHeader*)vmFile)->string_offset);
t -= VmFileRead16(&((tVmFileHeader*)vmFile)->constant_offset);
vmFileConstantCount = t/4;
return TRUE;
}
// void VmFileSaveApp(void)
//
// Save a VM application file to flash
//
// Args:
// u16 index -
// uint8_t *buffer - pointer to buffer containing app
// uint16_t size - number of bytes to copy
// Returns:
// nothing
void VmFileSaveApp(uint16_t index, uint8_t *buffer, uint16_t size)
{
// TODO: Write VmFileSaveApp
}
// void VmFileGetMethodHeader(uint16_t index)
//
// Gets a method header for the method at 'index'
//
// Args:
// uint16_t index - method index
// Returns:
// nothing
tVmFileMethodHeader *VmFileGetMethodHeader(uint16_t index)
{
uint16_t methodOffset;
// get pointer to method header
tVmFileMethodHeader *hdrs = ((tVmFileMethodHeader *)(vmFile+methodOffset))
+ index;
methodOffset = VmFileRead16(&((tVmFileHeader*)vmFile)->method_offset);
hdrs = ((tVmFileMethodHeader *)(vmFile+methodOffset))
+ index;
return(hdrs);
}
// void VmFileGetMethodHeader(uint8_t index)
//
// Gets a constant for the method at 'index'
//
// Args:
// uint8_t index - constant index
// Returns:
// nothing
uint32_t VmFileGetConstant(uint8_t index)
{
tVmReference res;
uint16_t addr;
uint32_t result;
if (index<vmFileConstantCount)
{
addr = VmFileRead16(&((tVmFileHeader*)vmFile)->constant_offset);
result = VmFileRead32(vmFile+addr+4*index);
return result;
}
// it's a string!
res = VM_TYPE_CONST | (index-vmFileConstantCount);
return res;
}
// void VmFileJumpToMain(void)
//
// Jump to the main method in the application
//
// Args:
// nothing
// Returns:
// nothing
void VmFileJumpToMain(void)
{
uint8_t i;
uint8_t flags;
tVmFileHeader *nvh;
uint16_t mn;
uint16_t main;
uint8_t methodCount=VmFileRead08(&((tVmFileHeader*)vmFile)->methods);
uint16_t code_index;
uint16_t id;
uint8_t args;
uint8_t max_locals;
uint8_t max_stack;
tVmFileMethodHeader *mainMethodHeader;
uint16_t mainClassId;
tHeapFrame *heap1;
tHeapFrame *heap2;
tHeapId heapId;
// todo:
//
// This is all wrong. To properly handle the default class, it must exist on the heap
// so that the stack frame is set up properly and so that there is storage space for local
// fields. This will be a special case because we want to start with an empty stack, whereas
// new will put the heapId (which is now our reference) on the stack.
//
nvh=(tVmFileHeader *)vmFile;
mn=(nvh)->main;
main = VmFileRead16(&(nvh)->main);
mainMethodHeader = (tVmFileMethodHeader *)VmFileGetMethodHeader(main);
mainClassId = VmFileRead16(&(mainMethodHeader)->id);
heapId=VmNewClass(mainClassId);
for(i=0;i<methodCount;i++)
{
tVmFileMethodHeader *mhdr = (tVmFileMethodHeader *)VmFileGetMethodHeader(i);
code_index = VmFileRead16(&(mhdr)->code_index);
id = VmFileRead16(&(mhdr)->id);
flags = VmFileRead08(&(mhdr)->flags) ;
args = VmFileRead08(&(mhdr)->args);
max_locals = VmFileRead08(&(mhdr)->max_locals);
max_stack = VmFileRead08(&(mhdr)->max_stack);
// is this a clinit method?
if(flags& FLAG_CLINIT)
{
// Make sure we are running the initialization for the same class that holds the
// Main() method.
if( NATIVE_ID2CLASS(mainClassId) == NATIVE_ID2CLASS(id) )
VmRunMethod(i);
}
}
heap1 = HeapSearch(1);
heap2 = HeapSearch(2);
StackPush(heapId);
// determine method description address and code
VmRunMethod(main);
}
// void VmFileGetAddress(uint16_t ref)
//
// Gets an address for a string at 'index'
//
// Args:
// uint16_t ref - method index
// Returns:
// nothing
void *VmFileGetAddress(uint16_t ref)
{
// get pointer to string
uint16_t *refs = (uint16_t *)(vmFile + VmFileRead16(&((tVmFileHeader*)vmFile)->string_offset));
return((uint8_t*)refs + VmFileRead16(refs+ref));
}
// void VmFileGetClassFieldCount(uint8_t index)
//
// Gets a count of the fields in a class for the class at 'index'
//
// Args:
// uint8_t index - class index
// Returns:
// nothing
uint8_t VmFileGetClassFieldCount(uint8_t index)
{
uint8_t retVal;
tVmFileHeader *vmfh;
void *addr;
tVmFileClassHeader *ch;
// fixed the pointer math.
vmfh = (tVmFileHeader*)vmFile;
ch = &vmfh->class_hdr;
addr = &(ch[index].fields);
retVal = VmFileRead08(addr);
return retVal;
}
// uint8_t VmFileGetStaticFieldCount(void)
//
// Gets a method header for the method at 'index'
//
// Args:
// Nothing
// Returns:
// Count of static fields in application
uint8_t VmFileGetStaticFieldCount(void)
{
return VmFileRead08(&((tVmFileHeader*)vmFile)->static_fields);
}
#ifdef VM_INCLUDE_INHERITANCE
// uint8_t VmFileGetMethodByFixedClassAndId(uint8_t classId, uint8_t id)
//
// Gets a method by class id and method id
//
// Args:
// uint8_t classId - class id
// uint8_t id - method id
// Returns:
// uint8_t containing method number
uint8_t VmFileGetMethodByFixedClassAndId(uint8_t classId, uint8_t id)
{
uint8_t i;
tVmFileMethodHeader mhdr, *mhdr_ptr;
for(i=0;i<VmFileRead08(&((tVmFileHeader*)vmFile)->methods);i++)
{
// load new method header into ram
mhdr_ptr = VmFileGetMethodHeader(i);
VmFileRead(&mhdr, mhdr_ptr, sizeof(tVmFileMethodHeader));
if(((mhdr.id >> 8) == classId) && ((mhdr.id & 0xff) == id))
{
// we found our guy, so return it.
return i;
}
}
// return 0xff if the classId and id are not found.
return 0xff;
}
// uint8_t VmFileGetMethodByClassAndId(uint8_t classId, uint8_t id)
//
// Gets a method by class id and method id
//
// Args:
// uint8_t classId - class id
// uint8_t id - method id
// Returns:
// uint8_t containing method number
// TODO: Rewrite this. It will loop endlessly if the result is not found. THIS IS A VERY BAD DESIGN
uint8_t VmFileGetMethodByClassAndId(uint8_t classId, uint8_t id)
{
uint8_t mref;
for(;;)
{
if((mref = VmFileGetMethodByFixedClassAndId(classId, id)) != 0xff)
return mref;
classId = VmFileRead08(&((tVmFileHeader*)vmFile)->class_hdr[classId].super);
}
return 0;
}
#endif