| 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 |
#ifdef __arm__ |
| 34 |
|
| 35 |
#include <sys/types.h> |
| 36 |
#include <sys/mman.h> |
| 37 |
#include <sys/time.h> |
| 38 |
#include <time.h> |
| 39 |
#include <stddef.h> |
| 40 |
|
| 41 |
#include <sys/types.h> |
| 42 |
#include <sys/stat.h> |
| 43 |
#include <fcntl.h> |
| 44 |
|
| 45 |
#include "vm_local.h" |
| 46 |
#define R0 0 |
| 47 |
#define R1 1 |
| 48 |
#define R2 2 |
| 49 |
#define R3 3 |
| 50 |
#define R4 4 |
| 51 |
|
| 52 |
#define R12 12 |
| 53 |
|
| 54 |
#define FP 11 |
| 55 |
#define SP 13 |
| 56 |
#define LR 14 |
| 57 |
#define PC 15 |
| 58 |
|
| 59 |
#define APSR_nzcv 15 |
| 60 |
|
| 61 |
#define S14 14 |
| 62 |
#define S15 15 |
| 63 |
|
| 64 |
#define rOPSTACK 5 |
| 65 |
#define rOPSTACKBASE 6 |
| 66 |
#define rCODEBASE 7 |
| 67 |
#define rPSTACK 8 |
| 68 |
#define rDATABASE 9 |
| 69 |
#define rDATAMASK 10 |
| 70 |
|
| 71 |
#define bit(x) (1<<x) |
| 72 |
|
| 73 |
|
| 74 |
int __aeabi_idiv (int, int); |
| 75 |
unsigned __aeabi_uidiv (unsigned, unsigned); |
| 76 |
void __aeabi_idivmod(void); |
| 77 |
void __aeabi_uidivmod(void); |
| 78 |
|
| 79 |
|
| 80 |
#define DIE( reason, args... ) \ |
| 81 |
do { \ |
| 82 |
Com_Error(ERR_DROP, "vm_arm compiler error: " reason, ##args); \ |
| 83 |
exit(1); \ |
| 84 |
} while(0) |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
#define opImm0 0x0000 |
| 93 |
#define opImm1 0x0001 |
| 94 |
#define opImm4 0x0002 |
| 95 |
|
| 96 |
#define opRet0 0x0000 |
| 97 |
#define opRetI 0x0004 |
| 98 |
#define opRetF 0x0008 |
| 99 |
#define opRetIF (opRetI | opRetF) |
| 100 |
|
| 101 |
#define opArg0 0x0000 |
| 102 |
#define opArgI 0x0010 |
| 103 |
#define opArgF 0x0020 |
| 104 |
#define opArgIF (opArgI | opArgF) |
| 105 |
|
| 106 |
#define opArg2I 0x0040 |
| 107 |
#define opArg2F 0x0080 |
| 108 |
#define opArg2IF (opArg2I | opArg2F) |
| 109 |
|
| 110 |
static const unsigned char vm_opInfo[256] = |
| 111 |
{ |
| 112 |
[OP_UNDEF] = opImm0, |
| 113 |
[OP_IGNORE] = opImm0, |
| 114 |
[OP_BREAK] = opImm0, |
| 115 |
[OP_ENTER] = opImm4, |
| 116 |
|
| 117 |
[OP_LEAVE] = opImm4 | opRet0 | opArgIF, |
| 118 |
|
| 119 |
|
| 120 |
[OP_CALL] = opImm0 | opRetI | opArgI, |
| 121 |
[OP_PUSH] = opImm0 | opRetIF, |
| 122 |
[OP_POP] = opImm0 | opRet0 | opArgIF, |
| 123 |
[OP_CONST] = opImm4 | opRetIF, |
| 124 |
[OP_LOCAL] = opImm4 | opRetI, |
| 125 |
[OP_JUMP] = opImm0 | opRet0 | opArgI, |
| 126 |
|
| 127 |
[OP_EQ] = opImm4 | opRet0 | opArgI | opArg2I, |
| 128 |
[OP_NE] = opImm4 | opRet0 | opArgI | opArg2I, |
| 129 |
[OP_LTI] = opImm4 | opRet0 | opArgI | opArg2I, |
| 130 |
[OP_LEI] = opImm4 | opRet0 | opArgI | opArg2I, |
| 131 |
[OP_GTI] = opImm4 | opRet0 | opArgI | opArg2I, |
| 132 |
[OP_GEI] = opImm4 | opRet0 | opArgI | opArg2I, |
| 133 |
[OP_LTU] = opImm4 | opRet0 | opArgI | opArg2I, |
| 134 |
[OP_LEU] = opImm4 | opRet0 | opArgI | opArg2I, |
| 135 |
[OP_GTU] = opImm4 | opRet0 | opArgI | opArg2I, |
| 136 |
[OP_GEU] = opImm4 | opRet0 | opArgI | opArg2I, |
| 137 |
[OP_EQF] = opImm4 | opRet0 | opArgF | opArg2F, |
| 138 |
[OP_NEF] = opImm4 | opRet0 | opArgF | opArg2F, |
| 139 |
[OP_LTF] = opImm4 | opRet0 | opArgF | opArg2F, |
| 140 |
[OP_LEF] = opImm4 | opRet0 | opArgF | opArg2F, |
| 141 |
[OP_GTF] = opImm4 | opRet0 | opArgF | opArg2F, |
| 142 |
[OP_GEF] = opImm4 | opRet0 | opArgF | opArg2F, |
| 143 |
|
| 144 |
[OP_LOAD1] = opImm0 | opRetI | opArgI, |
| 145 |
[OP_LOAD2] = opImm0 | opRetI | opArgI, |
| 146 |
[OP_LOAD4] = opImm0 | opRetIF| opArgI, |
| 147 |
[OP_STORE1] = opImm0 | opRet0 | opArgI | opArg2I, |
| 148 |
[OP_STORE2] = opImm0 | opRet0 | opArgI | opArg2I, |
| 149 |
[OP_STORE4] = opImm0 | opRet0 | opArgIF| opArg2I, |
| 150 |
[OP_ARG] = opImm1 | opRet0 | opArgIF, |
| 151 |
[OP_BLOCK_COPY] = opImm4 | opRet0 | opArgI | opArg2I, |
| 152 |
|
| 153 |
[OP_SEX8] = opImm0 | opRetI | opArgI, |
| 154 |
[OP_SEX16] = opImm0 | opRetI | opArgI, |
| 155 |
[OP_NEGI] = opImm0 | opRetI | opArgI, |
| 156 |
[OP_ADD] = opImm0 | opRetI | opArgI | opArg2I, |
| 157 |
[OP_SUB] = opImm0 | opRetI | opArgI | opArg2I, |
| 158 |
[OP_DIVI] = opImm0 | opRetI | opArgI | opArg2I, |
| 159 |
[OP_DIVU] = opImm0 | opRetI | opArgI | opArg2I, |
| 160 |
[OP_MODI] = opImm0 | opRetI | opArgI | opArg2I, |
| 161 |
[OP_MODU] = opImm0 | opRetI | opArgI | opArg2I, |
| 162 |
[OP_MULI] = opImm0 | opRetI | opArgI | opArg2I, |
| 163 |
[OP_MULU] = opImm0 | opRetI | opArgI | opArg2I, |
| 164 |
[OP_BAND] = opImm0 | opRetI | opArgI | opArg2I, |
| 165 |
[OP_BOR] = opImm0 | opRetI | opArgI | opArg2I, |
| 166 |
[OP_BXOR] = opImm0 | opRetI | opArgI | opArg2I, |
| 167 |
[OP_BCOM] = opImm0 | opRetI | opArgI, |
| 168 |
[OP_LSH] = opImm0 | opRetI | opArgI | opArg2I, |
| 169 |
[OP_RSHI] = opImm0 | opRetI | opArgI | opArg2I, |
| 170 |
[OP_RSHU] = opImm0 | opRetI | opArgI | opArg2I, |
| 171 |
[OP_NEGF] = opImm0 | opRetF | opArgF, |
| 172 |
[OP_ADDF] = opImm0 | opRetF | opArgF | opArg2F, |
| 173 |
[OP_SUBF] = opImm0 | opRetF | opArgF | opArg2F, |
| 174 |
[OP_DIVF] = opImm0 | opRetF | opArgF | opArg2F, |
| 175 |
[OP_MULF] = opImm0 | opRetF | opArgF | opArg2F, |
| 176 |
[OP_CVIF] = opImm0 | opRetF | opArgI, |
| 177 |
[OP_CVFI] = opImm0 | opRetI | opArgF, |
| 178 |
}; |
| 179 |
|
| 180 |
#ifdef DEBUG_VM |
| 181 |
static const char *opnames[256] = { |
| 182 |
"OP_UNDEF", "OP_IGNORE", "OP_BREAK", "OP_ENTER", "OP_LEAVE", "OP_CALL", |
| 183 |
"OP_PUSH", "OP_POP", "OP_CONST", "OP_LOCAL", "OP_JUMP", |
| 184 |
"OP_EQ", "OP_NE", "OP_LTI", "OP_LEI", "OP_GTI", "OP_GEI", |
| 185 |
"OP_LTU", "OP_LEU", "OP_GTU", "OP_GEU", "OP_EQF", "OP_NEF", |
| 186 |
"OP_LTF", "OP_LEF", "OP_GTF", "OP_GEF", |
| 187 |
"OP_LOAD1", "OP_LOAD2", "OP_LOAD4", "OP_STORE1", "OP_STORE2", |
| 188 |
"OP_STORE4", "OP_ARG", "OP_BLOCK_COPY", |
| 189 |
"OP_SEX8", "OP_SEX16", |
| 190 |
"OP_NEGI", "OP_ADD", "OP_SUB", "OP_DIVI", "OP_DIVU", |
| 191 |
"OP_MODI", "OP_MODU", "OP_MULI", "OP_MULU", "OP_BAND", |
| 192 |
"OP_BOR", "OP_BXOR", "OP_BCOM", "OP_LSH", "OP_RSHI", "OP_RSHU", |
| 193 |
"OP_NEGF", "OP_ADDF", "OP_SUBF", "OP_DIVF", "OP_MULF", |
| 194 |
"OP_CVIF", "OP_CVFI", |
| 195 |
}; |
| 196 |
|
| 197 |
#define NOTIMPL(x) \ |
| 198 |
do { Com_Error(ERR_DROP, "instruction not implemented: %s", opnames[x]); } while(0) |
| 199 |
#else |
| 200 |
#define NOTIMPL(x) \ |
| 201 |
do { Com_Printf(S_COLOR_RED "instruction not implemented: %x\n", x); vm->compiled = qfalse; return; } while(0) |
| 202 |
#endif |
| 203 |
|
| 204 |
static void VM_Destroy_Compiled(vm_t *vm) |
| 205 |
{ |
| 206 |
if (vm->codeBase) { |
| 207 |
if (munmap(vm->codeBase, vm->codeLength)) |
| 208 |
Com_Printf(S_COLOR_RED "Memory unmap failed, possible memory leak\n"); |
| 209 |
} |
| 210 |
vm->codeBase = NULL; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
static void Q_NO_RETURN ErrJump(unsigned num) |
| 221 |
{ |
| 222 |
Com_Error(ERR_DROP, "program tried to execute code outside VM (%x)", num); |
| 223 |
} |
| 224 |
|
| 225 |
static int asmcall(int call, int pstack) |
| 226 |
{ |
| 227 |
|
| 228 |
vm_t *savedVM = currentVM; |
| 229 |
int i, ret; |
| 230 |
|
| 231 |
|
| 232 |
currentVM->programStack = pstack - 4; |
| 233 |
|
| 234 |
if (sizeof(intptr_t) == sizeof(int)) { |
| 235 |
intptr_t *argPosition = (intptr_t *)((byte *)currentVM->dataBase + pstack + 4); |
| 236 |
argPosition[0] = -1 - call; |
| 237 |
ret = currentVM->systemCall(argPosition); |
| 238 |
} else { |
| 239 |
intptr_t args[MAX_VMSYSCALL_ARGS]; |
| 240 |
|
| 241 |
args[0] = -1 - call; |
| 242 |
int *argPosition = (int *)((byte *)currentVM->dataBase + pstack + 4); |
| 243 |
for( i = 1; i < ARRAY_LEN(args); i++ ) |
| 244 |
args[i] = argPosition[i]; |
| 245 |
|
| 246 |
ret = currentVM->systemCall(args); |
| 247 |
} |
| 248 |
|
| 249 |
currentVM = savedVM; |
| 250 |
|
| 251 |
return ret; |
| 252 |
} |
| 253 |
|
| 254 |
void _emit(vm_t *vm, unsigned isn, int pass) |
| 255 |
{ |
| 256 |
#if 0 |
| 257 |
static int fd = -2; |
| 258 |
if (fd == -2) |
| 259 |
fd = open("code.bin", O_TRUNC|O_WRONLY|O_CREAT, 0644); |
| 260 |
if (fd > 0) |
| 261 |
write(fd, &isn, 4); |
| 262 |
#endif |
| 263 |
|
| 264 |
if (pass) |
| 265 |
memcpy(vm->codeBase+vm->codeLength, &isn, 4); |
| 266 |
vm->codeLength+=4; |
| 267 |
} |
| 268 |
|
| 269 |
#define emit(isn) _emit(vm, isn, pass) |
| 270 |
|
| 271 |
static unsigned char off8(unsigned val) |
| 272 |
{ |
| 273 |
if (val&3) |
| 274 |
DIE("offset must be multiple of four"); |
| 275 |
if (val > 1020) |
| 276 |
DIE("offset too large"); |
| 277 |
return val>>2; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
static unsigned short rimm(unsigned val) |
| 282 |
{ |
| 283 |
unsigned shift = 0; |
| 284 |
if (val < 256) |
| 285 |
return val; |
| 286 |
|
| 287 |
while (shift < 16 && (val>255 || !(val&3))) { |
| 288 |
val = (val&3)<<30 | val>>2; |
| 289 |
++shift; |
| 290 |
} |
| 291 |
if (shift > 15 || val > 255) { |
| 292 |
DIE("immediate cannot be encoded (%d, %d)\n", shift, val); |
| 293 |
} |
| 294 |
return (16-shift)<<8 | val; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
static unsigned short can_encode(unsigned val) |
| 299 |
{ |
| 300 |
unsigned shift = 0; |
| 301 |
if (!val) |
| 302 |
DIE("can_encode: invalid argument"); |
| 303 |
if (val < 256) |
| 304 |
return val; |
| 305 |
|
| 306 |
while (shift < 16 && (val>255 || !(val&3))) { |
| 307 |
val = (val&3)<<30 | val>>2; |
| 308 |
++shift; |
| 309 |
} |
| 310 |
if (shift > 15 || val > 255) { |
| 311 |
return 0; |
| 312 |
} |
| 313 |
return (16-shift)<<8 | val; |
| 314 |
} |
| 315 |
|
| 316 |
#define PREINDEX (1<<24) |
| 317 |
|
| 318 |
#define rASR(i, reg) (0b10<<5 | ((i&31)<<7) | reg) |
| 319 |
#define rLSL(i, reg) (0b00<<5 | ((i&31)<<7) | reg) |
| 320 |
#define rLSR(i, reg) (0b01<<5 | ((i&31)<<7) | reg) |
| 321 |
#define rROR(i, reg) (0b11<<5 | ((i&31)<<7) | reg) |
| 322 |
|
| 323 |
|
| 324 |
#define EQ (0b0000<<28) |
| 325 |
#define NE (0b0001<<28) |
| 326 |
#define CS (0b0010<<28) |
| 327 |
#define HS CS |
| 328 |
#define CC (0b0011<<28) |
| 329 |
#define LO CC |
| 330 |
#define MI (0b0100<<28) |
| 331 |
#define PL (0b0101<<28) |
| 332 |
#define VS (0b0110<<28) |
| 333 |
#define VC (0b0111<<28) |
| 334 |
#define HI (0b1000<<28) |
| 335 |
#define LS (0b1001<<28) |
| 336 |
#define GE (0b1010<<28) |
| 337 |
#define LT (0b1011<<28) |
| 338 |
#define GT (0b1100<<28) |
| 339 |
#define LE (0b1101<<28) |
| 340 |
#define AL (0b1110<<28) |
| 341 |
#define cond(what, op) (what | (op&~AL)) |
| 342 |
|
| 343 |
|
| 344 |
#define BKPT(v) (AL | 0b10010<<20 | ((v&~0xF)<<4) | 0b0111<<4 | (v&0xF)) |
| 345 |
|
| 346 |
#define YIELD (0b110010<<20 | 0b1111<<12 | 1) |
| 347 |
#define NOP cond(AL, YIELD) |
| 348 |
|
| 349 |
|
| 350 |
#define ANDi(dst, src, i) (AL | (0b001<<25) | (0b00000<<20) | (src<<16) | (dst<<12) | rimm(i)) |
| 351 |
#define EORi(dst, src, i) (AL | (0b001<<25) | (0b00010<<20) | (src<<16) | (dst<<12) | rimm(i)) |
| 352 |
#define SUBi(dst, src, i) (AL | (0b001<<25) | (0b00100<<20) | (src<<16) | (dst<<12) | rimm(i)) |
| 353 |
#define RSBi(dst, src, i) (AL | (0b001<<25) | (0b00110<<20) | (src<<16) | (dst<<12) | rimm(i)) |
| 354 |
#define ADDi(dst, src, i) (AL | (0b001<<25) | (0b01000<<20) | (src<<16) | (dst<<12) | rimm(i)) |
| 355 |
#define ADCi(dst, src, i) (AL | (0b001<<25) | (0b01010<<20) | (src<<16) | (dst<<12) | rimm(i)) |
| 356 |
#define SBCi(dst, src, i) (AL | (0b001<<25) | (0b01100<<20) | (src<<16) | (dst<<12) | rimm(i)) |
| 357 |
#define RSCi(dst, src, i) (AL | (0b001<<25) | (0b01110<<20) | (src<<16) | (dst<<12) | rimm(i)) |
| 358 |
|
| 359 |
#define ORRi(dst, src, i) (AL | (0b001<<25) | (0b11000<<20) | (src<<16) | (dst<<12) | rimm(i)) |
| 360 |
#define MOVi(dst, i) (AL | (0b001<<25) | (0b11010<<20) | (dst<<12) | rimm(i)) |
| 361 |
#define BICi(dst, src, i) (AL | (0b001<<25) | (0b11100<<20) | (src<<16) | (dst<<12) | rimm(i)) |
| 362 |
#define MVNi(dst, i) (AL | (0b001<<25) | (0b11110<<20) | (dst<<12) | rimm(i)) |
| 363 |
|
| 364 |
#define MOVW(dst, i) (AL | (0b11<<24) | ((((i)>>12)&0xF)<<16) | (dst<<12) | ((i)&((1<<12)-1))) |
| 365 |
#define MOVT(dst, i) (AL | (0b11<<24) | (0b0100<<20) | ((((i)>>12)&0xF)<<16) | (dst<<12) | ((i)&((1<<12)-1))) |
| 366 |
|
| 367 |
#define TSTi( src, i) (AL | (0b001<<25) | (0b10001<<20) | (src<<16) | rimm(i)) |
| 368 |
#define TEQi( src, i) (AL | (0b001<<25) | (0b10011<<20) | (src<<16) | rimm(i)) |
| 369 |
#define CMPi( src, i) (AL | (0b001<<25) | (0b10101<<20) | (src<<16) | rimm(i)) |
| 370 |
#define CMNi( src, i) (AL | (0b001<<25) | (0b10111<<20) | (src<<16) | rimm(i)) |
| 371 |
|
| 372 |
#define ANDSi(dst, src, i) (ANDi(dst, src, i) | (1<<20)) |
| 373 |
#define EORSi(dst, src, i) (EORi(dst, src, i) | (1<<20)) |
| 374 |
#define SUBSi(dst, src, i) (SUBi(dst, src, i) | (1<<20)) |
| 375 |
#define RSBSi(dst, src, i) (RSBi(dst, src, i) | (1<<20)) |
| 376 |
#define ADDSi(dst, src, i) (ADDi(dst, src, i) | (1<<20)) |
| 377 |
#define ADCSi(dst, src, i) (ADCi(dst, src, i) | (1<<20)) |
| 378 |
#define SBCSi(dst, src, i) (SBCi(dst, src, i) | (1<<20)) |
| 379 |
#define RSCSi(dst, src, i) (RSCi(dst, src, i) | (1<<20)) |
| 380 |
|
| 381 |
#define ORRSi(dst, src, i) (ORRi(dst, src, i) | (1<<20)) |
| 382 |
#define MOVSi(dst, i) (MOVi(dst, i) | (1<<20)) |
| 383 |
#define BICSi(dst, src, i) (BICi(dst, src, i) | (1<<20)) |
| 384 |
#define MVNSi(dst, i) (MVNi(dst, src, i) | (1<<20)) |
| 385 |
|
| 386 |
#define AND(dst, src, reg) (AL | (0b000<<25) | (0b00000<<20) | (src<<16) | (dst<<12) | reg) |
| 387 |
#define EOR(dst, src, reg) (AL | (0b000<<25) | (0b00010<<20) | (src<<16) | (dst<<12) | reg) |
| 388 |
#define SUB(dst, src, reg) (AL | (0b000<<25) | (0b00100<<20) | (src<<16) | (dst<<12) | reg) |
| 389 |
#define RSB(dst, src, reg) (AL | (0b000<<25) | (0b00110<<20) | (src<<16) | (dst<<12) | reg) |
| 390 |
#define ADD(dst, src, reg) (AL | (0b000<<25) | (0b01000<<20) | (src<<16) | (dst<<12) | reg) |
| 391 |
#define ADC(dst, src, reg) (AL | (0b000<<25) | (0b01010<<20) | (src<<16) | (dst<<12) | reg) |
| 392 |
#define SBC(dst, src, reg) (AL | (0b000<<25) | (0b01100<<20) | (src<<16) | (dst<<12) | reg) |
| 393 |
#define RSC(dst, src, reg) (AL | (0b000<<25) | (0b01110<<20) | (src<<16) | (dst<<12) | reg) |
| 394 |
|
| 395 |
#define ORR(dst, src, reg) (AL | (0b000<<25) | (0b11000<<20) | (src<<16) | (dst<<12) | reg) |
| 396 |
#define MOV(dst, src) (AL | (0b000<<25) | (0b11010<<20) | (dst<<12) | src) |
| 397 |
|
| 398 |
#define LSL(dst, src, reg) (AL | (0b000<<25) | (0b1101<<21) | (0<<20) | (dst<<12) | (reg<<8) | (0b0001<<4) | src) |
| 399 |
#define LSR(dst, src, reg) (AL | (0b000<<25) | (0b1101<<21) | (0<<20) | (dst<<12) | (reg<<8) | (0b0011<<4) | src) |
| 400 |
#define ASR(dst, src, reg) (AL | (0b000<<25) | (0b1101<<21) | (0<<20) | (dst<<12) | (reg<<8) | (0b0101<<4) | src) |
| 401 |
#define ROR(dst, src, reg) (AL | (0b000<<25) | (0b1101<<21) | (0<<20) | (dst<<12) | (reg<<8) | (0b0111<<4) | src) |
| 402 |
|
| 403 |
#define LSLi(dst, src, i) (AL | (0b000<<25) | (0b1101<<21) | (0<<20) | (dst<<12) | ((i&0x1F)<<7) | (0b000<<4) | src) |
| 404 |
#define LSRi(dst, src, i) (AL | (0b000<<25) | (0b1101<<21) | (0<<20) | (dst<<12) | ((i&0x1F)<<7) | (0b010<<4) | src) |
| 405 |
#define ASRi(dst, src, i) (AL | (0b000<<25) | (0b1101<<21) | (0<<20) | (dst<<12) | ((i&0x1F)<<7) | (0b100<<4) | src) |
| 406 |
#define RORi(dst, src, i) (AL | (0b000<<25) | (0b1101<<21) | (0<<20) | (dst<<12) | ((i&0x1F)<<7) | (0b110<<4) | src) |
| 407 |
#define RRX(dst, src) (AL | (0b000<<25) | (0b1101<<21) | (0<<20) | (dst<<12) | (0b110<<4) | src) |
| 408 |
|
| 409 |
#define BIC(dst, src, reg) (AL | (0b000<<25) | (0b11100<<20) | (src<<16) | (dst<<12) | reg) |
| 410 |
#define MVN(dst, reg) (AL | (0b000<<25) | (0b11110<<20) | (dst<<12) | reg) |
| 411 |
|
| 412 |
#define TST( src, reg) (AL | (0b000<<25) | (0b10001<<20) | (src<<16) | reg) |
| 413 |
#define TEQ( src, reg) (AL | (0b000<<25) | (0b10011<<20) | (src<<16) | reg) |
| 414 |
#define CMP( src, reg) (AL | (0b000<<25) | (0b10101<<20) | (src<<16) | reg) |
| 415 |
#define CMN( src, reg) (AL | (0b000<<25) | (0b10111<<20) | (src<<16) | reg) |
| 416 |
|
| 417 |
#define LDRa(dst, base, off) (AL | (0b011<<25) | (0b1100<<21) | (1<<20) | base<<16 | dst<<12 | off) |
| 418 |
#define LDRx(dst, base, off) (AL | (0b011<<25) | (0b1000<<21) | (1<<20) | base<<16 | dst<<12 | off) |
| 419 |
|
| 420 |
#define LDRai(dst, base, off) (AL | (0b010<<25) | (0b1100<<21) | (1<<20) | base<<16 | dst<<12 | rimm(off)) |
| 421 |
#define LDRxi(dst, base, off) (AL | (0b010<<25) | (0b1000<<21) | (1<<20) | base<<16 | dst<<12 | rimm(off)) |
| 422 |
#define LDRxiw(dst, base, off) (AL | (0b010<<25) | (0b1001<<21) | (1<<20) | base<<16 | dst<<12 | rimm(off)) |
| 423 |
|
| 424 |
#define LDRTa(dst, base, off) (AL | (0b011<<25) | (0b0101<<21) | (1<<20) | base<<16 | dst<<12 | off) |
| 425 |
#define LDRTx(dst, base, off) (AL | (0b011<<25) | (0b0001<<21) | (1<<20) | base<<16 | dst<<12 | off) |
| 426 |
#define LDRTai(dst, base, off) (AL | (0b010<<25) | (0b0101<<21) | (1<<20) | base<<16 | dst<<12 | rimm(off)) |
| 427 |
#define LDRTxi(dst, base, off) (AL | (0b010<<25) | (0b0001<<21) | (1<<20) | base<<16 | dst<<12 | rimm(off)) |
| 428 |
|
| 429 |
#define LDRBa(dst, base, off) (AL | (0b011<<25) | (0b1110<<21) | (1<<20) | base<<16 | dst<<12 | off) |
| 430 |
#define LDRSBai(dst, base, off) (AL | (0b000<<25) | (0b0110<<21) | (1<<20) | base<<16 | dst<<12 | ((off&0xF0)<<4)|0b1101<<4|(off&0x0F)) |
| 431 |
#define STRBa(dst, base, off) (AL | (0b011<<25) | (0b1110<<21) | (0<<20) | base<<16 | dst<<12 | off) |
| 432 |
|
| 433 |
#define LDRHa(dst, base, off) (AL | (0b000<<25) | (0b1100<<21) | (1<<20) | base<<16 | dst<<12 | (0b1011<<4) | off) |
| 434 |
#define LDRSHai(dst, base, off) (AL | (0b000<<25) | (0b1110<<21) | (1<<20) | base<<16 | dst<<12 | ((off&0xF0)<<4)|0b1111<<4|(off&0x0F)) |
| 435 |
#define STRHa(dst, base, off) (AL | (0b000<<25) | (0b1100<<21) | (0<<20) | base<<16 | dst<<12 | (0b1011<<4) | off) |
| 436 |
|
| 437 |
#define STRa(dst, base, off) (AL | (0b011<<25) | (0b1100<<21) | (0<<20) | base<<16 | dst<<12 | off) |
| 438 |
#define STRx(dst, base, off) (AL | (0b011<<25) | (0b1000<<21) | (0<<20) | base<<16 | dst<<12 | off) |
| 439 |
#define STRai(dst, base, off) (AL | (0b010<<25) | (0b1100<<21) | (0<<20) | base<<16 | dst<<12 | rimm(off)) |
| 440 |
#define STRxi(dst, base, off) (AL | (0b010<<25) | (0b1000<<21) | (0<<20) | base<<16 | dst<<12 | rimm(off)) |
| 441 |
#define STRaiw(dst, base, off) (AL | (0b010<<25) | (0b1101<<21) | (0<<20) | base<<16 | dst<<12 | rimm(off)) |
| 442 |
#define STRxiw(dst, base, off) (AL | (0b010<<25) | (0b1001<<21) | (0<<20) | base<<16 | dst<<12 | rimm(off)) |
| 443 |
|
| 444 |
|
| 445 |
#define POP1(reg) (AL | (0b010<<25) | (0b0100<<21) | (1<<20) | SP<<16 | reg<<12 | reg) |
| 446 |
|
| 447 |
#define PUSH1(reg) (AL | (0b010<<25) | (0b1001<<21) | (0<<20) | SP<<16 | reg<<12 | 4) |
| 448 |
|
| 449 |
|
| 450 |
#define Bi(i) \ |
| 451 |
(AL | (0b10)<<26 | (1<<25) | (0<<24) | (i)) |
| 452 |
|
| 453 |
#define BLi(i) \ |
| 454 |
(AL | (0b10)<<26 | (1<<25) | (1<<24) | (i)) |
| 455 |
|
| 456 |
#define BX(reg) \ |
| 457 |
(AL | 0b00010010<<20 | 0b1111<<16 | 0b1111<<12 | 0b1111<<8| 0b0001<<4 | reg) |
| 458 |
|
| 459 |
#define BLX(reg) \ |
| 460 |
(AL | 0b00010010<<20 | 0b1111<<16 | 0b1111<<12 | 0b1111<<8| 0b0011<<4 | reg) |
| 461 |
|
| 462 |
#define PUSH(mask) (AL | (0b100100<<22) | (0b10<<20) | (0b1101<<16) | mask) |
| 463 |
#define PUSH2(r1, r2) (AL | (0b100100<<22) | (0b10<<20) | (0b1101<<16) | 1<<r1 | 1<<r2) |
| 464 |
|
| 465 |
|
| 466 |
#define POP(mask) (0xe8bd0000|mask) |
| 467 |
|
| 468 |
#define STM(base, regs) \ |
| 469 |
(AL | 0b100<<25 | 0<<24| 0<<24| 0<<24| 0<<24 | (base<<16) | (regs&~(1<<16))) |
| 470 |
|
| 471 |
|
| 472 |
#define MUL(op1, op2, op3) \ |
| 473 |
(AL | 0b0000000<<21 | (1<<20) | (op1<<16) | (op3<<8) | 0b1001<<4 | (op2)) |
| 474 |
|
| 475 |
|
| 476 |
#define emit_MOVR0i(arg) emit_MOVRxi(R0, arg) |
| 477 |
|
| 478 |
|
| 479 |
#define emit_MOVRxi(reg, arg) do { \ |
| 480 |
emit(MOVW(reg, (arg&0xFFFF))); \ |
| 481 |
if (arg > 0xFFFF) \ |
| 482 |
emit(MOVT(reg, (((arg>>16)&0xFFFF)))); \ |
| 483 |
} while(0) |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
#define emit_MOVRxi_or_NOP(reg, arg) do { \ |
| 488 |
emit(MOVW(reg, (arg&0xFFFF))); \ |
| 489 |
if (arg > 0xFFFF) \ |
| 490 |
emit(MOVT(reg, (((arg>>16)&0xFFFF)))); \ |
| 491 |
else \ |
| 492 |
emit(NOP); \ |
| 493 |
} while(0) |
| 494 |
|
| 495 |
|
| 496 |
#define VMOVass(Vn, Rt) (AL|(0b1110<<24)|(0b000<<21)|(0<<20)| ((Vn>>1)<<16) | (Rt<<12) | (0b1010<<8) | ((Vn&1)<<7) | (1<<4)) |
| 497 |
|
| 498 |
#define VMOVssa(Rt, Vn) (AL|(0b1110<<24)|(0b000<<21)|(1<<20)| ((Vn>>1)<<16) | (Rt<<12) | (0b1010<<8) | ((Vn&1)<<7) | (1<<4)) |
| 499 |
|
| 500 |
#define _VCVT_F(Vd, Vm, opc2, op) \ |
| 501 |
(AL|(0b11101<<23)|((Vd&1)<<22)|(0b111<<19)|(opc2<<16)|((Vd>>1)<<12)|(0b101<<9)|(0<<8)|(op<<7)|(1<<6)|((Vm&1)<<5)|(Vm>>1)) |
| 502 |
#define VCVT_F32_U32(Sd, Sm) _VCVT_F(Sd, Sm, 0b000, 0 ) |
| 503 |
#define VCVT_U32_F32(Sd, Sm) _VCVT_F(Sd, Sm, 0b100, 1 ) |
| 504 |
#define VCVT_F32_S32(Sd, Sm) _VCVT_F(Sd, Sm, 0b000, 1 ) |
| 505 |
#define VCVT_S32_F32(Sd, Sm) _VCVT_F(Sd, Sm, 0b101, 1 ) |
| 506 |
|
| 507 |
#define VLDRa(Vd, Rn, i) (AL|(0b1101<<24)|1<<23|((Vd&1)<<22)|1<<20|(Rn<<16)|((Vd>>1)<<12)|(0b1010<<8)|off8(i)) |
| 508 |
#define VSTRa(Vd, Rn, i) (AL|(0b1101<<24)|1<<23|((Vd&1)<<22)|0<<20|(Rn<<16)|((Vd>>1)<<12)|(0b1010<<8)|off8(i)) |
| 509 |
|
| 510 |
#define VNEG_F32(Vd, Vm) \ |
| 511 |
(AL|(0b11101<<23)|((Vd&1)<<22)|(0b11<<20)|(1<<16)|((Vd>>1)<<12)|(0b101<<9)|(0<<8)|(1<<6)|((Vm&1)<<5)|(Vm>>1)) |
| 512 |
|
| 513 |
#define VADD_F32(Vd, Vn, Vm) \ |
| 514 |
(AL|(0b11100<<23)|((Vd&1)<<22)|(0b11<<20)|((Vn>>1)<<16)|((Vd>>1)<<12)|(0b101<<9)|(0<<8)|((Vn&1)<<7)|(0<<6)|((Vm&1)<<5)|(Vm>>1)) |
| 515 |
#define VSUB_F32(Vd, Vn, Vm) \ |
| 516 |
(AL|(0b11100<<23)|((Vd&1)<<22)|(0b11<<20)|((Vn>>1)<<16)|((Vd>>1)<<12)|(0b101<<9)|(0<<8)|((Vn&1)<<7)|(1<<6)|((Vm&1)<<5)|(Vm>>1)) |
| 517 |
#define VMUL_F32(Vd, Vn, Vm) \ |
| 518 |
(AL|(0b11100<<23)|((Vd&1)<<22)|(0b10<<20)|((Vn>>1)<<16)|((Vd>>1)<<12)|(0b101)<<9|(0<<8)|((Vn&1)<<7)|(0<<6)|((Vm&1)<<5)|(Vm>>1)) |
| 519 |
#define VDIV_F32(Vd, Vn, Vm) \ |
| 520 |
(AL|(0b11101<<23)|((Vd&1)<<22)|(0b00<<20)|((Vn>>1)<<16)|((Vd>>1)<<12)|(0b101<<9)|(0<<8)|((Vn&1)<<7)|(0<<6)|((Vm&1)<<5)|(Vm>>1)) |
| 521 |
|
| 522 |
#define _VCMP_F32(Vd, Vm, E) \ |
| 523 |
(AL|(0b11101<<23)|((Vd&1)<<22)|(0b11<<20)|((0b0100)<<16)|((Vd>>1)<<12)|(0b101<<9)|(0<<8)|(E<<7)|(1<<6)|((Vm&1)<<5)|(Vm>>1)) |
| 524 |
#define VCMP_F32(Vd, Vm) _VCMP_F32(Vd, Vm, 0) |
| 525 |
|
| 526 |
#define VMRS(Rt) \ |
| 527 |
(AL|(0b11101111<<20)|(0b0001<<16)|(Rt<<12)|(0b1010<<8)|(1<<4)) |
| 528 |
|
| 529 |
|
| 530 |
#define CHECK_JUMP do { \ |
| 531 |
static int bytes_to_skip = -1; \ |
| 532 |
static unsigned branch = -1; \ |
| 533 |
emit_MOVRxi(R1, (unsigned)vm->instructionCount); \ |
| 534 |
emit(CMP(R0, R1)); \ |
| 535 |
if (branch == -1) \ |
| 536 |
branch = vm->codeLength; \ |
| 537 |
emit(cond(LT, Bi(j_rel(bytes_to_skip)))); \ |
| 538 |
emit_MOVRxi_or_NOP(R12, (unsigned)ErrJump); \ |
| 539 |
emit(BLX(R12)); \ |
| 540 |
if (bytes_to_skip == -1) \ |
| 541 |
bytes_to_skip = vm->codeLength - branch; \ |
| 542 |
} while(0) |
| 543 |
|
| 544 |
|
| 545 |
#ifdef CONST_OPTIMIZE |
| 546 |
#define MAYBE_EMIT_CONST() \ |
| 547 |
if (got_const) \ |
| 548 |
{ \ |
| 549 |
got_const = 0; \ |
| 550 |
vm->instructionPointers[instruction-1] = assembler_get_code_size(); \ |
| 551 |
STACK_PUSH(4); \ |
| 552 |
emit("movl $%d, (%%r9, %%rbx, 4)", const_value); \ |
| 553 |
} |
| 554 |
#else |
| 555 |
#define MAYBE_EMIT_CONST() |
| 556 |
#endif |
| 557 |
|
| 558 |
|
| 559 |
#define IJ(comparator) do { \ |
| 560 |
MAYBE_EMIT_CONST(); \ |
| 561 |
emit_MOVRxi(R0, arg.i); \ |
| 562 |
CHECK_JUMP; \ |
| 563 |
emit(LDRTxi(R0, rOPSTACK, 4)); \ |
| 564 |
emit(LDRTxi(R1, rOPSTACK, 4)); \ |
| 565 |
emit(CMP(R1, R0)); \ |
| 566 |
emit(cond(comparator, Bi(j_rel(vm->instructionPointers[arg.i]-vm->codeLength)))); \ |
| 567 |
} while (0) |
| 568 |
|
| 569 |
#define FJ(comparator) do { \ |
| 570 |
emit_MOVRxi(R0, arg.i); \ |
| 571 |
CHECK_JUMP; \ |
| 572 |
emit(SUBi(rOPSTACK, rOPSTACK, 8)); \ |
| 573 |
emit(VLDRa(S15, rOPSTACK, 4)); \ |
| 574 |
emit(VLDRa(S14, rOPSTACK, 8)); \ |
| 575 |
emit(VCMP_F32(S15, S14)); \ |
| 576 |
emit(VMRS(APSR_nzcv)); \ |
| 577 |
emit(cond(comparator, Bi(j_rel(vm->instructionPointers[arg.i]-vm->codeLength)))); \ |
| 578 |
} while (0) |
| 579 |
|
| 580 |
#define printreg(reg) emit(PUSH1(R3)); emit(BLX(reg)); emit(POP1(R3)); |
| 581 |
|
| 582 |
static inline unsigned _j_rel(int x, int pc) |
| 583 |
{ |
| 584 |
if (x&3) goto err; |
| 585 |
x = (x>>2)-2; |
| 586 |
if (x < 0) |
| 587 |
{ |
| 588 |
if ((x&(0xFF<<24)) != 0xFF<<24) |
| 589 |
goto err; |
| 590 |
x &= ~(0xFF<<24); |
| 591 |
} |
| 592 |
else if (x&(0xFF<<24)) |
| 593 |
goto err; |
| 594 |
return x; |
| 595 |
err: |
| 596 |
DIE("jump %d out of range at %d", x, pc); |
| 597 |
} |
| 598 |
|
| 599 |
void VM_Compile(vm_t *vm, vmHeader_t *header) |
| 600 |
{ |
| 601 |
unsigned char *code; |
| 602 |
int i_count, pc = 0; |
| 603 |
int pass; |
| 604 |
int codeoffsets[2]; |
| 605 |
|
| 606 |
#define j_rel(x) (pass?_j_rel(x, pc):0xBAD) |
| 607 |
#define OFFSET(i) (pass?(j_rel(codeoffsets[i]-vm->codeLength)):(0xF000000F)) |
| 608 |
|
| 609 |
#define get_offset(i) (codeoffsets[i]) |
| 610 |
#define save_offset(i) (codeoffsets[i] = vm->codeLength) |
| 611 |
#define OFF_CODE 0 |
| 612 |
#define OFF_IMMEDIATES 1 |
| 613 |
|
| 614 |
vm->compiled = qfalse; |
| 615 |
|
| 616 |
vm->codeBase = NULL; |
| 617 |
vm->codeLength = 0; |
| 618 |
|
| 619 |
for (pass = 0; pass < 2; ++pass) { |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
#ifdef CONST_OPTIMIZE |
| 624 |
|
| 625 |
unsigned got_const = 0, const_value = 0; |
| 626 |
#endif |
| 627 |
|
| 628 |
if(pass) |
| 629 |
{ |
| 630 |
vm->codeBase = mmap(NULL, vm->codeLength, PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0); |
| 631 |
if(vm->codeBase == MAP_FAILED) |
| 632 |
Com_Error(ERR_FATAL, "VM_CompileARM: can't mmap memory"); |
| 633 |
vm->codeLength = 0; |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
emit(PUSH((((1<<8)-1)<<4)|(1<<14))); |
| 638 |
emit(SUBi(SP, SP, 12)); |
| 639 |
emit(LDRai(rCODEBASE, R0, offsetof(vm_t, codeBase))); |
| 640 |
emit(LDRai(rDATABASE, R0, offsetof(vm_t, dataBase))); |
| 641 |
emit(LDRai(rDATAMASK, R0, offsetof(vm_t, dataMask))); |
| 642 |
emit(LDRai(rPSTACK, R1, 0)); |
| 643 |
emit(MOV(rOPSTACK, R2)); |
| 644 |
emit(MOV(rOPSTACKBASE, rOPSTACK)); |
| 645 |
|
| 646 |
emit(BLi(OFFSET(OFF_CODE))); |
| 647 |
|
| 648 |
|
| 649 |
emit(LDRTxi(R0, rOPSTACK, 4)); |
| 650 |
|
| 651 |
emit(ADDi(SP, SP, 12)); |
| 652 |
emit(POP((((1<<8)-1)<<4)|(1<<15))); |
| 653 |
|
| 654 |
|
| 655 |
emit(BKPT(0)); |
| 656 |
emit(BKPT(0)); |
| 657 |
save_offset(OFF_IMMEDIATES); |
| 658 |
|
| 659 |
emit(BKPT(0)); |
| 660 |
emit(BKPT(0)); |
| 661 |
|
| 662 |
save_offset(OFF_CODE); |
| 663 |
|
| 664 |
|
| 665 |
code = (unsigned char *) header + header->codeOffset; |
| 666 |
pc = 0; |
| 667 |
|
| 668 |
for (i_count = 0; i_count < header->instructionCount; i_count++) { |
| 669 |
union { |
| 670 |
unsigned char b[4]; |
| 671 |
unsigned int i; |
| 672 |
} arg; |
| 673 |
unsigned char op = code[pc++]; |
| 674 |
|
| 675 |
vm->instructionPointers[i_count] = vm->codeLength; |
| 676 |
|
| 677 |
if (vm_opInfo[op] & opImm4) |
| 678 |
{ |
| 679 |
memcpy(arg.b, &code[pc], 4); |
| 680 |
pc += 4; |
| 681 |
#ifdef EXCESSIVE_DEBUG |
| 682 |
Com_Printf("%d: instruction %d (%s %d), offset %d\n", pass, i_count, opnames[op], arg.i, vm->codeLength); |
| 683 |
#endif |
| 684 |
} |
| 685 |
else if (vm_opInfo[op] & opImm1) |
| 686 |
{ |
| 687 |
arg.b[0] = code[pc]; |
| 688 |
++pc; |
| 689 |
#ifdef EXCESSIVE_DEBUG |
| 690 |
Com_Printf("%d: instruction %d (%s %hhd), offset %d\n", pass, i_count, opnames[op], arg.i, vm->codeLength); |
| 691 |
#endif |
| 692 |
} |
| 693 |
else |
| 694 |
{ |
| 695 |
#ifdef EXCESSIVE_DEBUG |
| 696 |
Com_Printf("%d: instruction %d (%s), offset %d\n", pass, i_count, opnames[op], vm->codeLength); |
| 697 |
#endif |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
switch ( op ) |
| 704 |
{ |
| 705 |
case OP_UNDEF: |
| 706 |
break; |
| 707 |
|
| 708 |
case OP_IGNORE: |
| 709 |
NOTIMPL(op); |
| 710 |
break; |
| 711 |
|
| 712 |
case OP_BREAK: |
| 713 |
emit(BKPT(0)); |
| 714 |
break; |
| 715 |
|
| 716 |
case OP_ENTER: |
| 717 |
MAYBE_EMIT_CONST(); |
| 718 |
emit(PUSH1(LR)); |
| 719 |
emit(SUBi(SP, SP, 12)); |
| 720 |
if (arg.i == 0 || can_encode(arg.i)) |
| 721 |
{ |
| 722 |
emit(SUBi(rPSTACK, rPSTACK, arg.i)); |
| 723 |
} |
| 724 |
else |
| 725 |
{ |
| 726 |
emit_MOVR0i(arg.i); |
| 727 |
emit(SUB(rPSTACK, rPSTACK, R0)); |
| 728 |
} |
| 729 |
break; |
| 730 |
|
| 731 |
case OP_LEAVE: |
| 732 |
if (arg.i == 0 || can_encode(arg.i)) |
| 733 |
{ |
| 734 |
emit(ADDi(rPSTACK, rPSTACK, arg.i)); |
| 735 |
} |
| 736 |
else |
| 737 |
{ |
| 738 |
emit_MOVR0i(arg.i); |
| 739 |
emit(ADD(rPSTACK, rPSTACK, R0)); |
| 740 |
} |
| 741 |
emit(ADDi(SP, SP, 12)); |
| 742 |
emit(0xe49df004); |
| 743 |
break; |
| 744 |
|
| 745 |
case OP_CALL: |
| 746 |
#if 0 |
| 747 |
|
| 748 |
emit_MOVR0i(i_count); |
| 749 |
emit(STRa(R0, rDATABASE, rPSTACK)); |
| 750 |
#endif |
| 751 |
#ifdef CONST_OPTIMIZE |
| 752 |
if (got_const) |
| 753 |
{ |
| 754 |
NOTIMPL(op); |
| 755 |
} |
| 756 |
else |
| 757 |
#endif |
| 758 |
{ |
| 759 |
static int bytes_to_skip = -1; |
| 760 |
static unsigned start_block = -1; |
| 761 |
MAYBE_EMIT_CONST(); |
| 762 |
|
| 763 |
emit(LDRTxi(R0, rOPSTACK, 4)); |
| 764 |
emit(CMPi(R0, 0)); |
| 765 |
if (start_block == -1) |
| 766 |
start_block = vm->codeLength; |
| 767 |
emit(cond(LT, Bi(j_rel(bytes_to_skip)))); |
| 768 |
CHECK_JUMP; |
| 769 |
emit_MOVRxi_or_NOP(R1, (unsigned)vm->instructionPointers); |
| 770 |
emit(LDRa(R0, R1, rLSL(2, R0))); |
| 771 |
emit(ADD(R0, rCODEBASE, R0)); |
| 772 |
emit(BLX(R0)); |
| 773 |
emit(Bi(j_rel(vm->instructionPointers[i_count+1]-vm->codeLength))); |
| 774 |
if (bytes_to_skip == -1) |
| 775 |
bytes_to_skip = vm->codeLength - start_block; |
| 776 |
emit(MOV(R1, rPSTACK)); |
| 777 |
emit_MOVRxi(R12, (unsigned)asmcall); |
| 778 |
emit(BLX(R12)); |
| 779 |
|
| 780 |
emit(STRaiw(R0, rOPSTACK, 4)); |
| 781 |
} |
| 782 |
break; |
| 783 |
|
| 784 |
case OP_PUSH: |
| 785 |
MAYBE_EMIT_CONST(); |
| 786 |
emit(ADDi(rOPSTACK, rOPSTACK, 4)); |
| 787 |
break; |
| 788 |
|
| 789 |
case OP_POP: |
| 790 |
MAYBE_EMIT_CONST(); |
| 791 |
emit(SUBi(rOPSTACK, rOPSTACK, 4)); |
| 792 |
break; |
| 793 |
|
| 794 |
case OP_CONST: |
| 795 |
MAYBE_EMIT_CONST(); |
| 796 |
emit_MOVR0i(arg.i); |
| 797 |
emit(STRaiw(R0, rOPSTACK, 4)); |
| 798 |
break; |
| 799 |
|
| 800 |
case OP_LOCAL: |
| 801 |
MAYBE_EMIT_CONST(); |
| 802 |
if (arg.i == 0 || can_encode(arg.i)) |
| 803 |
{ |
| 804 |
emit(ADDi(R0, rPSTACK, arg.i)); |
| 805 |
} |
| 806 |
else |
| 807 |
{ |
| 808 |
emit_MOVR0i(arg.i); |
| 809 |
emit(ADD(R0, rPSTACK, R0)); |
| 810 |
} |
| 811 |
emit(STRaiw(R0, rOPSTACK, 4)); |
| 812 |
break; |
| 813 |
|
| 814 |
case OP_JUMP: |
| 815 |
#ifdef CONST_OPTIMIZE |
| 816 |
if (got_const) |
| 817 |
{ |
| 818 |
NOTIMPL(op); |
| 819 |
} |
| 820 |
else |
| 821 |
#endif |
| 822 |
{ |
| 823 |
emit(LDRTxi(R0, rOPSTACK, 4)); |
| 824 |
CHECK_JUMP; |
| 825 |
emit_MOVRxi(R1, (unsigned)vm->instructionPointers); |
| 826 |
emit(LDRa(R0, R1, rLSL(2, R0))); |
| 827 |
emit(ADD(R0, rCODEBASE, R0)); |
| 828 |
emit(BLX(R0)); |
| 829 |
} |
| 830 |
break; |
| 831 |
|
| 832 |
case OP_EQ: |
| 833 |
IJ(EQ); |
| 834 |
break; |
| 835 |
|
| 836 |
case OP_NE: |
| 837 |
IJ(NE); |
| 838 |
break; |
| 839 |
|
| 840 |
case OP_LTI: |
| 841 |
IJ(LT); |
| 842 |
break; |
| 843 |
|
| 844 |
case OP_LEI: |
| 845 |
IJ(LE); |
| 846 |
break; |
| 847 |
|
| 848 |
case OP_GTI: |
| 849 |
IJ(GT); |
| 850 |
break; |
| 851 |
|
| 852 |
case OP_GEI: |
| 853 |
IJ(GE); |
| 854 |
break; |
| 855 |
|
| 856 |
case OP_LTU: |
| 857 |
IJ(LO); |
| 858 |
break; |
| 859 |
|
| 860 |
case OP_LEU: |
| 861 |
IJ(LS); |
| 862 |
break; |
| 863 |
|
| 864 |
case OP_GTU: |
| 865 |
IJ(HI); |
| 866 |
break; |
| 867 |
|
| 868 |
case OP_GEU: |
| 869 |
IJ(HS); |
| 870 |
break; |
| 871 |
|
| 872 |
case OP_EQF: |
| 873 |
FJ(EQ); |
| 874 |
break; |
| 875 |
|
| 876 |
case OP_NEF: |
| 877 |
FJ(NE); |
| 878 |
break; |
| 879 |
|
| 880 |
case OP_LTF: |
| 881 |
FJ(LT); |
| 882 |
break; |
| 883 |
|
| 884 |
case OP_LEF: |
| 885 |
FJ(LE); |
| 886 |
break; |
| 887 |
|
| 888 |
case OP_GTF: |
| 889 |
FJ(GT); |
| 890 |
break; |
| 891 |
|
| 892 |
case OP_GEF: |
| 893 |
FJ(GE); |
| 894 |
break; |
| 895 |
|
| 896 |
case OP_LOAD1: |
| 897 |
MAYBE_EMIT_CONST(); |
| 898 |
emit(LDRai(R0, rOPSTACK, 0)); |
| 899 |
emit(AND(R0, rDATAMASK, R0)); |
| 900 |
emit(LDRBa(R0, rDATABASE, R0)); |
| 901 |
emit(STRai(R0, rOPSTACK, 0)); |
| 902 |
break; |
| 903 |
|
| 904 |
case OP_LOAD2: |
| 905 |
MAYBE_EMIT_CONST(); |
| 906 |
emit(LDRai(R0, rOPSTACK, 0)); |
| 907 |
emit(AND(R0, rDATAMASK, R0)); |
| 908 |
emit(LDRHa(R0, rDATABASE, R0)); |
| 909 |
emit(STRai(R0, rOPSTACK, 0)); |
| 910 |
break; |
| 911 |
|
| 912 |
case OP_LOAD4: |
| 913 |
MAYBE_EMIT_CONST(); |
| 914 |
emit(LDRai(R0, rOPSTACK, 0)); |
| 915 |
emit(AND(R0, rDATAMASK, R0)); |
| 916 |
emit(LDRa(R0, rDATABASE, R0)); |
| 917 |
emit(STRai(R0, rOPSTACK, 0)); |
| 918 |
break; |
| 919 |
|
| 920 |
case OP_STORE1: |
| 921 |
MAYBE_EMIT_CONST(); |
| 922 |
emit(LDRTxi(R0, rOPSTACK, 4)); |
| 923 |
emit(LDRTxi(R1, rOPSTACK, 4)); |
| 924 |
emit(AND(R1, rDATAMASK, R1)); |
| 925 |
emit(STRBa(R0, rDATABASE, R1)); |
| 926 |
break; |
| 927 |
|
| 928 |
case OP_STORE2: |
| 929 |
MAYBE_EMIT_CONST(); |
| 930 |
emit(LDRTxi(R0, rOPSTACK, 4)); |
| 931 |
emit(LDRTxi(R1, rOPSTACK, 4)); |
| 932 |
emit(AND(R1, rDATAMASK, R1)); |
| 933 |
emit(STRHa(R0, rDATABASE, R1)); |
| 934 |
break; |
| 935 |
|
| 936 |
case OP_STORE4: |
| 937 |
MAYBE_EMIT_CONST(); |
| 938 |
|
| 939 |
|
| 940 |
emit(LDRTxi(R0, rOPSTACK, 4)); |
| 941 |
|
| 942 |
emit(LDRTxi(R1, rOPSTACK, 4)); |
| 943 |
emit(AND(R1, rDATAMASK, R1)); |
| 944 |
|
| 945 |
emit(STRa(R0, rDATABASE, R1)); |
| 946 |
break; |
| 947 |
|
| 948 |
case OP_ARG: |
| 949 |
MAYBE_EMIT_CONST(); |
| 950 |
emit(LDRTxi(R0, rOPSTACK, 4)); |
| 951 |
emit(ADDi(R1, rPSTACK, arg.b[0])); |
| 952 |
emit(AND(R1, rDATAMASK, R1)); |
| 953 |
emit(STRa(R0, rDATABASE, R1)); |
| 954 |
break; |
| 955 |
|
| 956 |
case OP_BLOCK_COPY: |
| 957 |
MAYBE_EMIT_CONST(); |
| 958 |
emit(LDRTxi(R1, rOPSTACK, 4)); |
| 959 |
emit(LDRTxi(R0, rOPSTACK, 4)); |
| 960 |
emit_MOVRxi(R2, arg.i); |
| 961 |
emit_MOVRxi(R12, (unsigned)VM_BlockCopy); |
| 962 |
emit(BLX(R12)); |
| 963 |
break; |
| 964 |
|
| 965 |
case OP_SEX8: |
| 966 |
MAYBE_EMIT_CONST(); |
| 967 |
emit(LDRSBai(R0, rOPSTACK, 0)); |
| 968 |
emit(STRai(R0, rOPSTACK, 0)); |
| 969 |
break; |
| 970 |
|
| 971 |
case OP_SEX16: |
| 972 |
MAYBE_EMIT_CONST(); |
| 973 |
emit(LDRSHai(R0, rOPSTACK, 0)); |
| 974 |
emit(STRai(R0, rOPSTACK, 0)); |
| 975 |
break; |
| 976 |
|
| 977 |
case OP_NEGI: |
| 978 |
MAYBE_EMIT_CONST(); |
| 979 |
emit(LDRai(R0, rOPSTACK, 0)); |
| 980 |
emit(RSBi(R0, R0, 0)); |
| 981 |
emit(STRai(R0, rOPSTACK, 0)); |
| 982 |
break; |
| 983 |
|
| 984 |
case OP_ADD: |
| 985 |
MAYBE_EMIT_CONST(); |
| 986 |
emit(LDRai(R0, rOPSTACK, 0)); |
| 987 |
emit(LDRxiw(R1, rOPSTACK, 4)); |
| 988 |
emit(ADD(R0, R1, R0)); |
| 989 |
emit(STRai(R0, rOPSTACK, 0)); |
| 990 |
break; |
| 991 |
|
| 992 |
case OP_SUB: |
| 993 |
MAYBE_EMIT_CONST(); |
| 994 |
emit(LDRai(R0, rOPSTACK, 0)); |
| 995 |
emit(LDRxiw(R1, rOPSTACK, 4)); |
| 996 |
emit(SUB(R0, R1, R0)); |
| 997 |
emit(STRai(R0, rOPSTACK, 0)); |
| 998 |
break; |
| 999 |
|
| 1000 |
case OP_DIVI: |
| 1001 |
case OP_DIVU: |
| 1002 |
MAYBE_EMIT_CONST(); |
| 1003 |
emit(LDRai(R1, rOPSTACK, 0)); |
| 1004 |
emit(LDRxiw(R0, rOPSTACK, 4)); |
| 1005 |
if ( op == OP_DIVI ) |
| 1006 |
emit_MOVRxi(R12, (unsigned)__aeabi_idiv); |
| 1007 |
else |
| 1008 |
emit_MOVRxi(R12, (unsigned)__aeabi_uidiv); |
| 1009 |
emit(BLX(R12)); |
| 1010 |
emit(STRai(R0, rOPSTACK, 0)); |
| 1011 |
break; |
| 1012 |
|
| 1013 |
case OP_MODI: |
| 1014 |
case OP_MODU: |
| 1015 |
MAYBE_EMIT_CONST(); |
| 1016 |
emit(LDRai(R1, rOPSTACK, 0)); |
| 1017 |
emit(LDRxiw(R0, rOPSTACK, 4)); |
| 1018 |
if ( op == OP_MODI ) |
| 1019 |
emit_MOVRxi(R12, (unsigned)__aeabi_idivmod); |
| 1020 |
else |
| 1021 |
emit_MOVRxi(R12, (unsigned)__aeabi_uidivmod); |
| 1022 |
emit(BLX(R12)); |
| 1023 |
emit(STRai(R1, rOPSTACK, 0)); |
| 1024 |
break; |
| 1025 |
|
| 1026 |
case OP_MULI: |
| 1027 |
case OP_MULU: |
| 1028 |
MAYBE_EMIT_CONST(); |
| 1029 |
emit(LDRai(R0, rOPSTACK, 0)); |
| 1030 |
emit(LDRxiw(R1, rOPSTACK, 4)); |
| 1031 |
emit(MUL(R0, R1, R0)); |
| 1032 |
emit(STRai(R0, rOPSTACK, 0)); |
| 1033 |
break; |
| 1034 |
|
| 1035 |
case OP_BAND: |
| 1036 |
MAYBE_EMIT_CONST(); |
| 1037 |
emit(LDRai(R0, rOPSTACK, 0)); |
| 1038 |
emit(LDRxiw(R1, rOPSTACK, 4)); |
| 1039 |
emit(AND(R0, R1, R0)); |
| 1040 |
emit(STRai(R0, rOPSTACK, 0)); |
| 1041 |
break; |
| 1042 |
|
| 1043 |
case OP_BOR: |
| 1044 |
MAYBE_EMIT_CONST(); |
| 1045 |
emit(LDRai(R0, rOPSTACK, 0)); |
| 1046 |
emit(LDRxiw(R1, rOPSTACK, 4)); |
| 1047 |
emit(ORR(R0, R1, R0)); |
| 1048 |
emit(STRai(R0, rOPSTACK, 0)); |
| 1049 |
break; |
| 1050 |
|
| 1051 |
case OP_BXOR: |
| 1052 |
MAYBE_EMIT_CONST(); |
| 1053 |
emit(LDRai(R0, rOPSTACK, 0)); |
| 1054 |
emit(LDRxiw(R1, rOPSTACK, 4)); |
| 1055 |
emit(EOR(R0, R1, R0)); |
| 1056 |
emit(STRai(R0, rOPSTACK, 0)); |
| 1057 |
break; |
| 1058 |
|
| 1059 |
case OP_BCOM: |
| 1060 |
MAYBE_EMIT_CONST(); |
| 1061 |
emit(LDRai(R0, rOPSTACK, 0)); |
| 1062 |
emit(MVN(R0, R0)); |
| 1063 |
emit(STRai(R0, rOPSTACK, 0)); |
| 1064 |
break; |
| 1065 |
|
| 1066 |
case OP_LSH: |
| 1067 |
MAYBE_EMIT_CONST(); |
| 1068 |
emit(LDRai(R0, rOPSTACK, 0)); |
| 1069 |
emit(LDRxiw(R1, rOPSTACK, 4)); |
| 1070 |
emit(LSL(R0, R1, R0)); |
| 1071 |
emit(STRai(R0, rOPSTACK, 0)); |
| 1072 |
break; |
| 1073 |
|
| 1074 |
case OP_RSHI: |
| 1075 |
MAYBE_EMIT_CONST(); |
| 1076 |
emit(LDRai(R0, rOPSTACK, 0)); |
| 1077 |
emit(LDRxiw(R1, rOPSTACK, 4)); |
| 1078 |
emit(ASR(R0, R1, R0)); |
| 1079 |
emit(STRai(R0, rOPSTACK, 0)); |
| 1080 |
break; |
| 1081 |
|
| 1082 |
case OP_RSHU: |
| 1083 |
MAYBE_EMIT_CONST(); |
| 1084 |
emit(LDRai(R0, rOPSTACK, 0)); |
| 1085 |
emit(LDRxiw(R1, rOPSTACK, 4)); |
| 1086 |
emit(LSR(R0, R1, R0)); |
| 1087 |
emit(STRai(R0, rOPSTACK, 0)); |
| 1088 |
break; |
| 1089 |
|
| 1090 |
case OP_NEGF: |
| 1091 |
MAYBE_EMIT_CONST(); |
| 1092 |
emit(VLDRa(S14, rOPSTACK, 0)); |
| 1093 |
emit(VNEG_F32(S14, S14)); |
| 1094 |
emit(VSTRa(S14, rOPSTACK, 0)); |
| 1095 |
break; |
| 1096 |
|
| 1097 |
case OP_ADDF: |
| 1098 |
MAYBE_EMIT_CONST(); |
| 1099 |
emit(VLDRa(S14, rOPSTACK, 0)); |
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
emit(LDRxiw(R0, rOPSTACK, 4)); |
| 1104 |
emit(VMOVass(S15,R0)); |
| 1105 |
emit(VADD_F32(S14, S15, S14)); |
| 1106 |
emit(VSTRa(S14, rOPSTACK, 0)); |
| 1107 |
break; |
| 1108 |
|
| 1109 |
case OP_SUBF: |
| 1110 |
emit(VLDRa(S14, rOPSTACK, 0)); |
| 1111 |
|
| 1112 |
emit(LDRxiw(R0, rOPSTACK, 4)); |
| 1113 |
emit(VMOVass(S15,R0)); |
| 1114 |
emit(VSUB_F32(S14, S15, S14)); |
| 1115 |
emit(VSTRa(S14, rOPSTACK, 0)); |
| 1116 |
break; |
| 1117 |
|
| 1118 |
case OP_DIVF: |
| 1119 |
emit(VLDRa(S14, rOPSTACK, 0)); |
| 1120 |
|
| 1121 |
emit(LDRxiw(R0, rOPSTACK, 4)); |
| 1122 |
emit(VMOVass(S15,R0)); |
| 1123 |
emit(VDIV_F32(S14, S15, S14)); |
| 1124 |
emit(VSTRa(S14, rOPSTACK, 0)); |
| 1125 |
break; |
| 1126 |
|
| 1127 |
case OP_MULF: |
| 1128 |
emit(VLDRa(S14, rOPSTACK, 0)); |
| 1129 |
|
| 1130 |
emit(LDRxiw(R0, rOPSTACK, 4)); |
| 1131 |
emit(VMOVass(S15,R0)); |
| 1132 |
emit(VMUL_F32(S14, S15, S14)); |
| 1133 |
emit(VSTRa(S14, rOPSTACK, 0)); |
| 1134 |
break; |
| 1135 |
|
| 1136 |
case OP_CVIF: |
| 1137 |
MAYBE_EMIT_CONST(); |
| 1138 |
emit(LDRai(R0, rOPSTACK, 0)); |
| 1139 |
emit(VMOVass(S14,R0)); |
| 1140 |
emit(VCVT_F32_S32(S14, S14)); |
| 1141 |
emit(VSTRa(S14, rOPSTACK, 0)); |
| 1142 |
break; |
| 1143 |
|
| 1144 |
case OP_CVFI: |
| 1145 |
MAYBE_EMIT_CONST(); |
| 1146 |
emit(VLDRa(S14, rOPSTACK, 0)); |
| 1147 |
emit(VCVT_S32_F32(S14, S14)); |
| 1148 |
emit(VMOVssa(R0,S14)); |
| 1149 |
emit(STRai(R0, rOPSTACK, 0)); |
| 1150 |
break; |
| 1151 |
} |
| 1152 |
} |
| 1153 |
|
| 1154 |
|
| 1155 |
emit(BKPT(0)); |
| 1156 |
} |
| 1157 |
|
| 1158 |
if (mprotect(vm->codeBase, vm->codeLength, PROT_READ|PROT_EXEC)) { |
| 1159 |
VM_Destroy_Compiled(vm); |
| 1160 |
DIE("mprotect failed"); |
| 1161 |
} |
| 1162 |
|
| 1163 |
|
| 1164 |
__clear_cache(vm->codeBase, vm->codeBase+vm->codeLength); |
| 1165 |
|
| 1166 |
vm->destroy = VM_Destroy_Compiled; |
| 1167 |
vm->compiled = qtrue; |
| 1168 |
} |
| 1169 |
|
| 1170 |
int VM_CallCompiled(vm_t *vm, int *args) |
| 1171 |
{ |
| 1172 |
byte stack[OPSTACK_SIZE + 15]; |
| 1173 |
int *opStack; |
| 1174 |
int programStack = vm->programStack; |
| 1175 |
int stackOnEntry = programStack; |
| 1176 |
byte *image = vm->dataBase; |
| 1177 |
int *argPointer; |
| 1178 |
int retVal; |
| 1179 |
|
| 1180 |
currentVM = vm; |
| 1181 |
|
| 1182 |
vm->currentlyInterpreting = qtrue; |
| 1183 |
|
| 1184 |
programStack -= ( 8 + 4 * MAX_VMMAIN_ARGS ); |
| 1185 |
argPointer = (int *)&image[ programStack + 8 ]; |
| 1186 |
memcpy( argPointer, args, 4 * MAX_VMMAIN_ARGS ); |
| 1187 |
argPointer[-1] = 0; |
| 1188 |
argPointer[-2] = -1; |
| 1189 |
|
| 1190 |
|
| 1191 |
opStack = PADP(stack, 16); |
| 1192 |
*opStack = 0xDEADBEEF; |
| 1193 |
|
| 1194 |
#if 0 |
| 1195 |
Com_Printf("r5 opStack:\t\t%p\n", opStack); |
| 1196 |
Com_Printf("r7 codeBase:\t\t%p\n", vm->codeBase); |
| 1197 |
Com_Printf("r8 programStack:\t0x%x\n", programStack); |
| 1198 |
Com_Printf("r9 dataBase:\t\t%p\n", vm->dataBase); |
| 1199 |
#endif |
| 1200 |
|
| 1201 |
|
| 1202 |
{ |
| 1203 |
|
| 1204 |
int (*entry)(vm_t*, int*, int*); |
| 1205 |
|
| 1206 |
entry = (void *)(vm->codeBase); |
| 1207 |
|
| 1208 |
|
| 1209 |
retVal = entry(vm, &programStack, opStack); |
| 1210 |
} |
| 1211 |
|
| 1212 |
if(*opStack != 0xDEADBEEF) |
| 1213 |
{ |
| 1214 |
Com_Error(ERR_DROP, "opStack corrupted in compiled code"); |
| 1215 |
} |
| 1216 |
|
| 1217 |
if(programStack != stackOnEntry - (8 + 4 * MAX_VMMAIN_ARGS)) |
| 1218 |
Com_Error(ERR_DROP, "programStack corrupted in compiled code"); |
| 1219 |
|
| 1220 |
vm->programStack = stackOnEntry; |
| 1221 |
vm->currentlyInterpreting = qfalse; |
| 1222 |
|
| 1223 |
return retVal; |
| 1224 |
} |
| 1225 |
|
| 1226 |
#endif |