-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.c
106 lines (85 loc) · 3.17 KB
/
array.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
//
// NanoVM, a tiny java VM for the Atmel AVR family
// Copyright (C) 2005 by Till Harbaum <[email protected]>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#include <stdio.h>
#include "config.h"
#include "error.h"
#include "nvmtypes.h"
#include "vm.h"
#include "array.h"
#include "heap.h"
uint8_t array_typelen(uint8_t type) {
if((type == T_BOOLEAN)||(type == T_CHAR)||(type == T_BYTE))
return sizeof(nvm_byte_t);
if(type == T_SHORT)
return sizeof(nvm_short_t);
if(type == T_INT)
return sizeof(nvm_int_t);
if(type == T_FLOAT)
return sizeof(nvm_float_t);
error(ERROR_ARRAY_ILLEGAL_TYPE);
return 0; // to make compiler happy
}
heap_id_t array_new(nvm_int_t length, uint8_t type) {
heap_id_t id;
DEBUGF("newarray type %d len = %ld: total size = %ld bytes\n",
type, length, length * array_typelen(type));
id = heap_alloc(FALSE, 1 + length * array_typelen(type));
// store type in first byte
*(uint8_t*)(heap_get_addr(id)) = type;
return id;
}
nvm_int_t array_length(heap_id_t id) {
DEBUGF("arraylength %d = %d/%d\n", id,
heap_get_len(id)-1,
array_typelen(*(uint8_t*)heap_get_addr(id)));
return((heap_get_len(id)-1)/
array_typelen(*(uint8_t*)heap_get_addr(id)));
}
void array_bastore(heap_id_t id, nvm_int_t index, nvm_byte_t value) {
nvm_byte_t * ptr = (nvm_byte_t *)heap_get_addr(id) + 1;
DEBUGF("bastore id=%x, index=%ld, value=%d\n", id, index, value);
ptr[index] = value;
}
nvm_byte_t array_baload(heap_id_t id, nvm_int_t index) {
nvm_byte_t * ptr = (nvm_byte_t*)heap_get_addr(id) + 1;
DEBUGF("baload id=%x, index=%ld\n", id, index);
return ptr[index];
}
void array_iastore(heap_id_t id, nvm_int_t index, nvm_int_t value) {
nvm_int_t * ptr = (nvm_int_t *)((uint8_t*)heap_get_addr(id) + 1);
DEBUGF("iastore id=%x, index=%ld, value=%ld\n", id, index, value);
ptr[index] = value;
HEAP_CHECK();
}
nvm_int_t array_iaload(heap_id_t id, nvm_int_t index) {
nvm_int_t * ptr = (nvm_int_t *)((uint8_t*)heap_get_addr(id) + 1);
DEBUGF("iaload id=%x, index=%ld\n", id, index);
return ptr[index];
}
void array_fastore(heap_id_t id, nvm_int_t index, nvm_float_t value) {
nvm_float_t * ptr = (nvm_float_t*)((uint8_t*)heap_get_addr(id) + 1);
DEBUGF("iastore id=%x, index=%ld, value=%f\n", id, index, (double)value);
ptr[index] = value;
HEAP_CHECK();
}
nvm_float_t array_faload(heap_id_t id, nvm_int_t index) {
nvm_float_t * ptr = (nvm_float_t*)((uint8_t*)heap_get_addr(id) + 1);
DEBUGF("iaload id=%x, index=%ld\n", id, index);
return ptr[index];
}