| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#if defined(__sparc) || defined(__sparc__) |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#include <sys/types.h> |
| 30 |
#include <sys/mman.h> |
| 31 |
#include <sys/time.h> |
| 32 |
#include <time.h> |
| 33 |
#include <stddef.h> |
| 34 |
|
| 35 |
#include "vm_local.h" |
| 36 |
|
| 37 |
|
| 38 |
#define G0 0 |
| 39 |
#define G1 1 |
| 40 |
#define G2 2 |
| 41 |
#define G3 3 |
| 42 |
#define G4 4 |
| 43 |
#define G5 5 |
| 44 |
#define G6 6 |
| 45 |
#define G7 7 |
| 46 |
#define O0 8 |
| 47 |
#define O1 9 |
| 48 |
#define O2 10 |
| 49 |
#define O3 11 |
| 50 |
#define O4 12 |
| 51 |
#define O5 13 |
| 52 |
#define O6 14 |
| 53 |
#define O7 15 |
| 54 |
#define L0 16 |
| 55 |
#define L1 17 |
| 56 |
#define L2 18 |
| 57 |
#define L3 19 |
| 58 |
#define L4 20 |
| 59 |
#define L5 21 |
| 60 |
#define L6 22 |
| 61 |
#define L7 23 |
| 62 |
#define I0 24 |
| 63 |
#define I1 25 |
| 64 |
#define I2 26 |
| 65 |
#define I3 27 |
| 66 |
#define I4 28 |
| 67 |
#define I5 29 |
| 68 |
#define I6 30 |
| 69 |
#define I7 31 |
| 70 |
|
| 71 |
|
| 72 |
#define F0 0 |
| 73 |
#define F1 1 |
| 74 |
#define F2 2 |
| 75 |
#define F3 3 |
| 76 |
#define F4 4 |
| 77 |
#define F5 5 |
| 78 |
#define F6 6 |
| 79 |
#define F7 7 |
| 80 |
#define F8 8 |
| 81 |
#define F9 9 |
| 82 |
#define F10 10 |
| 83 |
#define F11 11 |
| 84 |
#define F12 12 |
| 85 |
#define F13 13 |
| 86 |
#define F14 14 |
| 87 |
#define F15 15 |
| 88 |
#define F16 16 |
| 89 |
#define F17 17 |
| 90 |
#define F18 18 |
| 91 |
#define F19 19 |
| 92 |
#define F20 20 |
| 93 |
#define F21 21 |
| 94 |
#define F22 22 |
| 95 |
#define F23 23 |
| 96 |
#define F24 24 |
| 97 |
#define F25 25 |
| 98 |
#define F26 26 |
| 99 |
#define F27 27 |
| 100 |
#define F28 28 |
| 101 |
#define F29 29 |
| 102 |
#define F30 30 |
| 103 |
#define F31 31 |
| 104 |
|
| 105 |
|
| 106 |
#define Y_REG 0 |
| 107 |
#define CCR_REG 2 |
| 108 |
#define ASI_REG 3 |
| 109 |
#define FPRS_REG 6 |
| 110 |
|
| 111 |
|
| 112 |
#define DIE( reason ) \ |
| 113 |
do { \ |
| 114 |
Com_Error(ERR_DROP, "vm_sparc compiler error: " reason); \ |
| 115 |
exit(1); \ |
| 116 |
} while(0) |
| 117 |
|
| 118 |
|
| 119 |
#ifdef __arch64__ |
| 120 |
#define SL(a, b) (b) |
| 121 |
#else |
| 122 |
#define SL(a, b) (a) |
| 123 |
#endif |
| 124 |
|
| 125 |
#define rTMP G1 |
| 126 |
#define rVMDATA G2 |
| 127 |
#define rPSTACK G3 |
| 128 |
#define rDATABASE G4 |
| 129 |
#define rDATAMASK G5 |
| 130 |
|
| 131 |
struct sparc_opcode { |
| 132 |
const char *name; |
| 133 |
unsigned int opcode; |
| 134 |
unsigned int mask; |
| 135 |
unsigned char args[4]; |
| 136 |
#define ARG_NONE 0 |
| 137 |
#define ARG_RS1 1 |
| 138 |
#define ARG_RS2 2 |
| 139 |
#define ARG_RD 3 |
| 140 |
#define ARG_SIMM13 4 |
| 141 |
#define ARG_DISP30 5 |
| 142 |
#define ARG_IMM22 6 |
| 143 |
#define ARG_DISP22 7 |
| 144 |
#define ARG_SWTRAP 8 |
| 145 |
}; |
| 146 |
|
| 147 |
#define ARG_RS1_RS2_RD { ARG_RS1, ARG_RS2, ARG_RD } |
| 148 |
#define ARG_RS1_SIMM13_RD { ARG_RS1, ARG_SIMM13, ARG_RD } |
| 149 |
#define ARG_RS1_RS2 { ARG_RS1, ARG_RS2 } |
| 150 |
#define ARG_RS2_RD { ARG_RS2, ARG_RD } |
| 151 |
|
| 152 |
#define OP_MASK 0xc0000000 |
| 153 |
#define OP2_MASK 0x01c00000 |
| 154 |
#define OP3_MASK 0x01f80000 |
| 155 |
#define OPF_MASK 0x00003fe0 |
| 156 |
|
| 157 |
#define IMM 0x00002000 |
| 158 |
|
| 159 |
#define FMT1(op) ((op) << 30), OP_MASK |
| 160 |
#define FMT2(op,op2) ((op) << 30)|((op2)<<22), (OP_MASK | OP2_MASK) |
| 161 |
#define FMT3(op,op3) ((op) << 30)|((op3)<<19), (OP_MASK | OP3_MASK | IMM) |
| 162 |
#define FMT3I(op,op3) ((op) << 30)|((op3)<<19)|IMM, (OP_MASK | OP3_MASK | IMM) |
| 163 |
#define FMT3F(op,op3,opf) ((op) << 30)|((op3)<<19)|((opf)<<5), \ |
| 164 |
(OP_MASK | OP3_MASK | OPF_MASK) |
| 165 |
|
| 166 |
#define BICC(A,COND) FMT2(0,((A<<7)|(COND<<3)|0x2)) |
| 167 |
#define BFCC(A,COND) FMT2(0,((A<<7)|(COND<<3)|0x6)) |
| 168 |
#define TICC(COND) FMT3I(0,((COND<<6)|0x3a)) |
| 169 |
|
| 170 |
enum sparc_iname { |
| 171 |
CALL, NOP, SETHI, |
| 172 |
|
| 173 |
BA, BN, BNE, BE, BG, BLE, BGE, BL, BGU, BLEU, BCC, BCS, |
| 174 |
BPOS, BNEG, BVC, BVS, |
| 175 |
|
| 176 |
ADDI, ADD, |
| 177 |
ANDI, AND, |
| 178 |
ORI, OR, |
| 179 |
XORI, XOR, |
| 180 |
SUBI, SUB, |
| 181 |
ANDNI, ANDN, |
| 182 |
ORNI, ORN, |
| 183 |
XNORI, XNOR, |
| 184 |
|
| 185 |
UMULI, UMUL, |
| 186 |
SMULI, SMUL, |
| 187 |
UDIVI, UDIV, |
| 188 |
SDIVI, SDIV, |
| 189 |
|
| 190 |
SUBCCI, SUBCC, |
| 191 |
|
| 192 |
SLLI, SLL, |
| 193 |
SRLI, SRL, |
| 194 |
SRAI, SRA, |
| 195 |
|
| 196 |
WRI, WR, |
| 197 |
|
| 198 |
SAVEI, SAVE, |
| 199 |
RESTOREI, RESTORE, |
| 200 |
|
| 201 |
TA, |
| 202 |
|
| 203 |
JMPLI, JMPL, |
| 204 |
|
| 205 |
LDXI, LDX, |
| 206 |
LDUWI, LDUW, |
| 207 |
LDUHI, LDUH, |
| 208 |
LDUBI, LDUB, |
| 209 |
|
| 210 |
STXI, STX, |
| 211 |
STWI, STW, |
| 212 |
STHI, STH, |
| 213 |
STBI, STB, |
| 214 |
|
| 215 |
LDFI, LDF, |
| 216 |
STFI, STF, |
| 217 |
|
| 218 |
FADD, FSUB, FCMP, FSTOI, FITOS, FNEG, FDIV, FMUL, |
| 219 |
FBE, FBNE, FBL, FBGE, FBG, FBLE, |
| 220 |
}; |
| 221 |
|
| 222 |
#define LDLI SL(LDUWI, LDXI) |
| 223 |
#define LDL SL(LDUW, LDX) |
| 224 |
#define STLI SL(STWI, STXI) |
| 225 |
#define STL SL(STW, STX) |
| 226 |
|
| 227 |
#define SPARC_NOP 0x01000000 |
| 228 |
|
| 229 |
static const struct sparc_opcode sparc_opcodes[] = { |
| 230 |
{ "call", FMT1(1), { ARG_DISP30 }, }, |
| 231 |
{ "nop", SPARC_NOP, 0xffffffff, { ARG_NONE }, }, |
| 232 |
{ "sethi", FMT2(0,4), { ARG_IMM22, ARG_RD }, }, |
| 233 |
{ "ba", BICC(0,8), { ARG_DISP22 }, }, |
| 234 |
{ "bn", BICC(0,0), { ARG_DISP22 }, }, |
| 235 |
{ "bne", BICC(0,9), { ARG_DISP22 }, }, |
| 236 |
{ "be", BICC(0,1), { ARG_DISP22 }, }, |
| 237 |
{ "bg", BICC(0,10), { ARG_DISP22 }, }, |
| 238 |
{ "ble", BICC(0,2), { ARG_DISP22 }, }, |
| 239 |
{ "bge", BICC(0,11), { ARG_DISP22 }, }, |
| 240 |
{ "bl", BICC(0,3), { ARG_DISP22 }, }, |
| 241 |
{ "bgu", BICC(0,12), { ARG_DISP22 }, }, |
| 242 |
{ "bleu", BICC(0,4), { ARG_DISP22 }, }, |
| 243 |
{ "bcc", BICC(0,13), { ARG_DISP22 }, }, |
| 244 |
{ "bcs", BICC(0,5), { ARG_DISP22 }, }, |
| 245 |
{ "bpos", BICC(0,14), { ARG_DISP22 }, }, |
| 246 |
{ "bneg", BICC(0,6), { ARG_DISP22 }, }, |
| 247 |
{ "bvc", BICC(0,15), { ARG_DISP22 }, }, |
| 248 |
{ "bvs", BICC(0,7), { ARG_DISP22 }, }, |
| 249 |
|
| 250 |
{ "add", FMT3I(2, 0x00), ARG_RS1_SIMM13_RD, }, |
| 251 |
{ "add", FMT3 (2, 0x00), ARG_RS1_RS2_RD, }, |
| 252 |
{ "and", FMT3I(2, 0x01), ARG_RS1_SIMM13_RD, }, |
| 253 |
{ "and", FMT3 (2, 0x01), ARG_RS1_RS2_RD, }, |
| 254 |
{ "or", FMT3I(2, 0x02), ARG_RS1_SIMM13_RD, }, |
| 255 |
{ "or", FMT3 (2, 0x02), ARG_RS1_RS2_RD, }, |
| 256 |
{ "xor", FMT3I(2, 0x03), ARG_RS1_SIMM13_RD, }, |
| 257 |
{ "xor", FMT3 (2, 0x03), ARG_RS1_RS2_RD, }, |
| 258 |
{ "sub", FMT3I(2, 0x04), ARG_RS1_SIMM13_RD, }, |
| 259 |
{ "sub", FMT3 (2, 0x04), ARG_RS1_RS2_RD, }, |
| 260 |
{ "andn", FMT3I(2, 0x05), ARG_RS1_SIMM13_RD, }, |
| 261 |
{ "andn", FMT3 (2, 0x05), ARG_RS1_RS2_RD, }, |
| 262 |
{ "orn", FMT3I(2, 0x06), ARG_RS1_SIMM13_RD, }, |
| 263 |
{ "orn", FMT3 (2, 0x06), ARG_RS1_RS2_RD, }, |
| 264 |
{ "xnor", FMT3I(2, 0x07), ARG_RS1_SIMM13_RD, }, |
| 265 |
{ "xnor", FMT3 (2, 0x07), ARG_RS1_RS2_RD, }, |
| 266 |
|
| 267 |
{ "umul", FMT3I(2, 0x0a), ARG_RS1_SIMM13_RD, }, |
| 268 |
{ "umul", FMT3 (2, 0x0a), ARG_RS1_RS2_RD, }, |
| 269 |
{ "smul", FMT3I(2, 0x0b), ARG_RS1_SIMM13_RD, }, |
| 270 |
{ "smul", FMT3 (2, 0x0b), ARG_RS1_RS2_RD, }, |
| 271 |
{ "udiv", FMT3I(2, 0x0e), ARG_RS1_SIMM13_RD, }, |
| 272 |
{ "udiv", FMT3 (2, 0x0e), ARG_RS1_RS2_RD, }, |
| 273 |
{ "sdiv", FMT3I(2, 0x0f), ARG_RS1_SIMM13_RD, }, |
| 274 |
{ "sdiv", FMT3 (2, 0x0f), ARG_RS1_RS2_RD, }, |
| 275 |
|
| 276 |
{ "subcc", FMT3I(2, 0x14), ARG_RS1_SIMM13_RD, }, |
| 277 |
{ "subcc", FMT3 (2, 0x14), ARG_RS1_RS2_RD, }, |
| 278 |
|
| 279 |
{ "sll", FMT3I(2, 0x25), ARG_RS1_SIMM13_RD, }, |
| 280 |
{ "sll", FMT3 (2, 0x25), ARG_RS1_RS2_RD, }, |
| 281 |
{ "srl", FMT3I(2, 0x26), ARG_RS1_SIMM13_RD, }, |
| 282 |
{ "srl", FMT3 (2, 0x26), ARG_RS1_RS2_RD, }, |
| 283 |
{ "sra", FMT3I(2, 0x27), ARG_RS1_SIMM13_RD, }, |
| 284 |
{ "sra", FMT3 (2, 0x27), ARG_RS1_RS2_RD, }, |
| 285 |
|
| 286 |
{ "wr", FMT3I(2, 0x30), ARG_RS1_SIMM13_RD, }, |
| 287 |
{ "wr", FMT3 (2, 0x30), ARG_RS1_SIMM13_RD, }, |
| 288 |
|
| 289 |
{ "save", FMT3I(2,0x3c), ARG_RS1_SIMM13_RD, }, |
| 290 |
{ "save", FMT3 (2,0x3c), ARG_RS1_RS2_RD, }, |
| 291 |
{ "restore", FMT3I(2,0x3d), ARG_RS1_SIMM13_RD, }, |
| 292 |
{ "restore", FMT3 (2,0x3d), ARG_RS1_RS2_RD, }, |
| 293 |
{ "ta", TICC(8), { ARG_SWTRAP, ARG_NONE }, }, |
| 294 |
{ "jmpl", FMT3I(2,0x38), ARG_RS1_SIMM13_RD, }, |
| 295 |
{ "jmpl", FMT3 (2,0x38), ARG_RS1_RS2_RD, }, |
| 296 |
|
| 297 |
{ "ldx", FMT3I(3,0x0b), ARG_RS1_SIMM13_RD, }, |
| 298 |
{ "ldx", FMT3 (3,0x0b), ARG_RS1_RS2_RD, }, |
| 299 |
{ "lduw", FMT3I(3,0x00), ARG_RS1_SIMM13_RD, }, |
| 300 |
{ "lduw", FMT3 (3,0x00), ARG_RS1_RS2_RD, }, |
| 301 |
{ "lduh", FMT3I(3,0x02), ARG_RS1_SIMM13_RD, }, |
| 302 |
{ "lduh", FMT3 (3,0x02), ARG_RS1_RS2_RD, }, |
| 303 |
{ "ldub", FMT3I(3,0x01), ARG_RS1_SIMM13_RD, }, |
| 304 |
{ "ldub", FMT3 (3,0x01), ARG_RS1_RS2_RD, }, |
| 305 |
|
| 306 |
{ "stx", FMT3I(3,0x0e), ARG_RS1_SIMM13_RD, }, |
| 307 |
{ "stx", FMT3 (3,0x0e), ARG_RS1_RS2_RD, }, |
| 308 |
{ "stw", FMT3I(3,0x04), ARG_RS1_SIMM13_RD, }, |
| 309 |
{ "stw", FMT3 (3,0x04), ARG_RS1_RS2_RD, }, |
| 310 |
{ "sth", FMT3I(3,0x06), ARG_RS1_SIMM13_RD, }, |
| 311 |
{ "sth", FMT3 (3,0x06), ARG_RS1_RS2_RD, }, |
| 312 |
{ "stb", FMT3I(3,0x05), ARG_RS1_SIMM13_RD, }, |
| 313 |
{ "stb", FMT3 (3,0x05), ARG_RS1_RS2_RD, }, |
| 314 |
|
| 315 |
{ "ldf", FMT3I(3,0x20), ARG_RS1_SIMM13_RD, }, |
| 316 |
{ "ldf", FMT3 (3,0x20), ARG_RS1_RS2_RD, }, |
| 317 |
{ "stf", FMT3I(3,0x24), ARG_RS1_SIMM13_RD, }, |
| 318 |
{ "stf", FMT3 (3,0x24), ARG_RS1_RS2_RD, }, |
| 319 |
|
| 320 |
{ "fadd", FMT3F(2,0x34,0x041), ARG_RS1_RS2_RD, }, |
| 321 |
{ "fsub", FMT3F(2,0x34,0x045), ARG_RS1_RS2_RD, }, |
| 322 |
{ "fcmp", FMT3F(2,0x35,0x051), ARG_RS1_RS2, }, |
| 323 |
{ "fstoi", FMT3F(2,0x34,0x0d1), ARG_RS2_RD, }, |
| 324 |
{ "fitos", FMT3F(2,0x34,0x0c4), ARG_RS2_RD, }, |
| 325 |
|
| 326 |
{ "fneg", FMT3F(2,0x34,0x005), ARG_RS2_RD, }, |
| 327 |
{ "fdiv", FMT3F(2,0x34,0x04d), ARG_RS1_RS2_RD, }, |
| 328 |
{ "fmul", FMT3F(2,0x34,0x049), ARG_RS1_RS2_RD, }, |
| 329 |
|
| 330 |
{ "fbe", BFCC(0,9), { ARG_DISP22 }, }, |
| 331 |
{ "fbne", BFCC(0,1), { ARG_DISP22 }, }, |
| 332 |
{ "fbl", BFCC(0,4), { ARG_DISP22 }, }, |
| 333 |
{ "fbge", BFCC(0,11), { ARG_DISP22 }, }, |
| 334 |
{ "fbg", BFCC(0,6), { ARG_DISP22 }, }, |
| 335 |
{ "fble", BFCC(0,13), { ARG_DISP22 }, }, |
| 336 |
}; |
| 337 |
#define SPARC_NUM_OPCODES (ARRAY_LEN(sparc_opcodes)) |
| 338 |
|
| 339 |
#define RS1(X) (((X) & 0x1f) << 14) |
| 340 |
#define RS2(X) (((X) & 0x1f) << 0) |
| 341 |
#define RD(X) (((X) & 0x1f) << 25) |
| 342 |
#define SIMM13(X) (((X) & 0x1fff) << 0) |
| 343 |
#define IMM22(X) (((X) & 0x3fffff) << 0) |
| 344 |
#define DISP30(X) ((((X) >> 2) & 0x3fffffff) << 0) |
| 345 |
#define DISP22(X) ((((X) >> 2) & 0x3fffff) << 0) |
| 346 |
#define SWTRAP(X) (((X) & 0x7f) << 0) |
| 347 |
|
| 348 |
#define SIMM13_P(X) ((unsigned int) (X) + 0x1000 < 0x2000) |
| 349 |
|
| 350 |
static void vimm(unsigned int val, int bits, int shift, int sgned, int arg_index) |
| 351 |
{ |
| 352 |
unsigned int orig_val = val; |
| 353 |
int orig_bits = bits; |
| 354 |
|
| 355 |
if (sgned) { |
| 356 |
int x = (int) val; |
| 357 |
if (x < 0) |
| 358 |
x = -x; |
| 359 |
val = (unsigned int) x; |
| 360 |
bits--; |
| 361 |
} |
| 362 |
if (val & ~((1U << bits) - 1U)) { |
| 363 |
Com_Printf("VM ERROR: immediate value 0x%08x out of %d bit range\n", |
| 364 |
orig_val, orig_bits); |
| 365 |
DIE("sparc VM bug"); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
static unsigned int sparc_assemble(enum sparc_iname iname, const int argc, const int *argv) |
| 370 |
{ |
| 371 |
const struct sparc_opcode *op = &sparc_opcodes[iname]; |
| 372 |
unsigned int insn = op->opcode; |
| 373 |
int i, flt, rd_flt; |
| 374 |
|
| 375 |
flt = (op->name[0] == 'f'); |
| 376 |
rd_flt = flt || (op->name[2] == 'f'); |
| 377 |
|
| 378 |
for (i = 0; op->args[i] != ARG_NONE; i++) { |
| 379 |
int val = argv[i]; |
| 380 |
|
| 381 |
switch (op->args[i]) { |
| 382 |
case ARG_RS1: insn |= RS1(val); break; |
| 383 |
case ARG_RS2: insn |= RS2(val); break; |
| 384 |
case ARG_RD: insn |= RD(val); break; |
| 385 |
case ARG_SIMM13: insn |= SIMM13(val); vimm(val,13,0,1,i); break; |
| 386 |
case ARG_DISP30: insn |= DISP30(val); vimm(val,30,0,1,i); break; |
| 387 |
case ARG_IMM22: insn |= IMM22(val); vimm(val,22,0,0,i); break; |
| 388 |
case ARG_DISP22: insn |= DISP22(val); vimm(val,22,0,1,i); break; |
| 389 |
case ARG_SWTRAP: insn |= SWTRAP(val); vimm(val,7,0,0,i); break; |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
return insn; |
| 394 |
} |
| 395 |
|
| 396 |
#define IN(inst, args...) \ |
| 397 |
({ const int argv[] = { args }; \ |
| 398 |
const int argc = ARRAY_LEN(argv); \ |
| 399 |
sparc_assemble(inst, argc, argv); \ |
| 400 |
}) |
| 401 |
|
| 402 |
#if 0 |
| 403 |
static void pgreg(int reg_num, int arg_index, int flt) |
| 404 |
{ |
| 405 |
if (!flt) { |
| 406 |
const char *fmt[] = { "%g", "%o", "%l", "%i" }; |
| 407 |
|
| 408 |
Com_Printf("%s%s%d", |
| 409 |
(arg_index ? ", " : ""), |
| 410 |
fmt[reg_num >> 3], reg_num & 7); |
| 411 |
} else |
| 412 |
Com_Printf("%s%%f%d", (arg_index ? ", " : ""), reg_num); |
| 413 |
} |
| 414 |
|
| 415 |
static void pimm(unsigned int val, int bits, int shift, int sgned, int arg_index) |
| 416 |
|
| 417 |
{ |
| 418 |
val >>= shift; |
| 419 |
val &= ((1 << bits) - 1); |
| 420 |
if (sgned) { |
| 421 |
int sval = val << (32 - bits); |
| 422 |
sval >>= (32 - bits); |
| 423 |
Com_Printf("%s%d", |
| 424 |
(arg_index ? ", " : ""), sval); |
| 425 |
} else |
| 426 |
Com_Printf("%s0x%08x", |
| 427 |
(arg_index ? ", " : ""), val); |
| 428 |
} |
| 429 |
|
| 430 |
static void sparc_disassemble(unsigned int insn) |
| 431 |
{ |
| 432 |
int op_idx; |
| 433 |
|
| 434 |
for (op_idx = 0; op_idx < SPARC_NUM_OPCODES; op_idx++) { |
| 435 |
const struct sparc_opcode *op = &sparc_opcodes[op_idx]; |
| 436 |
int i, flt, rd_flt; |
| 437 |
|
| 438 |
if ((insn & op->mask) != op->opcode) |
| 439 |
continue; |
| 440 |
|
| 441 |
flt = (op->name[0] == 'f'); |
| 442 |
rd_flt = flt || (op->name[2] == 'f'); |
| 443 |
|
| 444 |
Com_Printf("ASM: %7s\t", op->name); |
| 445 |
for (i = 0; op->args[i] != ARG_NONE; i++) { |
| 446 |
switch (op->args[i]) { |
| 447 |
case ARG_RS1: pgreg((insn >> 14) & 0x1f, i, flt); break; |
| 448 |
case ARG_RS2: pgreg((insn >> 0) & 0x1f, i, flt); break; |
| 449 |
case ARG_RD: pgreg((insn >> 25) & 0x1f, i, rd_flt); break; |
| 450 |
case ARG_SIMM13: pimm(insn, 13, 0, 1, i); break; |
| 451 |
case ARG_DISP30: pimm(insn, 30, 0, 0, i); break; |
| 452 |
case ARG_IMM22: pimm(insn, 22, 0, 0, i); break; |
| 453 |
case ARG_DISP22: pimm(insn, 22, 0, 0, i); break; |
| 454 |
case ARG_SWTRAP: pimm(insn, 7, 0, 0, i); break; |
| 455 |
} |
| 456 |
} |
| 457 |
Com_Printf("\n"); |
| 458 |
return; |
| 459 |
} |
| 460 |
} |
| 461 |
#endif |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
#define opImm0 0x0000 |
| 470 |
#define opImm1 0x0001 |
| 471 |
#define opImm4 0x0002 |
| 472 |
|
| 473 |
#define opRet0 0x0000 |
| 474 |
#define opRetI 0x0004 |
| 475 |
#define opRetF 0x0008 |
| 476 |
#define opRetIF (opRetI | opRetF) |
| 477 |
|
| 478 |
#define opArg0 0x0000 |
| 479 |
#define opArgI 0x0010 |
| 480 |
#define opArgF 0x0020 |
| 481 |
#define opArgIF (opArgI | opArgF) |
| 482 |
|
| 483 |
#define opArg2I 0x0040 |
| 484 |
#define opArg2F 0x0080 |
| 485 |
#define opArg2IF (opArg2I | opArg2F) |
| 486 |
|
| 487 |
static const unsigned char vm_opInfo[256] = |
| 488 |
{ |
| 489 |
[OP_UNDEF] = opImm0, |
| 490 |
[OP_IGNORE] = opImm0, |
| 491 |
[OP_BREAK] = opImm0, |
| 492 |
[OP_ENTER] = opImm4, |
| 493 |
|
| 494 |
[OP_LEAVE] = opImm4 | opRet0 | opArgIF, |
| 495 |
|
| 496 |
|
| 497 |
[OP_CALL] = opImm0 | opRetI | opArgI, |
| 498 |
[OP_PUSH] = opImm0 | opRetIF, |
| 499 |
[OP_POP] = opImm0 | opRet0 | opArgIF, |
| 500 |
[OP_CONST] = opImm4 | opRetIF, |
| 501 |
[OP_LOCAL] = opImm4 | opRetI, |
| 502 |
[OP_JUMP] = opImm0 | opRet0 | opArgI, |
| 503 |
|
| 504 |
[OP_EQ] = opImm4 | opRet0 | opArgI | opArg2I, |
| 505 |
[OP_NE] = opImm4 | opRet0 | opArgI | opArg2I, |
| 506 |
[OP_LTI] = opImm4 | opRet0 | opArgI | opArg2I, |
| 507 |
[OP_LEI] = opImm4 | opRet0 | opArgI | opArg2I, |
| 508 |
[OP_GTI] = opImm4 | opRet0 | opArgI | opArg2I, |
| 509 |
[OP_GEI] = opImm4 | opRet0 | opArgI | opArg2I, |
| 510 |
[OP_LTU] = opImm4 | opRet0 | opArgI | opArg2I, |
| 511 |
[OP_LEU] = opImm4 | opRet0 | opArgI | opArg2I, |
| 512 |
[OP_GTU] = opImm4 | opRet0 | opArgI | opArg2I, |
| 513 |
[OP_GEU] = opImm4 | opRet0 | opArgI | opArg2I, |
| 514 |
[OP_EQF] = opImm4 | opRet0 | opArgF | opArg2F, |
| 515 |
[OP_NEF] = opImm4 | opRet0 | opArgF | opArg2F, |
| 516 |
[OP_LTF] = opImm4 | opRet0 | opArgF | opArg2F, |
| 517 |
[OP_LEF] = opImm4 | opRet0 | opArgF | opArg2F, |
| 518 |
[OP_GTF] = opImm4 | opRet0 | opArgF | opArg2F, |
| 519 |
[OP_GEF] = opImm4 | opRet0 | opArgF | opArg2F, |
| 520 |
|
| 521 |
[OP_LOAD1] = opImm0 | opRetI | opArgI, |
| 522 |
[OP_LOAD2] = opImm0 | opRetI | opArgI, |
| 523 |
[OP_LOAD4] = opImm0 | opRetIF| opArgI, |
| 524 |
[OP_STORE1] = opImm0 | opRet0 | opArgI | opArg2I, |
| 525 |
[OP_STORE2] = opImm0 | opRet0 | opArgI | opArg2I, |
| 526 |
[OP_STORE4] = opImm0 | opRet0 | opArgIF| opArg2I, |
| 527 |
[OP_ARG] = opImm1 | opRet0 | opArgIF, |
| 528 |
[OP_BLOCK_COPY] = opImm4 | opRet0 | opArgI | opArg2I, |
| 529 |
|
| 530 |
[OP_SEX8] = opImm0 | opRetI | opArgI, |
| 531 |
[OP_SEX16] = opImm0 | opRetI | opArgI, |
| 532 |
[OP_NEGI] = opImm0 | opRetI | opArgI, |
| 533 |
[OP_ADD] = opImm0 | opRetI | opArgI | opArg2I, |
| 534 |
[OP_SUB] = opImm0 | opRetI | opArgI | opArg2I, |
| 535 |
[OP_DIVI] = opImm0 | opRetI | opArgI | opArg2I, |
| 536 |
[OP_DIVU] = opImm0 | opRetI | opArgI | opArg2I, |
| 537 |
[OP_MODI] = opImm0 | opRetI | opArgI | opArg2I, |
| 538 |
[OP_MODU] = opImm0 | opRetI | opArgI | opArg2I, |
| 539 |
[OP_MULI] = opImm0 | opRetI | opArgI | opArg2I, |
| 540 |
[OP_MULU] = opImm0 | opRetI | opArgI | opArg2I, |
| 541 |
[OP_BAND] = opImm0 | opRetI | opArgI | opArg2I, |
| 542 |
[OP_BOR] = opImm0 | opRetI | opArgI | opArg2I, |
| 543 |
[OP_BXOR] = opImm0 | opRetI | opArgI | opArg2I, |
| 544 |
[OP_BCOM] = opImm0 | opRetI | opArgI, |
| 545 |
[OP_LSH] = opImm0 | opRetI | opArgI | opArg2I, |
| 546 |
[OP_RSHI] = opImm0 | opRetI | opArgI | opArg2I, |
| 547 |
[OP_RSHU] = opImm0 | opRetI | opArgI | opArg2I, |
| 548 |
[OP_NEGF] = opImm0 | opRetF | opArgF, |
| 549 |
[OP_ADDF] = opImm0 | opRetF | opArgF | opArg2F, |
| 550 |
[OP_SUBF] = opImm0 | opRetF | opArgF | opArg2F, |
| 551 |
[OP_DIVF] = opImm0 | opRetF | opArgF | opArg2F, |
| 552 |
[OP_MULF] = opImm0 | opRetF | opArgF | opArg2F, |
| 553 |
[OP_CVIF] = opImm0 | opRetF | opArgI, |
| 554 |
[OP_CVFI] = opImm0 | opRetI | opArgF, |
| 555 |
}; |
| 556 |
|
| 557 |
static const char *opnames[256] = { |
| 558 |
"OP_UNDEF", "OP_IGNORE", "OP_BREAK", "OP_ENTER", "OP_LEAVE", "OP_CALL", |
| 559 |
"OP_PUSH", "OP_POP", "OP_CONST", "OP_LOCAL", "OP_JUMP", |
| 560 |
"OP_EQ", "OP_NE", "OP_LTI", "OP_LEI", "OP_GTI", "OP_GEI", |
| 561 |
"OP_LTU", "OP_LEU", "OP_GTU", "OP_GEU", "OP_EQF", "OP_NEF", |
| 562 |
"OP_LTF", "OP_LEF", "OP_GTF", "OP_GEF", |
| 563 |
"OP_LOAD1", "OP_LOAD2", "OP_LOAD4", "OP_STORE1", "OP_STORE2", |
| 564 |
"OP_STORE4", "OP_ARG", "OP_BLOCK_COPY", |
| 565 |
"OP_SEX8", "OP_SEX16", |
| 566 |
"OP_NEGI", "OP_ADD", "OP_SUB", "OP_DIVI", "OP_DIVU", |
| 567 |
"OP_MODI", "OP_MODU", "OP_MULI", "OP_MULU", "OP_BAND", |
| 568 |
"OP_BOR", "OP_BXOR", "OP_BCOM", "OP_LSH", "OP_RSHI", "OP_RSHU", |
| 569 |
"OP_NEGF", "OP_ADDF", "OP_SUBF", "OP_DIVF", "OP_MULF", |
| 570 |
"OP_CVIF", "OP_CVFI", |
| 571 |
}; |
| 572 |
|
| 573 |
static void VM_Destroy_Compiled(vm_t *vm) |
| 574 |
{ |
| 575 |
if (vm->codeBase) { |
| 576 |
if (munmap(vm->codeBase, vm->codeLength)) |
| 577 |
Com_Printf(S_COLOR_RED "Memory unmap failed, possible memory leak\n"); |
| 578 |
} |
| 579 |
vm->codeBase = NULL; |
| 580 |
} |
| 581 |
|
| 582 |
typedef struct VM_Data { |
| 583 |
unsigned int dataLength; |
| 584 |
unsigned int codeLength; |
| 585 |
unsigned int *CallThunk; |
| 586 |
int (*AsmCall)(int, int); |
| 587 |
void (*BlockCopy)(unsigned int, unsigned int, unsigned int); |
| 588 |
unsigned int *iPointers; |
| 589 |
void (*ErrJump)(void); |
| 590 |
unsigned int data[0]; |
| 591 |
} vm_data_t; |
| 592 |
|
| 593 |
#ifdef offsetof |
| 594 |
# define VM_Data_Offset(field) offsetof(vm_data_t, field) |
| 595 |
#else |
| 596 |
# define OFFSET(structName, field) \ |
| 597 |
((void *)&(((structName *)NULL)->field) - NULL) |
| 598 |
# define VM_Data_Offset(field) OFFSET(vm_data_t, field) |
| 599 |
#endif |
| 600 |
|
| 601 |
struct src_insn { |
| 602 |
unsigned char op; |
| 603 |
unsigned int i_count; |
| 604 |
|
| 605 |
union { |
| 606 |
unsigned int i; |
| 607 |
signed int si; |
| 608 |
signed short ss[2]; |
| 609 |
unsigned short us[2]; |
| 610 |
unsigned char b; |
| 611 |
} arg; |
| 612 |
|
| 613 |
unsigned char dst_reg_flags; |
| 614 |
unsigned char src1_reg_flags; |
| 615 |
unsigned char src2_reg_flags; |
| 616 |
#define REG_FLAGS_FLOAT 0x1 |
| 617 |
|
| 618 |
struct src_insn *next; |
| 619 |
}; |
| 620 |
|
| 621 |
struct dst_insn; |
| 622 |
struct jump_insn { |
| 623 |
enum sparc_iname jump_iname; |
| 624 |
int jump_dest_insn; |
| 625 |
struct dst_insn *parent; |
| 626 |
struct jump_insn *next; |
| 627 |
}; |
| 628 |
|
| 629 |
struct dst_insn { |
| 630 |
struct dst_insn *next; |
| 631 |
|
| 632 |
unsigned int count; |
| 633 |
unsigned int i_count; |
| 634 |
|
| 635 |
struct jump_insn *jump; |
| 636 |
unsigned int length; |
| 637 |
unsigned int code[0]; |
| 638 |
}; |
| 639 |
|
| 640 |
#define HUNK_SIZE 29 |
| 641 |
struct data_hunk { |
| 642 |
struct data_hunk *next; |
| 643 |
int count; |
| 644 |
unsigned int data[HUNK_SIZE]; |
| 645 |
}; |
| 646 |
|
| 647 |
struct func_info { |
| 648 |
struct src_insn *first; |
| 649 |
struct src_insn *last; |
| 650 |
int has_call; |
| 651 |
int need_float_tmp; |
| 652 |
|
| 653 |
struct src_insn *cached_const; |
| 654 |
|
| 655 |
int stack_space; |
| 656 |
int gpr_pos; |
| 657 |
#define rFIRST(fp) ((fp)->gpr_pos - 1) |
| 658 |
#define rSECOND(fp) ((fp)->gpr_pos - 2) |
| 659 |
#define POP_GPR(fp) ((fp)->gpr_pos--) |
| 660 |
#define PUSH_GPR(fp) ((fp)->gpr_pos++) |
| 661 |
|
| 662 |
int fpr_pos; |
| 663 |
#define fFIRST(fp) ((fp)->fpr_pos - 1) |
| 664 |
#define fSECOND(fp) ((fp)->fpr_pos - 2) |
| 665 |
#define POP_FPR(fp) ((fp)->fpr_pos--) |
| 666 |
#define PUSH_FPR(fp) ((fp)->fpr_pos++) |
| 667 |
|
| 668 |
#define INSN_BUF_SIZE 50 |
| 669 |
unsigned int insn_buf[INSN_BUF_SIZE]; |
| 670 |
int insn_index; |
| 671 |
|
| 672 |
int saved_icount; |
| 673 |
int force_emit; |
| 674 |
|
| 675 |
struct jump_insn *jump_first; |
| 676 |
struct jump_insn *jump_last; |
| 677 |
|
| 678 |
struct dst_insn *dst_first; |
| 679 |
struct dst_insn *dst_last; |
| 680 |
int dst_count; |
| 681 |
|
| 682 |
struct dst_insn **dst_by_i_count; |
| 683 |
|
| 684 |
struct data_hunk *data_first; |
| 685 |
int data_num; |
| 686 |
}; |
| 687 |
|
| 688 |
#define THUNK_ICOUNT -1 |
| 689 |
|
| 690 |
static unsigned int sparc_push_data(struct func_info * const fp, unsigned int val) |
| 691 |
{ |
| 692 |
struct data_hunk *last, *dp = fp->data_first; |
| 693 |
int off = 0; |
| 694 |
|
| 695 |
last = NULL; |
| 696 |
while (dp) { |
| 697 |
int i; |
| 698 |
|
| 699 |
for (i = 0; i < dp->count; i++) { |
| 700 |
if (dp->data[i] == val) { |
| 701 |
off += i; |
| 702 |
return VM_Data_Offset(data[off]); |
| 703 |
} |
| 704 |
} |
| 705 |
off += dp->count; |
| 706 |
last = dp; |
| 707 |
dp = dp->next; |
| 708 |
} |
| 709 |
|
| 710 |
dp = last; |
| 711 |
if (!dp || dp->count >= HUNK_SIZE) { |
| 712 |
struct data_hunk *new = Z_Malloc(sizeof(*new)); |
| 713 |
if (!dp) |
| 714 |
fp->data_first = new; |
| 715 |
else |
| 716 |
dp->next = new; |
| 717 |
dp = new; |
| 718 |
dp->count = 0; |
| 719 |
dp->next = NULL; |
| 720 |
} |
| 721 |
dp->data[dp->count++] = val; |
| 722 |
fp->data_num = off + 1; |
| 723 |
return VM_Data_Offset(data[off]); |
| 724 |
} |
| 725 |
|
| 726 |
static void dst_insn_insert_tail(struct func_info * const fp, |
| 727 |
struct dst_insn *dp) |
| 728 |
{ |
| 729 |
if (!fp->dst_first) { |
| 730 |
fp->dst_first = fp->dst_last = dp; |
| 731 |
} else { |
| 732 |
fp->dst_last->next = dp; |
| 733 |
fp->dst_last = dp; |
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
static void jump_insn_insert_tail(struct func_info * const fp, |
| 738 |
struct jump_insn *jp) |
| 739 |
{ |
| 740 |
if (!fp->jump_first) { |
| 741 |
fp->jump_first = fp->jump_last = jp; |
| 742 |
} else { |
| 743 |
fp->jump_last->next = jp; |
| 744 |
fp->jump_last = jp; |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
static struct dst_insn *dst_new(struct func_info * const fp, unsigned int length, |
| 749 |
struct jump_insn *jp, int insns_size) |
| 750 |
{ |
| 751 |
struct dst_insn *dp = Z_Malloc(sizeof(struct dst_insn) + insns_size); |
| 752 |
|
| 753 |
dp->length = length; |
| 754 |
dp->jump = jp; |
| 755 |
dp->count = fp->dst_count++; |
| 756 |
dp->i_count = fp->saved_icount; |
| 757 |
dp->next = NULL; |
| 758 |
if (fp->saved_icount != THUNK_ICOUNT) |
| 759 |
fp->dst_by_i_count[fp->saved_icount] = dp; |
| 760 |
|
| 761 |
return dp; |
| 762 |
} |
| 763 |
|
| 764 |
static void dst_insn_append(struct func_info * const fp) |
| 765 |
{ |
| 766 |
int insns_size = (sizeof(unsigned int) * fp->insn_index); |
| 767 |
struct dst_insn *dp; |
| 768 |
|
| 769 |
dp = dst_new(fp, fp->insn_index, NULL, insns_size); |
| 770 |
if (insns_size) |
| 771 |
memcpy(&dp->code[0], fp->insn_buf, insns_size); |
| 772 |
dst_insn_insert_tail(fp, dp); |
| 773 |
|
| 774 |
fp->insn_index = 0; |
| 775 |
} |
| 776 |
|
| 777 |
static void ErrJump(void) |
| 778 |
{ |
| 779 |
Com_Error(ERR_DROP, "program tried to execute code outside VM"); |
| 780 |
exit(1); |
| 781 |
} |
| 782 |
|
| 783 |
static void jump_insn_append(vm_t *vm, struct func_info * const fp, enum sparc_iname iname, int dest) |
| 784 |
{ |
| 785 |
struct jump_insn *jp = Z_Malloc(sizeof(*jp)); |
| 786 |
struct dst_insn *dp; |
| 787 |
|
| 788 |
if (dest < 0 || dest >= vm->instructionCount) |
| 789 |
ErrJump(); |
| 790 |
|
| 791 |
dp = dst_new(fp, 2, jp, 0); |
| 792 |
|
| 793 |
jp->jump_iname = iname; |
| 794 |
jp->jump_dest_insn = dest; |
| 795 |
jp->parent = dp; |
| 796 |
jp->next = NULL; |
| 797 |
|
| 798 |
jump_insn_insert_tail(fp, jp); |
| 799 |
dst_insn_insert_tail(fp, dp); |
| 800 |
} |
| 801 |
|
| 802 |
static void start_emit(struct func_info * const fp, int i_count) |
| 803 |
{ |
| 804 |
fp->saved_icount = i_count; |
| 805 |
fp->insn_index = 0; |
| 806 |
fp->force_emit = 0; |
| 807 |
} |
| 808 |
|
| 809 |
static void __do_emit_one(struct func_info * const fp, unsigned int insn) |
| 810 |
{ |
| 811 |
fp->insn_buf[fp->insn_index++] = insn; |
| 812 |
} |
| 813 |
|
| 814 |
#define in(inst, args...) __do_emit_one(fp, IN(inst, args)) |
| 815 |
|
| 816 |
static void end_emit(struct func_info * const fp) |
| 817 |
{ |
| 818 |
if (fp->insn_index || fp->force_emit) |
| 819 |
dst_insn_append(fp); |
| 820 |
} |
| 821 |
|
| 822 |
static void emit_jump(vm_t *vm, struct func_info * const fp, enum sparc_iname iname, int dest) |
| 823 |
{ |
| 824 |
end_emit(fp); |
| 825 |
jump_insn_append(vm, fp, iname, dest); |
| 826 |
} |
| 827 |
|
| 828 |
static void analyze_function(struct func_info * const fp) |
| 829 |
{ |
| 830 |
struct src_insn *value_provider[20] = { NULL }; |
| 831 |
struct src_insn *sp = fp->first; |
| 832 |
int opstack_depth = 0; |
| 833 |
|
| 834 |
while ((sp = sp->next) != NULL) { |
| 835 |
unsigned char opi, op = sp->op; |
| 836 |
|
| 837 |
opi = vm_opInfo[op]; |
| 838 |
if (opi & opArgIF) { |
| 839 |
struct src_insn *vp = value_provider[--opstack_depth]; |
| 840 |
unsigned char vpopi = vm_opInfo[vp->op]; |
| 841 |
|
| 842 |
if ((opi & opArgI) && (vpopi & opRetI)) { |
| 843 |
|
| 844 |
} else if ((opi & opArgF) && (vpopi & opRetF)) { |
| 845 |
|
| 846 |
vp->dst_reg_flags |= REG_FLAGS_FLOAT; |
| 847 |
sp->src1_reg_flags = REG_FLAGS_FLOAT; |
| 848 |
} else { |
| 849 |
|
| 850 |
DIE("unrecognized instruction combination"); |
| 851 |
} |
| 852 |
} |
| 853 |
if (opi & opArg2IF) { |
| 854 |
struct src_insn *vp = value_provider[--opstack_depth]; |
| 855 |
unsigned char vpopi = vm_opInfo[vp->op]; |
| 856 |
|
| 857 |
if ((opi & opArg2I) && (vpopi & opRetI)) { |
| 858 |
|
| 859 |
} else if ( (opi & opArg2F) && (vpopi & opRetF) ) { |
| 860 |
|
| 861 |
vp->dst_reg_flags |= REG_FLAGS_FLOAT; |
| 862 |
sp->src2_reg_flags = REG_FLAGS_FLOAT; |
| 863 |
} else { |
| 864 |
|
| 865 |
DIE("unrecognized instruction combination"); |
| 866 |
} |
| 867 |
} |
| 868 |
if (opi & opRetIF) { |
| 869 |
value_provider[opstack_depth] = sp; |
| 870 |
opstack_depth++; |
| 871 |
} |
| 872 |
} |
| 873 |
} |
| 874 |
|
| 875 |
static int asmcall(int call, int pstack) |
| 876 |
{ |
| 877 |
vm_t *savedVM = currentVM; |
| 878 |
int i, ret; |
| 879 |
|
| 880 |
currentVM->programStack = pstack - 4; |
| 881 |
if (sizeof(intptr_t) == sizeof(int)) { |
| 882 |
intptr_t *argPosition = (intptr_t *)((byte *)currentVM->dataBase + pstack + 4); |
| 883 |
argPosition[0] = -1 - call; |
| 884 |
ret = currentVM->systemCall(argPosition); |
| 885 |
} else { |
| 886 |
intptr_t args[MAX_VMSYSCALL_ARGS]; |
| 887 |
|
| 888 |
args[0] = -1 - call; |
| 889 |
int *argPosition = (int *)((byte *)currentVM->dataBase + pstack + 4); |
| 890 |
for( i = 1; i < ARRAY_LEN(args); i++ ) |
| 891 |
args[i] = argPosition[i]; |
| 892 |
|
| 893 |
ret = currentVM->systemCall(args); |
| 894 |
} |
| 895 |
|
| 896 |
currentVM = savedVM; |
| 897 |
|
| 898 |
return ret; |
| 899 |
} |
| 900 |
|
| 901 |
static void blockcopy(unsigned int dest, unsigned int src, unsigned int count) |
| 902 |
{ |
| 903 |
unsigned int dataMask = currentVM->dataMask; |
| 904 |
|
| 905 |
if ((dest & dataMask) != dest || |
| 906 |
(src & dataMask) != src || |
| 907 |
((dest+count) & dataMask) != dest + count || |
| 908 |
((src+count) & dataMask) != src + count) { |
| 909 |
DIE("OP_BLOCK_COPY out of range!"); |
| 910 |
} |
| 911 |
|
| 912 |
memcpy(currentVM->dataBase+dest, currentVM->dataBase+src, count); |
| 913 |
} |
| 914 |
|
| 915 |
static void do_emit_const(struct func_info * const fp, struct src_insn *sp) |
| 916 |
{ |
| 917 |
start_emit(fp, sp->i_count); |
| 918 |
if (sp->dst_reg_flags & REG_FLAGS_FLOAT) { |
| 919 |
in(LDFI, rVMDATA, sparc_push_data(fp, sp->arg.i), fFIRST(fp)); |
| 920 |
} else { |
| 921 |
if ((sp->arg.i & ~0x3ff) == 0) { |
| 922 |
in(ORI, G0, sp->arg.i & 0x3ff, rFIRST(fp)); |
| 923 |
} else if ((sp->arg.i & 0x3ff) == 0) { |
| 924 |
in(SETHI, sp->arg.i >> 10, rFIRST(fp)); |
| 925 |
} else { |
| 926 |
in(SETHI, sp->arg.i >> 10, rFIRST(fp)); |
| 927 |
in(ORI, rFIRST(fp), sp->arg.i & 0x3ff, rFIRST(fp)); |
| 928 |
} |
| 929 |
} |
| 930 |
end_emit(fp); |
| 931 |
} |
| 932 |
|
| 933 |
#define MAYBE_EMIT_CONST(fp) \ |
| 934 |
do { if ((fp)->cached_const) { \ |
| 935 |
int saved_i_count = (fp)->saved_icount; \ |
| 936 |
do_emit_const(fp, (fp)->cached_const); \ |
| 937 |
(fp)->saved_icount = saved_i_count; \ |
| 938 |
} \ |
| 939 |
} while (0) |
| 940 |
|
| 941 |
#define EMIT_FALSE_CONST(fp) \ |
| 942 |
do { int saved_i_count = (fp)->saved_icount; \ |
| 943 |
(fp)->saved_icount = (fp)->cached_const->i_count; \ |
| 944 |
dst_insn_append(fp); \ |
| 945 |
(fp)->saved_icount = saved_i_count; \ |
| 946 |
} while (0) |
| 947 |
|
| 948 |
static void compile_one_insn(vm_t *vm, struct func_info * const fp, struct src_insn *sp) |
| 949 |
{ |
| 950 |
start_emit(fp, sp->i_count); |
| 951 |
|
| 952 |
switch (sp->op) { |
| 953 |
default: |
| 954 |
Com_Printf("VM: Unhandled opcode 0x%02x[%s]\n", |
| 955 |
sp->op, |
| 956 |
opnames[sp->op] ? opnames[sp->op] : "UNKNOWN"); |
| 957 |
DIE("Unsupported opcode"); |
| 958 |
break; |
| 959 |
|
| 960 |
case OP_ENTER: { |
| 961 |
int stack = SL(64, 128); |
| 962 |
|
| 963 |
if (fp->need_float_tmp) |
| 964 |
stack += 16; |
| 965 |
|
| 966 |
in(SAVEI, O6, -stack, O6); |
| 967 |
if (!SIMM13_P(sp->arg.si)) { |
| 968 |
in(SETHI, sp->arg.i >> 10, rTMP); |
| 969 |
in(ORI, rTMP, sp->arg.i & 0x3ff, rTMP); |
| 970 |
in(SUB, rPSTACK, rTMP, rPSTACK); |
| 971 |
} else |
| 972 |
in(SUBI, rPSTACK, sp->arg.si, rPSTACK); |
| 973 |
break; |
| 974 |
} |
| 975 |
case OP_LEAVE: |
| 976 |
if (fp->cached_const && SIMM13_P(fp->cached_const->arg.si)) { |
| 977 |
EMIT_FALSE_CONST(fp); |
| 978 |
if (fp->cached_const->src1_reg_flags & REG_FLAGS_FLOAT) |
| 979 |
DIE("constant float in OP_LEAVE"); |
| 980 |
|
| 981 |
if (!SIMM13_P(sp->arg.si)) { |
| 982 |
in(SETHI, sp->arg.i >> 10, rTMP); |
| 983 |
in(ORI, rTMP, sp->arg.i & 0x3ff, rTMP); |
| 984 |
in(ADD, rPSTACK, rTMP, rPSTACK); |
| 985 |
} else |
| 986 |
in(ADDI, rPSTACK, sp->arg.si, rPSTACK); |
| 987 |
in(JMPLI, I7, 8, G0); |
| 988 |
in(RESTOREI, G0, fp->cached_const->arg.si, O0); |
| 989 |
POP_GPR(fp); |
| 990 |
} else { |
| 991 |
MAYBE_EMIT_CONST(fp); |
| 992 |
if (!SIMM13_P(sp->arg.si)) { |
| 993 |
in(SETHI, sp->arg.i >> 10, rTMP); |
| 994 |
in(ORI, rTMP, sp->arg.i & 0x3ff, rTMP); |
| 995 |
in(ADD, rPSTACK, rTMP, rPSTACK); |
| 996 |
} else |
| 997 |
in(ADDI, rPSTACK, sp->arg.si, rPSTACK); |
| 998 |
if (sp->src1_reg_flags & REG_FLAGS_FLOAT) { |
| 999 |
in(STFI, O6, SL(64, 128), fFIRST(fp)); |
| 1000 |
in(LDUWI, O6, SL(64, 128), O0); |
| 1001 |
in(JMPLI, I7, 8, G0); |
| 1002 |
in(RESTORE, O0, G0, O0); |
| 1003 |
POP_FPR(fp); |
| 1004 |
} else { |
| 1005 |
in(JMPLI, I7, 8, G0); |
| 1006 |
in(RESTORE, rFIRST(fp), G0, O0); |
| 1007 |
POP_GPR(fp); |
| 1008 |
} |
| 1009 |
} |
| 1010 |
assert(fp->gpr_pos == L0); |
| 1011 |
assert(fp->fpr_pos == F0); |
| 1012 |
break; |
| 1013 |
case OP_JUMP: |
| 1014 |
if (fp->cached_const) { |
| 1015 |
EMIT_FALSE_CONST(fp); |
| 1016 |
emit_jump(vm, fp, BA, fp->cached_const->arg.i); |
| 1017 |
} else { |
| 1018 |
MAYBE_EMIT_CONST(fp); |
| 1019 |
in(SETHI, vm->instructionCount >> 10, rTMP); |
| 1020 |
in(ORI, rTMP, vm->instructionCount & 0x3ff, rTMP); |
| 1021 |
in(SUBCC, rTMP, rFIRST(fp), G0); |
| 1022 |
in(BLEU, +4*5); |
| 1023 |
in(LDLI, rVMDATA, VM_Data_Offset(ErrJump), rTMP); |
| 1024 |
|
| 1025 |
in(SLLI, rFIRST(fp), 2, rFIRST(fp)); |
| 1026 |
in(LDLI, rVMDATA, VM_Data_Offset(iPointers), rTMP); |
| 1027 |
in(LDL, rTMP, rFIRST(fp), rTMP); |
| 1028 |
in(JMPL, rTMP, G0, G0); |
| 1029 |
in(NOP); |
| 1030 |
} |
| 1031 |
POP_GPR(fp); |
| 1032 |
break; |
| 1033 |
case OP_CALL: |
| 1034 |
if (fp->cached_const) { |
| 1035 |
EMIT_FALSE_CONST(fp); |
| 1036 |
if (fp->cached_const->arg.si >= 0) { |
| 1037 |
emit_jump(vm, fp, CALL, fp->cached_const->arg.i); |
| 1038 |
} else { |
| 1039 |
in(LDLI, rVMDATA, VM_Data_Offset(CallThunk), rTMP); |
| 1040 |
in(LDLI, rVMDATA, VM_Data_Offset(AsmCall), O3); |
| 1041 |
in(ORI, G0, fp->cached_const->arg.si, O0); |
| 1042 |
in(JMPL, rTMP, G0, O7); |
| 1043 |
in(OR, G0, rPSTACK, O1); |
| 1044 |
} |
| 1045 |
in(OR, G0, O0, rFIRST(fp)); |
| 1046 |
} else { |
| 1047 |
MAYBE_EMIT_CONST(fp); |
| 1048 |
in(SUBCCI, rFIRST(fp), 0, G0); |
| 1049 |
in(BL, +4*7); |
| 1050 |
in(NOP); |
| 1051 |
|
| 1052 |
|
| 1053 |
in(SETHI, vm->instructionCount >> 10, rTMP); |
| 1054 |
in(ORI, rTMP, vm->instructionCount & 0x3ff, rTMP); |
| 1055 |
in(SUBCC, rTMP, rFIRST(fp), G0); |
| 1056 |
in(BLEU, +4*9); |
| 1057 |
in(LDLI, rVMDATA, VM_Data_Offset(ErrJump), rTMP); |
| 1058 |
in(LDLI, rVMDATA, VM_Data_Offset(iPointers), O5); |
| 1059 |
in(SLLI, rFIRST(fp), 2, rFIRST(fp)); |
| 1060 |
in(LDL, O5, rFIRST(fp), rTMP); |
| 1061 |
in(BA, +4*4); |
| 1062 |
in(NOP); |
| 1063 |
|
| 1064 |
|
| 1065 |
in(LDLI, rVMDATA, VM_Data_Offset(CallThunk), rTMP); |
| 1066 |
in(LDLI, rVMDATA, VM_Data_Offset(AsmCall), O3); |
| 1067 |
|
| 1068 |
in(OR, G0, rFIRST(fp), O0); |
| 1069 |
in(JMPL, rTMP, G0, O7); |
| 1070 |
in(OR, G0, rPSTACK, O1); |
| 1071 |
|
| 1072 |
|
| 1073 |
in(OR, G0, O0, rFIRST(fp)); |
| 1074 |
} |
| 1075 |
break; |
| 1076 |
case OP_BLOCK_COPY: |
| 1077 |
MAYBE_EMIT_CONST(fp); |
| 1078 |
in(LDLI, rVMDATA, VM_Data_Offset(CallThunk), rTMP); |
| 1079 |
in(LDLI, rVMDATA, VM_Data_Offset(BlockCopy), O3); |
| 1080 |
in(OR, G0, rSECOND(fp), O0); |
| 1081 |
in(OR, G0, rFIRST(fp), O1); |
| 1082 |
if ((sp->arg.i & ~0x3ff) == 0) { |
| 1083 |
in(ORI, G0, sp->arg.i & 0x3ff, O2); |
| 1084 |
} else if ((sp->arg.i & 0x3ff) == 0) { |
| 1085 |
in(SETHI, sp->arg.i >> 10, O2); |
| 1086 |
} else { |
| 1087 |
in(SETHI, sp->arg.i >> 10, O2); |
| 1088 |
in(ORI, O2, sp->arg.i & 0x3ff, O2); |
| 1089 |
} |
| 1090 |
in(JMPL, rTMP, G0, O7); |
| 1091 |
in(NOP); |
| 1092 |
POP_GPR(fp); |
| 1093 |
POP_GPR(fp); |
| 1094 |
break; |
| 1095 |
|
| 1096 |
case OP_PUSH: |
| 1097 |
MAYBE_EMIT_CONST(fp); |
| 1098 |
if (sp->dst_reg_flags & REG_FLAGS_FLOAT) |
| 1099 |
PUSH_FPR(fp); |
| 1100 |
else |
| 1101 |
PUSH_GPR(fp); |
| 1102 |
fp->force_emit = 1; |
| 1103 |
break; |
| 1104 |
case OP_POP: |
| 1105 |
MAYBE_EMIT_CONST(fp); |
| 1106 |
if (sp->src1_reg_flags & REG_FLAGS_FLOAT) |
| 1107 |
POP_FPR(fp); |
| 1108 |
else |
| 1109 |
POP_GPR(fp); |
| 1110 |
fp->force_emit = 1; |
| 1111 |
break; |
| 1112 |
case OP_ARG: |
| 1113 |
MAYBE_EMIT_CONST(fp); |
| 1114 |
in(ADDI, rPSTACK, sp->arg.b, rTMP); |
| 1115 |
if (sp->src1_reg_flags & REG_FLAGS_FLOAT) { |
| 1116 |
in(STF, rDATABASE, rTMP, fFIRST(fp)); |
| 1117 |
POP_FPR(fp); |
| 1118 |
} else { |
| 1119 |
in(STW, rDATABASE, rTMP, rFIRST(fp)); |
| 1120 |
POP_GPR(fp); |
| 1121 |
} |
| 1122 |
break; |
| 1123 |
case OP_IGNORE: |
| 1124 |
MAYBE_EMIT_CONST(fp); |
| 1125 |
in(NOP); |
| 1126 |
break; |
| 1127 |
case OP_BREAK: |
| 1128 |
MAYBE_EMIT_CONST(fp); |
| 1129 |
in(TA, 0x5); |
| 1130 |
break; |
| 1131 |
case OP_LOCAL: |
| 1132 |
MAYBE_EMIT_CONST(fp); |
| 1133 |
PUSH_GPR(fp); |
| 1134 |
if (!SIMM13_P(sp->arg.i)) { |
| 1135 |
in(SETHI, sp->arg.i >> 10, rTMP); |
| 1136 |
in(ORI, rTMP, sp->arg.i & 0x3ff, rTMP); |
| 1137 |
in(ADD, rPSTACK, rTMP, rFIRST(fp)); |
| 1138 |
} else |
| 1139 |
in(ADDI, rPSTACK, sp->arg.i, rFIRST(fp)); |
| 1140 |
break; |
| 1141 |
case OP_CONST: |
| 1142 |
MAYBE_EMIT_CONST(fp); |
| 1143 |
break; |
| 1144 |
case OP_LOAD4: |
| 1145 |
MAYBE_EMIT_CONST(fp); |
| 1146 |
in(AND, rFIRST(fp), rDATAMASK, rFIRST(fp)); |
| 1147 |
if (sp->dst_reg_flags & REG_FLAGS_FLOAT) { |
| 1148 |
PUSH_FPR(fp); |
| 1149 |
in(LDF, rFIRST(fp), rDATABASE, fFIRST(fp)); |
| 1150 |
POP_GPR(fp); |
| 1151 |
} else { |
| 1152 |
in(LDUW, rFIRST(fp), rDATABASE, rFIRST(fp)); |
| 1153 |
} |
| 1154 |
break; |
| 1155 |
case OP_LOAD2: |
| 1156 |
MAYBE_EMIT_CONST(fp); |
| 1157 |
in(AND, rFIRST(fp), rDATAMASK, rFIRST(fp)); |
| 1158 |
in(LDUH, rFIRST(fp), rDATABASE, rFIRST(fp)); |
| 1159 |
break; |
| 1160 |
case OP_LOAD1: |
| 1161 |
MAYBE_EMIT_CONST(fp); |
| 1162 |
in(AND, rFIRST(fp), rDATAMASK, rFIRST(fp)); |
| 1163 |
in(LDUB, rFIRST(fp), rDATABASE, rFIRST(fp)); |
| 1164 |
break; |
| 1165 |
case OP_STORE4: |
| 1166 |
MAYBE_EMIT_CONST(fp); |
| 1167 |
if (sp->src1_reg_flags & REG_FLAGS_FLOAT) { |
| 1168 |
in(AND, rFIRST(fp), rDATAMASK, rFIRST(fp)); |
| 1169 |
in(STF, rFIRST(fp), rDATABASE, fFIRST(fp)); |
| 1170 |
POP_FPR(fp); |
| 1171 |
} else { |
| 1172 |
in(AND, rSECOND(fp), rDATAMASK, rSECOND(fp)); |
| 1173 |
in(STW, rSECOND(fp), rDATABASE, rFIRST(fp)); |
| 1174 |
POP_GPR(fp); |
| 1175 |
} |
| 1176 |
POP_GPR(fp); |
| 1177 |
break; |
| 1178 |
case OP_STORE2: |
| 1179 |
MAYBE_EMIT_CONST(fp); |
| 1180 |
in(AND, rSECOND(fp), rDATAMASK, rSECOND(fp)); |
| 1181 |
in(STH, rSECOND(fp), rDATABASE, rFIRST(fp)); |
| 1182 |
POP_GPR(fp); |
| 1183 |
POP_GPR(fp); |
| 1184 |
break; |
| 1185 |
case OP_STORE1: |
| 1186 |
MAYBE_EMIT_CONST(fp); |
| 1187 |
in(AND, rSECOND(fp), rDATAMASK, rSECOND(fp)); |
| 1188 |
in(STB, rSECOND(fp), rDATABASE, rFIRST(fp)); |
| 1189 |
POP_GPR(fp); |
| 1190 |
POP_GPR(fp); |
| 1191 |
break; |
| 1192 |
case OP_EQ: |
| 1193 |
case OP_NE: |
| 1194 |
case OP_LTI: |
| 1195 |
case OP_GEI: |
| 1196 |
case OP_GTI: |
| 1197 |
case OP_LEI: |
| 1198 |
case OP_LTU: |
| 1199 |
case OP_GEU: |
| 1200 |
case OP_GTU: |
| 1201 |
case OP_LEU: { |
| 1202 |
enum sparc_iname iname = BA; |
| 1203 |
|
| 1204 |
if (fp->cached_const && SIMM13_P(fp->cached_const->arg.si)) { |
| 1205 |
EMIT_FALSE_CONST(fp); |
| 1206 |
in(SUBCCI, rSECOND(fp), fp->cached_const->arg.si, G0); |
| 1207 |
} else { |
| 1208 |
MAYBE_EMIT_CONST(fp); |
| 1209 |
in(SUBCC, rSECOND(fp), rFIRST(fp), G0); |
| 1210 |
} |
| 1211 |
switch(sp->op) { |
| 1212 |
case OP_EQ: iname = BE; break; |
| 1213 |
case OP_NE: iname = BNE; break; |
| 1214 |
case OP_LTI: iname = BL; break; |
| 1215 |
case OP_GEI: iname = BGE; break; |
| 1216 |
case OP_GTI: iname = BG; break; |
| 1217 |
case OP_LEI: iname = BLE; break; |
| 1218 |
case OP_LTU: iname = BCS; break; |
| 1219 |
case OP_GEU: iname = BCC; break; |
| 1220 |
case OP_GTU: iname = BGU; break; |
| 1221 |
case OP_LEU: iname = BLEU; break; |
| 1222 |
} |
| 1223 |
emit_jump(vm, fp, iname, sp->arg.i); |
| 1224 |
POP_GPR(fp); |
| 1225 |
POP_GPR(fp); |
| 1226 |
break; |
| 1227 |
} |
| 1228 |
|
| 1229 |
case OP_SEX8: |
| 1230 |
MAYBE_EMIT_CONST(fp); |
| 1231 |
in(SLLI, rFIRST(fp), 24, rFIRST(fp)); |
| 1232 |
in(SRAI, rFIRST(fp), 24, rFIRST(fp)); |
| 1233 |
break; |
| 1234 |
case OP_SEX16: |
| 1235 |
MAYBE_EMIT_CONST(fp); |
| 1236 |
in(SLLI, rFIRST(fp), 16, rFIRST(fp)); |
| 1237 |
in(SRAI, rFIRST(fp), 16, rFIRST(fp)); |
| 1238 |
break; |
| 1239 |
case OP_NEGI: |
| 1240 |
MAYBE_EMIT_CONST(fp); |
| 1241 |
in(SUB, G0, rFIRST(fp), rFIRST(fp)); |
| 1242 |
break; |
| 1243 |
case OP_ADD: |
| 1244 |
if (fp->cached_const && SIMM13_P(fp->cached_const->arg.si)) { |
| 1245 |
EMIT_FALSE_CONST(fp); |
| 1246 |
in(ADDI, rSECOND(fp), fp->cached_const->arg.si, rSECOND(fp)); |
| 1247 |
} else { |
| 1248 |
MAYBE_EMIT_CONST(fp); |
| 1249 |
in(ADD, rSECOND(fp), rFIRST(fp), rSECOND(fp)); |
| 1250 |
} |
| 1251 |
POP_GPR(fp); |
| 1252 |
break; |
| 1253 |
case OP_SUB: |
| 1254 |
if (fp->cached_const && SIMM13_P(fp->cached_const->arg.si)) { |
| 1255 |
EMIT_FALSE_CONST(fp); |
| 1256 |
in(SUBI, rSECOND(fp), fp->cached_const->arg.si, rSECOND(fp)); |
| 1257 |
} else { |
| 1258 |
MAYBE_EMIT_CONST(fp); |
| 1259 |
in(SUB, rSECOND(fp), rFIRST(fp), rSECOND(fp)); |
| 1260 |
} |
| 1261 |
POP_GPR(fp); |
| 1262 |
break; |
| 1263 |
case OP_DIVI: |
| 1264 |
MAYBE_EMIT_CONST(fp); |
| 1265 |
in(SRAI, rSECOND(fp), 31, rTMP); |
| 1266 |
in(WRI, rTMP, 0, Y_REG); |
| 1267 |
in(SDIV, rSECOND(fp), rFIRST(fp), rSECOND(fp)); |
| 1268 |
POP_GPR(fp); |
| 1269 |
break; |
| 1270 |
case OP_DIVU: |
| 1271 |
MAYBE_EMIT_CONST(fp); |
| 1272 |
in(WRI, G0, 0, Y_REG); |
| 1273 |
in(UDIV, rSECOND(fp), rFIRST(fp), rSECOND(fp)); |
| 1274 |
POP_GPR(fp); |
| 1275 |
break; |
| 1276 |
case OP_MODI: |
| 1277 |
MAYBE_EMIT_CONST(fp); |
| 1278 |
in(SRAI, rSECOND(fp), 31, rTMP); |
| 1279 |
in(WRI, rTMP, 0, Y_REG); |
| 1280 |
in(SDIV, rSECOND(fp), rFIRST(fp), rTMP); |
| 1281 |
in(SMUL, rTMP, rFIRST(fp), rTMP); |
| 1282 |
in(SUB, rSECOND(fp), rTMP, rSECOND(fp)); |
| 1283 |
POP_GPR(fp); |
| 1284 |
break; |
| 1285 |
case OP_MODU: |
| 1286 |
MAYBE_EMIT_CONST(fp); |
| 1287 |
in(WRI, G0, 0, Y_REG); |
| 1288 |
in(UDIV, rSECOND(fp), rFIRST(fp), rTMP); |
| 1289 |
in(SMUL, rTMP, rFIRST(fp), rTMP); |
| 1290 |
in(SUB, rSECOND(fp), rTMP, rSECOND(fp)); |
| 1291 |
POP_GPR(fp); |
| 1292 |
break; |
| 1293 |
case OP_MULI: |
| 1294 |
MAYBE_EMIT_CONST(fp); |
| 1295 |
in(SMUL, rSECOND(fp), rFIRST(fp), rSECOND(fp)); |
| 1296 |
POP_GPR(fp); |
| 1297 |
break; |
| 1298 |
case OP_MULU: |
| 1299 |
MAYBE_EMIT_CONST(fp); |
| 1300 |
in(UMUL, rSECOND(fp), rFIRST(fp), rSECOND(fp)); |
| 1301 |
POP_GPR(fp); |
| 1302 |
break; |
| 1303 |
case OP_BAND: |
| 1304 |
MAYBE_EMIT_CONST(fp); |
| 1305 |
in(AND, rSECOND(fp), rFIRST(fp), rSECOND(fp)); |
| 1306 |
POP_GPR(fp); |
| 1307 |
break; |
| 1308 |
case OP_BOR: |
| 1309 |
MAYBE_EMIT_CONST(fp); |
| 1310 |
in(OR, rSECOND(fp), rFIRST(fp), rSECOND(fp)); |
| 1311 |
POP_GPR(fp); |
| 1312 |
break; |
| 1313 |
case OP_BXOR: |
| 1314 |
MAYBE_EMIT_CONST(fp); |
| 1315 |
in(XOR, rSECOND(fp), rFIRST(fp), rSECOND(fp)); |
| 1316 |
POP_GPR(fp); |
| 1317 |
break; |
| 1318 |
case OP_BCOM: |
| 1319 |
MAYBE_EMIT_CONST(fp); |
| 1320 |
in(XNOR, rFIRST(fp), G0, rFIRST(fp)); |
| 1321 |
break; |
| 1322 |
case OP_LSH: |
| 1323 |
if (fp->cached_const) { |
| 1324 |
EMIT_FALSE_CONST(fp); |
| 1325 |
in(SLLI, rSECOND(fp), fp->cached_const->arg.si, rSECOND(fp)); |
| 1326 |
} else { |
| 1327 |
MAYBE_EMIT_CONST(fp); |
| 1328 |
in(SLL, rSECOND(fp), rFIRST(fp), rSECOND(fp)); |
| 1329 |
} |
| 1330 |
POP_GPR(fp); |
| 1331 |
break; |
| 1332 |
case OP_RSHI: |
| 1333 |
if (fp->cached_const) { |
| 1334 |
EMIT_FALSE_CONST(fp); |
| 1335 |
in(SRAI, rSECOND(fp), fp->cached_const->arg.si, rSECOND(fp)); |
| 1336 |
} else { |
| 1337 |
MAYBE_EMIT_CONST(fp); |
| 1338 |
in(SRA, rSECOND(fp), rFIRST(fp), rSECOND(fp)); |
| 1339 |
} |
| 1340 |
POP_GPR(fp); |
| 1341 |
break; |
| 1342 |
case OP_RSHU: |
| 1343 |
if (fp->cached_const) { |
| 1344 |
EMIT_FALSE_CONST(fp); |
| 1345 |
in(SRLI, rSECOND(fp), fp->cached_const->arg.si, rSECOND(fp)); |
| 1346 |
} else { |
| 1347 |
MAYBE_EMIT_CONST(fp); |
| 1348 |
in(SRL, rSECOND(fp), rFIRST(fp), rSECOND(fp)); |
| 1349 |
} |
| 1350 |
POP_GPR(fp); |
| 1351 |
break; |
| 1352 |
|
| 1353 |
case OP_NEGF: |
| 1354 |
MAYBE_EMIT_CONST(fp); |
| 1355 |
in(FNEG, fFIRST(fp), fFIRST(fp)); |
| 1356 |
break; |
| 1357 |
case OP_ADDF: |
| 1358 |
MAYBE_EMIT_CONST(fp); |
| 1359 |
in(FADD, fSECOND(fp), fFIRST(fp), fSECOND(fp)); |
| 1360 |
POP_FPR(fp); |
| 1361 |
break; |
| 1362 |
case OP_SUBF: |
| 1363 |
MAYBE_EMIT_CONST(fp); |
| 1364 |
in(FSUB, fSECOND(fp), fFIRST(fp), fSECOND(fp)); |
| 1365 |
POP_FPR(fp); |
| 1366 |
break; |
| 1367 |
case OP_DIVF: |
| 1368 |
MAYBE_EMIT_CONST(fp); |
| 1369 |
in(FDIV, fSECOND(fp), fFIRST(fp), fSECOND(fp)); |
| 1370 |
POP_FPR(fp); |
| 1371 |
break; |
| 1372 |
case OP_MULF: |
| 1373 |
MAYBE_EMIT_CONST(fp); |
| 1374 |
in(FMUL, fSECOND(fp), fFIRST(fp), fSECOND(fp)); |
| 1375 |
POP_FPR(fp); |
| 1376 |
break; |
| 1377 |
|
| 1378 |
case OP_EQF: |
| 1379 |
case OP_NEF: |
| 1380 |
case OP_LTF: |
| 1381 |
case OP_GEF: |
| 1382 |
case OP_GTF: |
| 1383 |
case OP_LEF: { |
| 1384 |
enum sparc_iname iname = FBE; |
| 1385 |
|
| 1386 |
MAYBE_EMIT_CONST(fp); |
| 1387 |
in(FCMP, fSECOND(fp), fFIRST(fp)); |
| 1388 |
switch(sp->op) { |
| 1389 |
case OP_EQF: iname = FBE; break; |
| 1390 |
case OP_NEF: iname = FBNE; break; |
| 1391 |
case OP_LTF: iname = FBL; break; |
| 1392 |
case OP_GEF: iname = FBGE; break; |
| 1393 |
case OP_GTF: iname = FBG; break; |
| 1394 |
case OP_LEF: iname = FBLE; break; |
| 1395 |
} |
| 1396 |
emit_jump(vm, fp, iname, sp->arg.i); |
| 1397 |
POP_FPR(fp); |
| 1398 |
POP_FPR(fp); |
| 1399 |
break; |
| 1400 |
} |
| 1401 |
case OP_CVIF: |
| 1402 |
MAYBE_EMIT_CONST(fp); |
| 1403 |
PUSH_FPR(fp); |
| 1404 |
in(STWI, O6, SL(64, 128), rFIRST(fp)); |
| 1405 |
in(LDFI, O6, SL(64, 128), fFIRST(fp)); |
| 1406 |
in(FITOS, fFIRST(fp), fFIRST(fp)); |
| 1407 |
POP_GPR(fp); |
| 1408 |
break; |
| 1409 |
case OP_CVFI: |
| 1410 |
MAYBE_EMIT_CONST(fp); |
| 1411 |
PUSH_GPR(fp); |
| 1412 |
in(FSTOI, fFIRST(fp), fFIRST(fp)); |
| 1413 |
in(STFI, O6, SL(64, 128), fFIRST(fp)); |
| 1414 |
in(LDUWI, O6, SL(64, 128), rFIRST(fp)); |
| 1415 |
POP_FPR(fp); |
| 1416 |
break; |
| 1417 |
} |
| 1418 |
if (sp->op != OP_CONST) { |
| 1419 |
fp->cached_const = NULL; |
| 1420 |
end_emit(fp); |
| 1421 |
} else { |
| 1422 |
fp->cached_const = sp; |
| 1423 |
if (sp->dst_reg_flags & REG_FLAGS_FLOAT) { |
| 1424 |
PUSH_FPR(fp); |
| 1425 |
} else { |
| 1426 |
PUSH_GPR(fp); |
| 1427 |
} |
| 1428 |
} |
| 1429 |
end_emit(fp); |
| 1430 |
} |
| 1431 |
|
| 1432 |
static void free_source_insns(struct func_info * const fp) |
| 1433 |
{ |
| 1434 |
struct src_insn *sp = fp->first->next; |
| 1435 |
|
| 1436 |
while (sp) { |
| 1437 |
struct src_insn *next = sp->next; |
| 1438 |
Z_Free(sp); |
| 1439 |
sp = next; |
| 1440 |
} |
| 1441 |
} |
| 1442 |
|
| 1443 |
static void compile_function(vm_t *vm, struct func_info * const fp) |
| 1444 |
{ |
| 1445 |
struct src_insn *sp; |
| 1446 |
|
| 1447 |
analyze_function(fp); |
| 1448 |
|
| 1449 |
fp->gpr_pos = L0; |
| 1450 |
fp->fpr_pos = F0; |
| 1451 |
fp->insn_index = 0; |
| 1452 |
|
| 1453 |
fp->stack_space = SL(64, 128); |
| 1454 |
fp->cached_const = NULL; |
| 1455 |
|
| 1456 |
sp = fp->first; |
| 1457 |
while ((sp = sp->next) != NULL) |
| 1458 |
compile_one_insn(vm, fp, sp); |
| 1459 |
|
| 1460 |
free_source_insns(fp); |
| 1461 |
} |
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
static void emit_vm_thunk(struct func_info * const fp) |
| 1469 |
{ |
| 1470 |
|
| 1471 |
start_emit(fp, THUNK_ICOUNT); |
| 1472 |
|
| 1473 |
in(OR, G0, O0, rVMDATA); |
| 1474 |
in(OR, G0, O1, rPSTACK); |
| 1475 |
in(OR, G0, O2, rDATABASE); |
| 1476 |
in(BA, +4*17); |
| 1477 |
in(OR, G0, O3, rDATAMASK); |
| 1478 |
|
| 1479 |
|
| 1480 |
#define CALL_THUNK_INSN_OFFSET 5 |
| 1481 |
in(SAVEI, O6, -SL(64, 128), O6); |
| 1482 |
|
| 1483 |
in(OR, G0, rVMDATA, L0); |
| 1484 |
in(OR, G0, rPSTACK, L1); |
| 1485 |
in(OR, G0, rDATABASE, L2); |
| 1486 |
in(OR, G0, rDATAMASK, L3); |
| 1487 |
|
| 1488 |
in(OR, G0, I0, O0); |
| 1489 |
in(OR, G0, I1, O1); |
| 1490 |
in(JMPL, I3, G0, O7); |
| 1491 |
in(OR, G0, I2, O2); |
| 1492 |
|
| 1493 |
in(OR, G0, L0, rVMDATA); |
| 1494 |
in(OR, G0, L1, rPSTACK); |
| 1495 |
in(OR, G0, L2, rDATABASE); |
| 1496 |
in(OR, G0, L3, rDATAMASK); |
| 1497 |
|
| 1498 |
in(JMPLI, I7, 8, G0); |
| 1499 |
in(RESTORE, O0, G0, O0); |
| 1500 |
|
| 1501 |
end_emit(fp); |
| 1502 |
} |
| 1503 |
|
| 1504 |
static void sparc_compute_code(vm_t *vm, struct func_info * const fp) |
| 1505 |
{ |
| 1506 |
struct dst_insn *dp = fp->dst_first; |
| 1507 |
unsigned int *code_now, *code_begin; |
| 1508 |
unsigned char *data_and_code; |
| 1509 |
unsigned int code_length; |
| 1510 |
int code_insns = 0, off; |
| 1511 |
struct data_hunk *dhp; |
| 1512 |
struct jump_insn *jp; |
| 1513 |
vm_data_t *data; |
| 1514 |
|
| 1515 |
while (dp) { |
| 1516 |
code_insns += dp->length; |
| 1517 |
dp = dp->next; |
| 1518 |
} |
| 1519 |
|
| 1520 |
code_length = (sizeof(vm_data_t) + |
| 1521 |
(fp->data_num * sizeof(unsigned int)) + |
| 1522 |
(code_insns * sizeof(unsigned int))); |
| 1523 |
|
| 1524 |
data_and_code = mmap(NULL, code_length, PROT_READ | PROT_WRITE, |
| 1525 |
MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
| 1526 |
if (data_and_code == MAP_FAILED) |
| 1527 |
DIE("Not enough memory"); |
| 1528 |
|
| 1529 |
code_now = code_begin = (unsigned int *) |
| 1530 |
(data_and_code + VM_Data_Offset(data[fp->data_num])); |
| 1531 |
|
| 1532 |
dp = fp->dst_first; |
| 1533 |
while (dp) { |
| 1534 |
int i_count = dp->i_count; |
| 1535 |
|
| 1536 |
if (i_count != THUNK_ICOUNT) { |
| 1537 |
if (!fp->dst_by_i_count[i_count]) |
| 1538 |
fp->dst_by_i_count[i_count] = (void *) code_now; |
| 1539 |
} |
| 1540 |
if (!dp->jump) { |
| 1541 |
memcpy(code_now, &dp->code[0], dp->length * sizeof(unsigned int)); |
| 1542 |
code_now += dp->length; |
| 1543 |
} else { |
| 1544 |
int i; |
| 1545 |
|
| 1546 |
dp->jump->parent = (void *) code_now; |
| 1547 |
|
| 1548 |
for (i = 0; i < dp->length; i++) |
| 1549 |
code_now[i] = SPARC_NOP; |
| 1550 |
code_now += dp->length; |
| 1551 |
} |
| 1552 |
|
| 1553 |
dp = dp->next; |
| 1554 |
} |
| 1555 |
|
| 1556 |
jp = fp->jump_first; |
| 1557 |
while (jp) { |
| 1558 |
unsigned int *from = (void *) jp->parent; |
| 1559 |
unsigned int *to = (void *) fp->dst_by_i_count[jp->jump_dest_insn]; |
| 1560 |
signed int disp = (to - from); |
| 1561 |
|
| 1562 |
*from = IN(jp->jump_iname, disp << 2); |
| 1563 |
|
| 1564 |
jp = jp->next; |
| 1565 |
} |
| 1566 |
|
| 1567 |
vm->codeBase = data_and_code; |
| 1568 |
vm->codeLength = code_length; |
| 1569 |
|
| 1570 |
data = (vm_data_t *) data_and_code; |
| 1571 |
data->CallThunk = code_begin + CALL_THUNK_INSN_OFFSET; |
| 1572 |
data->AsmCall = asmcall; |
| 1573 |
data->BlockCopy = blockcopy; |
| 1574 |
data->iPointers = (unsigned int *) vm->instructionPointers; |
| 1575 |
data->dataLength = VM_Data_Offset(data[fp->data_num]); |
| 1576 |
data->codeLength = (code_now - code_begin) * sizeof(unsigned int); |
| 1577 |
data->ErrJump = ErrJump; |
| 1578 |
|
| 1579 |
#if 0 |
| 1580 |
{ |
| 1581 |
unsigned int *insn = code_begin; |
| 1582 |
int i; |
| 1583 |
|
| 1584 |
Com_Printf("INSN DUMP\n"); |
| 1585 |
for (i = 0; i < data->codeLength / 4; i+= 8) { |
| 1586 |
Com_Printf("\t.word\t0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x\n", |
| 1587 |
insn[i + 0], insn[i + 1], |
| 1588 |
insn[i + 2], insn[i + 3], |
| 1589 |
insn[i + 4], insn[i + 5], |
| 1590 |
insn[i + 6], insn[i + 7]); |
| 1591 |
} |
| 1592 |
} |
| 1593 |
#endif |
| 1594 |
|
| 1595 |
dhp = fp->data_first; |
| 1596 |
off = 0; |
| 1597 |
while (dhp) { |
| 1598 |
struct data_hunk *next = dhp->next; |
| 1599 |
int i; |
| 1600 |
|
| 1601 |
for (i = 0; i < dhp->count; i++) |
| 1602 |
data->data[off + i] = dhp->data[i]; |
| 1603 |
|
| 1604 |
off += dhp->count; |
| 1605 |
|
| 1606 |
Z_Free(dhp); |
| 1607 |
|
| 1608 |
dhp = next; |
| 1609 |
} |
| 1610 |
fp->data_first = NULL; |
| 1611 |
fp->data_num = 0; |
| 1612 |
|
| 1613 |
dp = fp->dst_first; |
| 1614 |
while (dp) { |
| 1615 |
struct dst_insn *next = dp->next; |
| 1616 |
if (dp->jump) |
| 1617 |
Z_Free(dp->jump); |
| 1618 |
Z_Free(dp); |
| 1619 |
dp = next; |
| 1620 |
} |
| 1621 |
fp->dst_first = fp->dst_last = NULL; |
| 1622 |
} |
| 1623 |
|
| 1624 |
void VM_Compile(vm_t *vm, vmHeader_t *header) |
| 1625 |
{ |
| 1626 |
struct func_info fi; |
| 1627 |
unsigned char *code; |
| 1628 |
int i_count, pc, i; |
| 1629 |
|
| 1630 |
memset(&fi, 0, sizeof(fi)); |
| 1631 |
|
| 1632 |
fi.first = Z_Malloc(sizeof(struct src_insn)); |
| 1633 |
fi.first->next = NULL; |
| 1634 |
|
| 1635 |
#ifdef __arch64__ |
| 1636 |
Z_Free(vm->instructionPointers); |
| 1637 |
vm->instructionPointers = Z_Malloc(header->instructionCount * |
| 1638 |
sizeof(void *)); |
| 1639 |
#endif |
| 1640 |
|
| 1641 |
fi.dst_by_i_count = (struct dst_insn **) vm->instructionPointers; |
| 1642 |
memset(fi.dst_by_i_count, 0, header->instructionCount * sizeof(void *)); |
| 1643 |
|
| 1644 |
vm->compiled = qfalse; |
| 1645 |
|
| 1646 |
emit_vm_thunk(&fi); |
| 1647 |
|
| 1648 |
code = (unsigned char *) header + header->codeOffset; |
| 1649 |
pc = 0; |
| 1650 |
|
| 1651 |
for (i_count = 0; i_count < header->instructionCount; i_count++) { |
| 1652 |
unsigned char opi, op = code[pc++]; |
| 1653 |
struct src_insn *sp; |
| 1654 |
|
| 1655 |
if (op == OP_CALL || op == OP_BLOCK_COPY) |
| 1656 |
fi.has_call = 1; |
| 1657 |
opi = vm_opInfo[op]; |
| 1658 |
if (op == OP_CVIF || op == OP_CVFI || |
| 1659 |
(op == OP_LEAVE && (opi & opArgF))) |
| 1660 |
fi.need_float_tmp = 1; |
| 1661 |
|
| 1662 |
if (op == OP_ENTER) { |
| 1663 |
if (fi.first->next) |
| 1664 |
compile_function(vm, &fi); |
| 1665 |
fi.first->next = NULL; |
| 1666 |
fi.last = fi.first; |
| 1667 |
fi.has_call = fi.need_float_tmp = 0; |
| 1668 |
} |
| 1669 |
|
| 1670 |
sp = Z_Malloc(sizeof(*sp)); |
| 1671 |
sp->op = op; |
| 1672 |
sp->i_count = i_count; |
| 1673 |
sp->arg.i = 0; |
| 1674 |
sp->next = NULL; |
| 1675 |
|
| 1676 |
if (vm_opInfo[op] & opImm4) { |
| 1677 |
union { |
| 1678 |
unsigned char b[4]; |
| 1679 |
unsigned int i; |
| 1680 |
} c = { { code[ pc + 3 ], code[ pc + 2 ], |
| 1681 |
code[ pc + 1 ], code[ pc + 0 ] }, }; |
| 1682 |
|
| 1683 |
sp->arg.i = c.i; |
| 1684 |
pc += 4; |
| 1685 |
} else if (vm_opInfo[op] & opImm1) { |
| 1686 |
sp->arg.b = code[pc++]; |
| 1687 |
} |
| 1688 |
|
| 1689 |
fi.last->next = sp; |
| 1690 |
fi.last = sp; |
| 1691 |
} |
| 1692 |
compile_function(vm, &fi); |
| 1693 |
|
| 1694 |
Z_Free(fi.first); |
| 1695 |
|
| 1696 |
memset(fi.dst_by_i_count, 0, header->instructionCount * sizeof(void *)); |
| 1697 |
sparc_compute_code(vm, &fi); |
| 1698 |
|
| 1699 |
for (i = 0; i < header->instructionCount; i++) { |
| 1700 |
if (!fi.dst_by_i_count[i]) { |
| 1701 |
Com_Printf(S_COLOR_RED "Pointer %d not initialized !\n", i); |
| 1702 |
DIE("sparc JIT bug"); |
| 1703 |
} |
| 1704 |
} |
| 1705 |
|
| 1706 |
if (mprotect(vm->codeBase, vm->codeLength, PROT_READ|PROT_EXEC)) { |
| 1707 |
VM_Destroy_Compiled(vm); |
| 1708 |
DIE("mprotect failed"); |
| 1709 |
} |
| 1710 |
|
| 1711 |
vm->destroy = VM_Destroy_Compiled; |
| 1712 |
vm->compiled = qtrue; |
| 1713 |
} |
| 1714 |
|
| 1715 |
int VM_CallCompiled(vm_t *vm, int *args) |
| 1716 |
{ |
| 1717 |
vm_data_t *vm_dataAndCode = (void *) vm->codeBase; |
| 1718 |
int programStack = vm->programStack; |
| 1719 |
int stackOnEntry = programStack; |
| 1720 |
byte *image = vm->dataBase; |
| 1721 |
int *argPointer; |
| 1722 |
int retVal; |
| 1723 |
|
| 1724 |
currentVM = vm; |
| 1725 |
|
| 1726 |
vm->currentlyInterpreting = qtrue; |
| 1727 |
|
| 1728 |
programStack -= ( 8 + 4 * MAX_VMMAIN_ARGS ); |
| 1729 |
argPointer = (int *)&image[ programStack + 8 ]; |
| 1730 |
memcpy( argPointer, args, 4 * MAX_VMMAIN_ARGS ); |
| 1731 |
argPointer[-1] = 0; |
| 1732 |
argPointer[-2] = -1; |
| 1733 |
|
| 1734 |
|
| 1735 |
{ |
| 1736 |
int (*entry)(void *, int, void *, int); |
| 1737 |
entry = (void *)(vm->codeBase + vm_dataAndCode->dataLength); |
| 1738 |
retVal = entry(vm->codeBase, programStack, vm->dataBase, vm->dataMask); |
| 1739 |
} |
| 1740 |
|
| 1741 |
vm->programStack = stackOnEntry; |
| 1742 |
vm->currentlyInterpreting = qfalse; |
| 1743 |
|
| 1744 |
return retVal; |
| 1745 |
} |
| 1746 |
|
| 1747 |
#endif |