|
| 1 | +// This file is part of SmallBASIC |
| 2 | +// |
| 3 | +// inlined hotspots |
| 4 | +// |
| 5 | +// This program is distributed under the terms of the GPL v2.0 or later |
| 6 | +// Download the GNU Public License (GPL) from www.gnu.org |
| 7 | +// |
| 8 | +// Copyright(C) 2014 Chris Warren-Smith |
| 9 | + |
| 10 | +#include "common/var_uds.h" |
| 11 | +#include "common/var_hash.h" |
| 12 | + |
| 13 | +void err_evsyntax(void); |
| 14 | +void err_varisarray(void); |
| 15 | +void err_varisnotarray(void); |
| 16 | +void err_notavar(void); |
| 17 | +var_t *code_resolve_varptr(var_t* var_p, int until_parens); |
| 18 | + |
| 19 | +/** |
| 20 | + * @ingroup var |
| 21 | + * |
| 22 | + * returns the next integer and moves the IP 4 bytes forward. |
| 23 | + * |
| 24 | + * R(long int) <- Code[IP]; IP+=4 |
| 25 | + */ |
| 26 | +static inline dword code_getnext32(void) { |
| 27 | + dword v; |
| 28 | + memcpy(&v, prog_source + prog_ip, 4); |
| 29 | + prog_ip += 4; |
| 30 | + return v; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * @ingroup var |
| 35 | + * |
| 36 | + * returns the next 64bit and moves the instruction pointer to the next instruction |
| 37 | + * |
| 38 | + * R(double) <- Code[IP]; IP+=8 |
| 39 | + */ |
| 40 | +static inline double code_getnext64f() { |
| 41 | + double v; |
| 42 | + memcpy(&v, prog_source + prog_ip, sizeof(double)); |
| 43 | + prog_ip += sizeof(double); |
| 44 | + return v; |
| 45 | +} |
| 46 | + |
| 47 | +#if defined(OS_PREC64) |
| 48 | + |
| 49 | +/** |
| 50 | + * @ingroup var |
| 51 | + * |
| 52 | + * returns the next 64bit and moves the instruction pointer to the next instruction |
| 53 | + */ |
| 54 | +static inline var_int_t code_getnext64i() { |
| 55 | + var_int_t v; |
| 56 | + memcpy(&v, prog_source + prog_ip, sizeof(var_int_t)); |
| 57 | + prog_ip += sizeof(var_int_t); |
| 58 | + return v; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * @ingroup var |
| 63 | + * |
| 64 | + * returns the next 128bit and moves the instruction pointer to the next instruction |
| 65 | + */ |
| 66 | +static inline var_num_t code_getnext128f() { |
| 67 | + var_num_t v; |
| 68 | + memcpy(&v, prog_source + prog_ip, sizeof(var_num_t)); |
| 69 | + prog_ip += sizeof(var_num_t); |
| 70 | + return v; |
| 71 | +} |
| 72 | + |
| 73 | +#endif |
| 74 | + |
| 75 | +/** |
| 76 | + * @ingroup var |
| 77 | + * |
| 78 | + * returns the floating-point value of a var. |
| 79 | + * if v is string it will converted to double. |
| 80 | + * |
| 81 | + * @param v the variable |
| 82 | + * @return the numeric value of a variable |
| 83 | + */ |
| 84 | +static inline var_num_t v_getval(var_t *v) { |
| 85 | + switch (v ? v->type : -1) { |
| 86 | + case V_UDS: |
| 87 | + return uds_to_int(v); |
| 88 | + case V_HASH: |
| 89 | + return hash_to_int(v); |
| 90 | + case V_PTR: |
| 91 | + return v->v.ap.p; |
| 92 | + case V_INT: |
| 93 | + return v->v.i; |
| 94 | + case V_NUM: |
| 95 | + return v->v.n; |
| 96 | + case V_STR: |
| 97 | + return numexpr_sb_strtof((char *) v->v.p.ptr); |
| 98 | + default: |
| 99 | + if (v == NULL) { |
| 100 | + err_evsyntax(); |
| 101 | + } else { |
| 102 | + err_varisarray(); |
| 103 | + } |
| 104 | + } |
| 105 | + return 0; |
| 106 | +} |
| 107 | + |
| 108 | +#define v_getnum(a) v_getval((a)) |
| 109 | + |
| 110 | +/** |
| 111 | + * @ingroup var |
| 112 | + * |
| 113 | + * returns the integer value of a var. |
| 114 | + * if v is string it will converted to integer. |
| 115 | + * |
| 116 | + * @param v the variable |
| 117 | + * @return the integer value of a variable |
| 118 | + */ |
| 119 | +static inline var_int_t v_igetval(var_t *v) { |
| 120 | + switch (v ? v->type : -1) { |
| 121 | + case V_UDS: |
| 122 | + return uds_to_int(v); |
| 123 | + case V_HASH: |
| 124 | + return hash_to_int(v); |
| 125 | + case V_PTR: |
| 126 | + return v->v.ap.p; |
| 127 | + case V_INT: |
| 128 | + return v->v.i; |
| 129 | + case V_NUM: |
| 130 | + return v->v.n; |
| 131 | + case V_STR: |
| 132 | + return numexpr_strtol((char *) v->v.p.ptr); |
| 133 | + default: |
| 134 | + if (v == NULL) { |
| 135 | + err_evsyntax(); |
| 136 | + } else { |
| 137 | + err_varisarray(); |
| 138 | + } |
| 139 | + } |
| 140 | + return 0; |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | + * @ingroup exec |
| 145 | + * |
| 146 | + * variant of code_getvarptr() derefence until left parenthesis found |
| 147 | + * |
| 148 | + * R(var_t*) <- Code[IP]; IP += 2; |
| 149 | + * |
| 150 | + * @return the var_t* |
| 151 | + */ |
| 152 | +static inline var_t* code_getvarptr_parens(int until_parens) { |
| 153 | + var_t *var_p = NULL; |
| 154 | + |
| 155 | + switch (code_peek()) { |
| 156 | + case kwTYPE_VAR: |
| 157 | + code_skipnext(); |
| 158 | + var_p = tvar[code_getaddr()]; |
| 159 | + switch (var_p->type) { |
| 160 | + case V_HASH: |
| 161 | + case V_ARRAY: |
| 162 | + var_p = code_resolve_varptr(var_p, until_parens); |
| 163 | + break; |
| 164 | + default: |
| 165 | + if (!until_parens && code_peek() == kwTYPE_LEVEL_BEGIN) { |
| 166 | + err_varisnotarray(); |
| 167 | + } |
| 168 | + } |
| 169 | + break; |
| 170 | + |
| 171 | + case kwTYPE_UDS: |
| 172 | + code_skipnext(); |
| 173 | + var_p = tvar[code_getaddr()]; |
| 174 | + var_p = code_resolve_varptr(uds_resolve_fields(var_p), until_parens); |
| 175 | + break; |
| 176 | + } |
| 177 | + |
| 178 | + if (var_p == NULL && !prog_error) { |
| 179 | + err_notavar(); |
| 180 | + return tvar[0]; |
| 181 | + } |
| 182 | + |
| 183 | + return var_p; |
| 184 | +} |
| 185 | + |
| 186 | +/** |
| 187 | + * @ingroup var |
| 188 | + * |
| 189 | + * Returns the varptr of the next variable. if the variable is an array |
| 190 | + * returns the element ptr |
| 191 | + */ |
| 192 | +#define code_getvarptr() code_getvarptr_parens(0) |
| 193 | + |
0 commit comments