forked from emigonza/JwiK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmarray.c
210 lines (192 loc) · 5.63 KB
/
vmarray.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
// 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
//=========================================================================================================
//
// array.c
//
// virtual machine array management
//#include "vmtypes.h"
//#include "config.h"
#include "vmerror.h"
#include "vm.h"
#include "vmarray.h"
#include "vmheap.h"
#ifdef VM_INCLUDE_ARRAY
// ***************************************************************************************************************************
// Internal functions
uint8_t ArrayTypeLen(ArrayElementTypes elementType)
{
uint8_t len = 0;
switch(elementType)
{
case kBoolean:
case kChar:
case kByte:
len = sizeof(tVmByte);
break;
case kShort:
len = sizeof(tVmShort);
break;
case kInt:
len = sizeof(tVmInt);
break;
default:
HandleError(kErrorArrayIllegalType);
}
return len;
// TODO: delete this code once the new switch statement is tested
/*
if((type == T_BOOLEAN)||(type == T_CHAR)||(type == T_BYTE))
return sizeof(tVmByte);
if(type == T_SHORT)
return sizeof(tVmShort);
if(type == T_INT)
return sizeof(tVmInt);
HandleError(kErrorArrayIllegalType);
return 0; // to make compiler happy
*/
}
// ****************************************************************************************************************************
// Public API
// ****************************************************************************************************************************
// tHeapId ArrayNew(tVmInt length, uint8_t type)
//
// Create a new array.
//
// Args:
// tVmInt length - number of elements in array
// uint8_t type - type of array elements
// Results:
// tHeapId - id of the array
tHeapId ArrayNew(tVmInt length, uint8_t type)
{
tHeapId id;
id = HeapAlloc(FALSE, 1 + length * ArrayTypeLen(type));
// store type in first byte
*(uint8_t*)(HeapGetAddress(id)) = type;
return id;
}
// tVmInt ArrayLen(tHeapId id)
//
// Gets the length of an array by its ID
//
// Args:
// tHeapId id - id of array
// Returns:
// tVmInt - length of the array as an integer
tVmInt ArrayLen(tHeapId id)
{
return(HeapGetLength(id)/
ArrayTypeLen(*(uint8_t*)HeapGetAddress(id)));
}
// void ArraySetByteValue(tHeapId id, tVmInt index, tVmByte value)
//
// Store a value in the array
//
// Args:
// tHeapId id - id of array
// tVmInt index - index of target element
// tVmByte value - value to store
// Returns:
// nothing
void ArraySetByteValue(tHeapId id, tVmInt index, tVmByte value)
{
tVmByte * ptr = (tVmByte *)HeapGetAddress(id) + 1;
ptr[index] = value;
}
// void ArrayGetByteValue(tHeapId id, tVmInt index)
//
// Gets a byte from an array
//
// Args:
// tHeapId id - id of array
// tVmInt index - index of value to get
// Returns:
// tVmByte value of array[index]
tVmByte ArrayGetByteValue(tHeapId id, tVmInt index)
{
tVmByte * ptr = (tVmByte *)HeapGetAddress(id) + 1;
return ptr[index];
}
// void ArraySetIntValue(tHeapId id, tVmInt index, tVmInt value)
//
// Store a value in the array
//
// Args:
// tHeapId id - id of array
// tVmInt index - index of target element
// tVmInt value - value to store
// Returns:
// nothing
void ArraySetIntValue(tHeapId id, tVmInt index, tVmInt value)
{
tVmInt * ptr = (tVmInt *)((uint8_t*)HeapGetAddress(id) + 1);
ptr[index] = value;
HeapCheck();
}
// void ArrayGetIntValue(tHeapId id, tVmInt index)
//
// Gets a int from an array
//
// Args:
// tHeapId id - id of array
// tVmInt index - index of value to get
// Returns:
// tVmInt value of array[index]
tVmInt ArrayGetIntValue(tHeapId id, tVmInt index)
{
tVmInt * ptr = (tVmInt *)((uint8_t*)HeapGetAddress(id) + 1);
return ptr[index];
}
#ifdef VM_INCLUDE_FLOAT
// void ArraySetFloatValue(tHeapId id, tVmInt index, tVmFloat value)
//
// Store a value in the array
//
// Args:
// tHeapId id - id of array
// tVmInt index - index of target element
// tVmFloat value - value to store
// Returns:
// nothing
void ArraySetFloatValue(tHeapId id, tVmInt index, tVmFloat value)
{
tVmFloat * ptr = (tVmFloat *)((uint8_t*)HeapGetAddress(id) + 1);
ptr[index] = value;
HeapCheck();
}
// void ArrayGetFloatValue(tHeapId id, tVmInt index)
//
// Gets a Float from an array
//
// Args:
// tHeapId id - id of array
// tVmInt index - index of value to get
// Returns:
// tVmFloat value of array[index]
tVmFloat ArrayGetFloatValue(tHeapId id, tVmInt index)
{
tVmFloat * ptr = (tVmFloat *)((uint8_t*)HeapGetAddress(id) + 1);
return ptr[index];
}
#endif
#endif