2020-10-13 09:58:51 +02:00
|
|
|
(*
|
|
|
|
BSD 2-Clause License
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
Copyright (c) 2020-2021, Anton Krotov
|
2020-10-13 09:58:51 +02:00
|
|
|
All rights reserved.
|
|
|
|
*)
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
MODULE RVMxI;
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
IMPORT
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
PROG, WR := WRITER, IL, CHL := CHUNKLISTS, REG, UTILS, STRINGS, ERRORS, TARGETS;
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
CONST
|
|
|
|
|
|
|
|
LTypes = 0;
|
|
|
|
LStrings = 1;
|
|
|
|
LGlobal = 2;
|
|
|
|
LHeap = 3;
|
|
|
|
LStack = 4;
|
|
|
|
|
|
|
|
numGPRs = 3;
|
|
|
|
|
|
|
|
R0 = 0; R1 = 1;
|
|
|
|
BP = 3; SP = 4;
|
|
|
|
|
|
|
|
ACC = R0;
|
|
|
|
|
|
|
|
GPRs = {0 .. 2} + {5 .. numGPRs + 1};
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
opSTOP = 0; opRET = 1; opENTER = 2; opNEG = 3; opNOT = 4; opNOP = 5;
|
|
|
|
opXCHG = 6; opLDB = 7; opLDH = 8; opLDW = 9; opPUSH = 10; opPUSHC = 11;
|
|
|
|
opPOP = 12; opLABEL = 13; opLEA = 14; opLLA = 15;
|
|
|
|
opLDD = 16; (* 17, 18 *)
|
|
|
|
opJMP = 19; opCALL = 20; opCALLI = 21;
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
opMOV = 22; opMUL = 24; opADD = 26; opSUB = 28; opDIV = 30; opMOD = 32;
|
2021-06-15 19:33:16 +02:00
|
|
|
opSTB = 34; opSTH = 36; opSTW = 38; opSTD = 40; (* 42, 44 *)
|
|
|
|
opAND = 46; opOR = 48; opXOR = 50; opASR = 52; opLSR = 54;
|
|
|
|
opLSL = 56; opROR = 58; (* 60, 62 *) opCMP = 64;
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
opMOVC = 23; opMULC = 25; opADDC = 27; opSUBC = 29; opDIVC = 31; opMODC = 33;
|
2021-06-15 19:33:16 +02:00
|
|
|
opSTBC = 35; opSTHC = 37; opSTWC = 39; opSTDC = 41; (* 43, 45 *)
|
|
|
|
opANDC = 47; opORC = 49; opXORC = 51; opASRC = 53; opLSRC = 55;
|
|
|
|
opLSLC = 57; opRORC = 59; (* 61, 63 *) opCMPC = 65;
|
2020-10-13 09:58:51 +02:00
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
opBIT = 66; opSYSCALL = 67; opJBT = 68; opADDRC = 69;
|
2020-10-13 09:58:51 +02:00
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
opJEQ = 70; opJNE = 71; opJLT = 72; opJGE = 73; opJGT = 74; opJLE = 75;
|
|
|
|
opSEQ = 76; opSNE = 77; opSLT = 78; opSGE = 79; opSGT = 80; opSLE = 81;
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
VAR
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
R: REG.REGS; count, szWord: INTEGER;
|
|
|
|
|
|
|
|
ldr, str: PROCEDURE (r1, r2: INTEGER);
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE OutByte (n: BYTE);
|
|
|
|
BEGIN
|
|
|
|
WR.WriteByte(n);
|
|
|
|
INC(count)
|
|
|
|
END OutByte;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE OutInt (n: INTEGER);
|
|
|
|
BEGIN
|
2021-06-15 19:33:16 +02:00
|
|
|
IF szWord = 8 THEN
|
|
|
|
WR.Write64LE(n);
|
|
|
|
INC(count, 8)
|
|
|
|
ELSE (* szWord = 4 *)
|
|
|
|
WR.Write32LE(n);
|
|
|
|
INC(count, 4)
|
|
|
|
END
|
2020-10-13 09:58:51 +02:00
|
|
|
END OutInt;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE Emit (op, par1, par2: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
OutInt(op);
|
|
|
|
OutInt(par1);
|
|
|
|
OutInt(par2)
|
|
|
|
END Emit;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE drop;
|
|
|
|
BEGIN
|
|
|
|
REG.Drop(R)
|
|
|
|
END drop;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE GetAnyReg (): INTEGER;
|
|
|
|
RETURN REG.GetAnyReg(R)
|
|
|
|
END GetAnyReg;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE GetAcc;
|
|
|
|
BEGIN
|
|
|
|
ASSERT(REG.GetReg(R, ACC))
|
|
|
|
END GetAcc;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE UnOp (VAR r: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
REG.UnOp(R, r)
|
|
|
|
END UnOp;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE BinOp (VAR r1, r2: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
REG.BinOp(R, r1, r2)
|
|
|
|
END BinOp;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE PushAll (NumberOfParameters: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
REG.PushAll(R);
|
|
|
|
DEC(R.pushed, NumberOfParameters)
|
|
|
|
END PushAll;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE push (r: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
Emit(opPUSH, r, 0)
|
|
|
|
END push;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE pop (r: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
Emit(opPOP, r, 0)
|
|
|
|
END pop;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE mov (r1, r2: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
Emit(opMOV, r1, r2)
|
|
|
|
END mov;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE xchg (r1, r2: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
Emit(opXCHG, r1, r2)
|
|
|
|
END xchg;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE addrc (r, c: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
Emit(opADDC, r, c)
|
|
|
|
END addrc;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE subrc (r, c: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
Emit(opSUBC, r, c)
|
|
|
|
END subrc;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE movrc (r, c: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
Emit(opMOVC, r, c)
|
|
|
|
END movrc;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE pushc (c: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
Emit(opPUSHC, c, 0)
|
|
|
|
END pushc;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE add (r1, r2: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
Emit(opADD, r1, r2)
|
|
|
|
END add;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE sub (r1, r2: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
Emit(opSUB, r1, r2)
|
|
|
|
END sub;
|
|
|
|
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
PROCEDURE ldr64 (r1, r2: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
Emit(opLDD, r2 * 256 + r1, 0)
|
|
|
|
END ldr64;
|
|
|
|
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
PROCEDURE ldr32 (r1, r2: INTEGER);
|
|
|
|
BEGIN
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opLDW, r2 * 256 + r1, 0)
|
2020-10-13 09:58:51 +02:00
|
|
|
END ldr32;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE ldr16 (r1, r2: INTEGER);
|
|
|
|
BEGIN
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opLDH, r2 * 256 + r1, 0)
|
2020-10-13 09:58:51 +02:00
|
|
|
END ldr16;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE ldr8 (r1, r2: INTEGER);
|
|
|
|
BEGIN
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opLDB, r2 * 256 + r1, 0)
|
2020-10-13 09:58:51 +02:00
|
|
|
END ldr8;
|
|
|
|
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
PROCEDURE str64 (r1, r2: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
Emit(opSTD, r1 * 256 + r2, 0)
|
|
|
|
END str64;
|
|
|
|
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
PROCEDURE str32 (r1, r2: INTEGER);
|
|
|
|
BEGIN
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opSTW, r1 * 256 + r2, 0)
|
2020-10-13 09:58:51 +02:00
|
|
|
END str32;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE str16 (r1, r2: INTEGER);
|
|
|
|
BEGIN
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opSTH, r1 * 256 + r2, 0)
|
2020-10-13 09:58:51 +02:00
|
|
|
END str16;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE str8 (r1, r2: INTEGER);
|
|
|
|
BEGIN
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opSTB, r1 * 256 + r2, 0)
|
2020-10-13 09:58:51 +02:00
|
|
|
END str8;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE GlobalAdr (r, offset: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
Emit(opLEA, r + 256 * LGlobal, offset)
|
|
|
|
END GlobalAdr;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE StrAdr (r, offset: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
Emit(opLEA, r + 256 * LStrings, offset)
|
|
|
|
END StrAdr;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE ProcAdr (r, label: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
Emit(opLLA, r, label)
|
|
|
|
END ProcAdr;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE jnz (r, label: INTEGER);
|
|
|
|
BEGIN
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opCMPC, r, 0);
|
|
|
|
Emit(opJNE, label, 0)
|
2020-10-13 09:58:51 +02:00
|
|
|
END jnz;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE CallRTL (proc, par: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
Emit(opCALL, IL.codes.rtl[proc], 0);
|
2021-06-15 19:33:16 +02:00
|
|
|
addrc(SP, par * szWord)
|
2020-10-13 09:58:51 +02:00
|
|
|
END CallRTL;
|
|
|
|
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
PROCEDURE jcc (cc: INTEGER): INTEGER;
|
|
|
|
BEGIN
|
|
|
|
CASE cc OF
|
|
|
|
|IL.opEQ, IL.opEQC: cc := opJEQ
|
|
|
|
|IL.opNE, IL.opNEC: cc := opJNE
|
|
|
|
|IL.opLT, IL.opLTC: cc := opJLT
|
|
|
|
|IL.opLE, IL.opLEC: cc := opJLE
|
|
|
|
|IL.opGT, IL.opGTC: cc := opJGT
|
|
|
|
|IL.opGE, IL.opGEC: cc := opJGE
|
|
|
|
END
|
|
|
|
RETURN cc
|
|
|
|
END jcc;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE shift1 (op, param: INTEGER);
|
|
|
|
VAR
|
|
|
|
r1, r2: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
r2 := GetAnyReg();
|
|
|
|
Emit(opMOVC, r2, param);
|
|
|
|
BinOp(r1, r2);
|
|
|
|
Emit(op, r2, r1);
|
|
|
|
mov(r1, r2);
|
|
|
|
drop
|
|
|
|
END shift1;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE shift (op: INTEGER);
|
2020-10-13 09:58:51 +02:00
|
|
|
VAR
|
2021-06-15 19:33:16 +02:00
|
|
|
r1, r2: INTEGER;
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
BEGIN
|
2021-06-15 19:33:16 +02:00
|
|
|
BinOp(r1, r2);
|
|
|
|
Emit(op, r1, r2);
|
|
|
|
drop
|
|
|
|
END shift;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE translate (szWord: INTEGER);
|
|
|
|
VAR
|
|
|
|
cmd, next: IL.COMMAND;
|
|
|
|
|
|
|
|
opcode, param1, param2, r1, r2, r3,
|
|
|
|
a, b, label, opLD, opST, opSTC: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
IF szWord = 8 THEN
|
|
|
|
opLD := opLDD;
|
|
|
|
opST := opSTD;
|
|
|
|
opSTC := opSTDC
|
|
|
|
ELSE
|
|
|
|
opLD := opLDW;
|
|
|
|
opST := opSTW;
|
|
|
|
opSTC := opSTWC
|
|
|
|
END;
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
cmd := IL.codes.commands.first(IL.COMMAND);
|
|
|
|
|
|
|
|
WHILE cmd # NIL DO
|
|
|
|
|
|
|
|
param1 := cmd.param1;
|
|
|
|
param2 := cmd.param2;
|
|
|
|
opcode := cmd.opcode;
|
|
|
|
|
|
|
|
CASE opcode OF
|
|
|
|
|
|
|
|
|IL.opJMP:
|
|
|
|
Emit(opJMP, param1, 0)
|
|
|
|
|
|
|
|
|IL.opLABEL:
|
|
|
|
Emit(opLABEL, param1, 0)
|
|
|
|
|
|
|
|
|IL.opCALL:
|
|
|
|
Emit(opCALL, param1, 0)
|
|
|
|
|
|
|
|
|IL.opCALLP:
|
|
|
|
UnOp(r1);
|
|
|
|
Emit(opCALLI, r1, 0);
|
|
|
|
drop;
|
|
|
|
ASSERT(R.top = -1)
|
|
|
|
|
|
|
|
|IL.opPUSHC:
|
|
|
|
pushc(param2)
|
|
|
|
|
|
|
|
|IL.opCLEANUP:
|
|
|
|
IF param2 # 0 THEN
|
2021-06-15 19:33:16 +02:00
|
|
|
addrc(SP, param2 * szWord)
|
2020-10-13 09:58:51 +02:00
|
|
|
END
|
|
|
|
|
|
|
|
|IL.opNOP, IL.opAND, IL.opOR:
|
|
|
|
|
|
|
|
|IL.opSADR:
|
|
|
|
StrAdr(GetAnyReg(), param2)
|
|
|
|
|
|
|
|
|IL.opGADR:
|
|
|
|
GlobalAdr(GetAnyReg(), param2)
|
|
|
|
|
|
|
|
|IL.opLADR:
|
2021-06-15 19:33:16 +02:00
|
|
|
param2 := param2 * szWord;
|
|
|
|
next := cmd.next(IL.COMMAND);
|
|
|
|
IF ((next.opcode = IL.opSAVE) OR (next.opcode = IL.opSAVEF)) & (szWord = 8) OR (next.opcode = IL.opSAVE64) THEN
|
|
|
|
UnOp(r1);
|
|
|
|
Emit(opSTD, BP * 256 + r1, param2);
|
|
|
|
drop;
|
|
|
|
cmd := next
|
|
|
|
ELSIF ((next.opcode = IL.opSAVE) OR (next.opcode = IL.opSAVEF)) & (szWord = 4) OR (next.opcode = IL.opSAVE32) THEN
|
|
|
|
UnOp(r1);
|
|
|
|
Emit(opSTW, BP * 256 + r1, param2);
|
|
|
|
drop;
|
|
|
|
cmd := next
|
|
|
|
ELSIF next.opcode = IL.opSAVE16 THEN
|
|
|
|
UnOp(r1);
|
|
|
|
Emit(opSTH, BP * 256 + r1, param2);
|
|
|
|
drop;
|
|
|
|
cmd := next
|
|
|
|
ELSIF next.opcode = IL.opSAVE8 THEN
|
|
|
|
UnOp(r1);
|
|
|
|
Emit(opSTB, BP * 256 + r1, param2);
|
|
|
|
drop;
|
|
|
|
cmd := next
|
|
|
|
ELSE
|
|
|
|
Emit(opADDRC, BP * 256 + GetAnyReg(), param2)
|
|
|
|
END
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opPARAM:
|
|
|
|
IF param2 = 1 THEN
|
|
|
|
UnOp(r1);
|
|
|
|
push(r1);
|
|
|
|
drop
|
|
|
|
ELSE
|
|
|
|
ASSERT(R.top + 1 <= param2);
|
|
|
|
PushAll(param2)
|
|
|
|
END
|
|
|
|
|
|
|
|
|IL.opONERR:
|
|
|
|
pushc(param2);
|
|
|
|
Emit(opJMP, param1, 0)
|
|
|
|
|
|
|
|
|IL.opPRECALL:
|
|
|
|
PushAll(0)
|
|
|
|
|
|
|
|
|IL.opRES, IL.opRESF:
|
|
|
|
ASSERT(R.top = -1);
|
|
|
|
GetAcc
|
|
|
|
|
|
|
|
|IL.opENTER:
|
|
|
|
ASSERT(R.top = -1);
|
|
|
|
Emit(opLABEL, param1, 0);
|
|
|
|
Emit(opENTER, param2, 0)
|
|
|
|
|
|
|
|
|IL.opLEAVE, IL.opLEAVER, IL.opLEAVEF:
|
|
|
|
IF opcode # IL.opLEAVE THEN
|
|
|
|
UnOp(r1);
|
|
|
|
IF r1 # ACC THEN
|
2021-06-15 19:33:16 +02:00
|
|
|
mov(ACC, r1)
|
2020-10-13 09:58:51 +02:00
|
|
|
END;
|
|
|
|
drop
|
|
|
|
END;
|
|
|
|
|
|
|
|
ASSERT(R.top = -1);
|
|
|
|
|
|
|
|
IF param1 > 0 THEN
|
|
|
|
mov(SP, BP)
|
|
|
|
END;
|
|
|
|
|
|
|
|
pop(BP);
|
|
|
|
|
|
|
|
Emit(opRET, 0, 0)
|
|
|
|
|
|
|
|
|IL.opLEAVEC:
|
|
|
|
Emit(opRET, 0, 0)
|
|
|
|
|
|
|
|
|IL.opCONST:
|
2021-06-15 19:33:16 +02:00
|
|
|
next := cmd.next(IL.COMMAND);
|
|
|
|
IF (next.opcode = IL.opPARAM) & (next.param2 = 1) THEN
|
|
|
|
pushc(param2);
|
|
|
|
cmd := next
|
|
|
|
ELSE
|
|
|
|
movrc(GetAnyReg(), param2)
|
|
|
|
END
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opDROP:
|
|
|
|
UnOp(r1);
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opSAVEC:
|
|
|
|
UnOp(r1);
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opSTC, r1, param2);
|
2020-10-13 09:58:51 +02:00
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opSAVE8C:
|
|
|
|
UnOp(r1);
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opSTBC, r1, param2 MOD 256);
|
2020-10-13 09:58:51 +02:00
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opSAVE16C:
|
|
|
|
UnOp(r1);
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opSTHC, r1, param2 MOD 65536);
|
2020-10-13 09:58:51 +02:00
|
|
|
drop
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
|IL.opSAVE, IL.opSAVEF:
|
|
|
|
BinOp(r2, r1);
|
|
|
|
str(r1, r2);
|
|
|
|
drop;
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opSAVE32:
|
2020-10-13 09:58:51 +02:00
|
|
|
BinOp(r2, r1);
|
|
|
|
str32(r1, r2);
|
|
|
|
drop;
|
|
|
|
drop
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
|IL.opSAVE64:
|
|
|
|
BinOp(r2, r1);
|
|
|
|
str64(r1, r2);
|
|
|
|
drop;
|
|
|
|
drop
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
|IL.opSAVEFI:
|
|
|
|
BinOp(r2, r1);
|
2021-06-15 19:33:16 +02:00
|
|
|
str(r2, r1);
|
2020-10-13 09:58:51 +02:00
|
|
|
drop;
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opSAVE8:
|
|
|
|
BinOp(r2, r1);
|
|
|
|
str8(r1, r2);
|
|
|
|
drop;
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opSAVE16:
|
|
|
|
BinOp(r2, r1);
|
|
|
|
str16(r1, r2);
|
|
|
|
drop;
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opGLOAD32:
|
|
|
|
r1 := GetAnyReg();
|
|
|
|
GlobalAdr(r1, param2);
|
|
|
|
ldr32(r1, r1)
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
|IL.opGLOAD64:
|
2020-10-13 09:58:51 +02:00
|
|
|
r1 := GetAnyReg();
|
2021-06-15 19:33:16 +02:00
|
|
|
GlobalAdr(r1, param2);
|
|
|
|
ldr64(r1, r1)
|
|
|
|
|
|
|
|
|IL.opVADR:
|
|
|
|
Emit(opLD, BP * 256 + GetAnyReg(), param2 * szWord)
|
|
|
|
|
|
|
|
|IL.opLLOAD32:
|
|
|
|
Emit(opLDW, BP * 256 + GetAnyReg(), param2 * szWord)
|
|
|
|
|
|
|
|
|IL.opLLOAD64:
|
|
|
|
Emit(opLDD, BP * 256 + GetAnyReg(), param2 * szWord)
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opVLOAD32:
|
|
|
|
r1 := GetAnyReg();
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opLD, BP * 256 + r1, param2 * szWord);
|
2020-10-13 09:58:51 +02:00
|
|
|
ldr32(r1, r1)
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
|IL.opVLOAD64:
|
|
|
|
r1 := GetAnyReg();
|
|
|
|
Emit(opLDD, BP * 256 + r1, param2 * szWord);
|
|
|
|
ldr64(r1, r1)
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
|IL.opGLOAD16:
|
|
|
|
r1 := GetAnyReg();
|
|
|
|
GlobalAdr(r1, param2);
|
|
|
|
ldr16(r1, r1)
|
|
|
|
|
|
|
|
|IL.opLLOAD16:
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opLDH, BP * 256 + GetAnyReg(), param2 * szWord)
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opVLOAD16:
|
|
|
|
r1 := GetAnyReg();
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opLD, BP * 256 + r1, param2 * szWord);
|
2020-10-13 09:58:51 +02:00
|
|
|
ldr16(r1, r1)
|
|
|
|
|
|
|
|
|IL.opGLOAD8:
|
|
|
|
r1 := GetAnyReg();
|
|
|
|
GlobalAdr(r1, param2);
|
|
|
|
ldr8(r1, r1)
|
|
|
|
|
|
|
|
|IL.opLLOAD8:
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opLDB, BP * 256 + GetAnyReg(), param2 * szWord)
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opVLOAD8:
|
|
|
|
r1 := GetAnyReg();
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opLD, BP * 256 + r1, param2 * szWord);
|
2020-10-13 09:58:51 +02:00
|
|
|
ldr8(r1, r1)
|
|
|
|
|
|
|
|
|IL.opLOAD8:
|
|
|
|
UnOp(r1);
|
|
|
|
ldr8(r1, r1)
|
|
|
|
|
|
|
|
|IL.opLOAD16:
|
|
|
|
UnOp(r1);
|
|
|
|
ldr16(r1, r1)
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
|IL.opLOAD32:
|
2020-10-13 09:58:51 +02:00
|
|
|
UnOp(r1);
|
|
|
|
ldr32(r1, r1)
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
|IL.opLOAD64:
|
|
|
|
UnOp(r1);
|
|
|
|
ldr64(r1, r1)
|
|
|
|
|
|
|
|
|IL.opLOADF:
|
|
|
|
UnOp(r1);
|
|
|
|
ldr(r1, r1)
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opUMINUS:
|
|
|
|
UnOp(r1);
|
|
|
|
Emit(opNEG, r1, 0)
|
|
|
|
|
|
|
|
|IL.opADD:
|
|
|
|
BinOp(r1, r2);
|
|
|
|
add(r1, r2);
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opSUB:
|
|
|
|
BinOp(r1, r2);
|
|
|
|
sub(r1, r2);
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opADDC:
|
|
|
|
UnOp(r1);
|
2021-06-15 19:33:16 +02:00
|
|
|
next := cmd.next(IL.COMMAND);
|
|
|
|
CASE next.opcode OF
|
|
|
|
|IL.opLOADF:
|
|
|
|
Emit(opLD, r1 * 256 + r1, param2);
|
|
|
|
cmd := next
|
|
|
|
|IL.opLOAD64:
|
|
|
|
Emit(opLDD, r1 * 256 + r1, param2);
|
|
|
|
cmd := next
|
|
|
|
|IL.opLOAD32:
|
|
|
|
Emit(opLDW, r1 * 256 + r1, param2);
|
|
|
|
cmd := next
|
|
|
|
|IL.opLOAD16:
|
|
|
|
Emit(opLDH, r1 * 256 + r1, param2);
|
|
|
|
cmd := next
|
|
|
|
|IL.opLOAD8:
|
|
|
|
Emit(opLDB, r1 * 256 + r1, param2);
|
|
|
|
cmd := next
|
|
|
|
ELSE
|
|
|
|
addrc(r1, param2)
|
|
|
|
END
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opSUBR:
|
|
|
|
UnOp(r1);
|
|
|
|
subrc(r1, param2)
|
|
|
|
|
|
|
|
|IL.opSUBL:
|
|
|
|
UnOp(r1);
|
|
|
|
subrc(r1, param2);
|
|
|
|
Emit(opNEG, r1, 0)
|
|
|
|
|
|
|
|
|IL.opMULC:
|
|
|
|
UnOp(r1);
|
|
|
|
Emit(opMULC, r1, param2)
|
|
|
|
|
|
|
|
|IL.opMUL:
|
|
|
|
BinOp(r1, r2);
|
|
|
|
Emit(opMUL, r1, r2);
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opDIV:
|
|
|
|
BinOp(r1, r2);
|
|
|
|
Emit(opDIV, r1, r2);
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opMOD:
|
|
|
|
BinOp(r1, r2);
|
|
|
|
Emit(opMOD, r1, r2);
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opDIVR:
|
|
|
|
UnOp(r1);
|
|
|
|
Emit(opDIVC, r1, param2)
|
|
|
|
|
|
|
|
|IL.opMODR:
|
|
|
|
UnOp(r1);
|
|
|
|
Emit(opMODC, r1, param2)
|
|
|
|
|
|
|
|
|IL.opDIVL:
|
|
|
|
UnOp(r1);
|
|
|
|
r2 := GetAnyReg();
|
|
|
|
movrc(r2, param2);
|
|
|
|
Emit(opDIV, r2, r1);
|
|
|
|
mov(r1, r2);
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opMODL:
|
|
|
|
UnOp(r1);
|
|
|
|
r2 := GetAnyReg();
|
|
|
|
movrc(r2, param2);
|
|
|
|
Emit(opMOD, r2, r1);
|
|
|
|
mov(r1, r2);
|
|
|
|
drop
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
|IL.opEQ .. IL.opGE, IL.opEQC .. IL.opGEC:
|
|
|
|
IF (IL.opEQ <= opcode) & (opcode <= IL.opGE) THEN
|
|
|
|
BinOp(r1, r2);
|
|
|
|
Emit(opCMP, r1, r2);
|
|
|
|
drop
|
|
|
|
ELSE
|
|
|
|
UnOp(r1);
|
|
|
|
Emit(opCMPC, r1, param2)
|
|
|
|
END;
|
|
|
|
next := cmd.next(IL.COMMAND);
|
|
|
|
IF next.opcode = IL.opJZ THEN
|
|
|
|
Emit(ORD(BITS(jcc(opcode)) / {0}), next.param1, 0);
|
|
|
|
cmd := next;
|
|
|
|
drop
|
|
|
|
ELSIF next.opcode = IL.opJNZ THEN
|
|
|
|
Emit(jcc(opcode), next.param1, 0);
|
|
|
|
cmd := next;
|
|
|
|
drop
|
|
|
|
ELSE
|
|
|
|
Emit(jcc(opcode) + 6, r1, 0)
|
|
|
|
END
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opJNZ1:
|
|
|
|
UnOp(r1);
|
|
|
|
jnz(r1, param1)
|
|
|
|
|
|
|
|
|IL.opJG:
|
|
|
|
UnOp(r1);
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opCMPC, r1, 0);
|
|
|
|
Emit(opJGT, param1, 0)
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opJNZ:
|
|
|
|
UnOp(r1);
|
|
|
|
jnz(r1, param1);
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opJZ:
|
|
|
|
UnOp(r1);
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opCMPC, r1, 0);
|
|
|
|
Emit(opJEQ, param1, 0);
|
2020-10-13 09:58:51 +02:00
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opMULS:
|
|
|
|
BinOp(r1, r2);
|
|
|
|
Emit(opAND, r1, r2);
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opMULSC:
|
|
|
|
UnOp(r1);
|
|
|
|
Emit(opANDC, r1, param2)
|
|
|
|
|
|
|
|
|IL.opDIVS:
|
|
|
|
BinOp(r1, r2);
|
|
|
|
Emit(opXOR, r1, r2);
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opDIVSC:
|
|
|
|
UnOp(r1);
|
|
|
|
Emit(opXORC, r1, param2)
|
|
|
|
|
|
|
|
|IL.opADDS:
|
|
|
|
BinOp(r1, r2);
|
|
|
|
Emit(opOR, r1, r2);
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opSUBS:
|
|
|
|
BinOp(r1, r2);
|
|
|
|
Emit(opNOT, r2, 0);
|
|
|
|
Emit(opAND, r1, r2);
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opADDSC:
|
|
|
|
UnOp(r1);
|
|
|
|
Emit(opORC, r1, param2)
|
|
|
|
|
|
|
|
|IL.opSUBSL:
|
|
|
|
UnOp(r1);
|
|
|
|
Emit(opNOT, r1, 0);
|
|
|
|
Emit(opANDC, r1, param2)
|
|
|
|
|
|
|
|
|IL.opSUBSR:
|
|
|
|
UnOp(r1);
|
|
|
|
Emit(opANDC, r1, ORD(-BITS(param2)))
|
|
|
|
|
|
|
|
|IL.opUMINS:
|
|
|
|
UnOp(r1);
|
|
|
|
Emit(opNOT, r1, 0)
|
|
|
|
|
|
|
|
|IL.opASR:
|
2021-06-15 19:33:16 +02:00
|
|
|
shift(opASR)
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opLSL:
|
2021-06-15 19:33:16 +02:00
|
|
|
shift(opLSL)
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opROR:
|
2021-06-15 19:33:16 +02:00
|
|
|
shift(opROR)
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opLSR:
|
2021-06-15 19:33:16 +02:00
|
|
|
shift(opLSR)
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opASR1:
|
2021-06-15 19:33:16 +02:00
|
|
|
shift1(opASR, param2)
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opLSL1:
|
2021-06-15 19:33:16 +02:00
|
|
|
shift1(opLSL, param2)
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opROR1:
|
2021-06-15 19:33:16 +02:00
|
|
|
shift1(opROR, param2)
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opLSR1:
|
2021-06-15 19:33:16 +02:00
|
|
|
shift1(opLSR, param2)
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opASR2:
|
|
|
|
UnOp(r1);
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opASRC, r1, param2 MOD (szWord * 8))
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opLSL2:
|
|
|
|
UnOp(r1);
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opLSLC, r1, param2 MOD (szWord * 8))
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opROR2:
|
|
|
|
UnOp(r1);
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opRORC, r1, param2 MOD (szWord * 8))
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opLSR2:
|
|
|
|
UnOp(r1);
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opLSRC, r1, param2 MOD (szWord * 8))
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opCHR:
|
|
|
|
UnOp(r1);
|
|
|
|
Emit(opANDC, r1, 255)
|
|
|
|
|
|
|
|
|IL.opWCHR:
|
|
|
|
UnOp(r1);
|
|
|
|
Emit(opANDC, r1, 65535)
|
|
|
|
|
|
|
|
|IL.opABS:
|
|
|
|
UnOp(r1);
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opCMPC, r1, 0);
|
|
|
|
label := IL.NewLabel();
|
|
|
|
Emit(opJGE, label, 0);
|
|
|
|
Emit(opNEG, r1, 0);
|
|
|
|
Emit(opLABEL, label, 0)
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opLEN:
|
|
|
|
UnOp(r1);
|
|
|
|
drop;
|
|
|
|
EXCL(R.regs, r1);
|
|
|
|
|
|
|
|
WHILE param2 > 0 DO
|
|
|
|
UnOp(r2);
|
|
|
|
drop;
|
|
|
|
DEC(param2)
|
|
|
|
END;
|
|
|
|
|
|
|
|
INCL(R.regs, r1);
|
|
|
|
ASSERT(REG.GetReg(R, r1))
|
|
|
|
|
|
|
|
|IL.opSWITCH:
|
|
|
|
UnOp(r1);
|
|
|
|
IF param2 = 0 THEN
|
|
|
|
r2 := ACC
|
|
|
|
ELSE
|
|
|
|
r2 := R1
|
|
|
|
END;
|
|
|
|
IF r1 # r2 THEN
|
|
|
|
ASSERT(REG.GetReg(R, r2));
|
|
|
|
ASSERT(REG.Exchange(R, r1, r2));
|
|
|
|
drop
|
|
|
|
END;
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opENDSW:
|
|
|
|
|
|
|
|
|IL.opCASEL:
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opCMPC, ACC, param1);
|
|
|
|
Emit(opJLT, param2, 0)
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opCASER:
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opCMPC, ACC, param1);
|
|
|
|
Emit(opJGT, param2, 0)
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opCASELR:
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opCMPC, ACC, param1);
|
|
|
|
IF param2 = cmd.param3 THEN
|
|
|
|
Emit(opJNE, param2, 0)
|
|
|
|
ELSE
|
|
|
|
Emit(opJLT, param2, 0);
|
|
|
|
Emit(opJGT, cmd.param3, 0)
|
|
|
|
END
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opSBOOL:
|
|
|
|
BinOp(r2, r1);
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opCMPC, r2, 0);
|
|
|
|
Emit(opSNE, r2, 0);
|
2020-10-13 09:58:51 +02:00
|
|
|
str8(r1, r2);
|
|
|
|
drop;
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opSBOOLC:
|
|
|
|
UnOp(r1);
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opSTBC, r1, ORD(param2 # 0));
|
2020-10-13 09:58:51 +02:00
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opINCC:
|
|
|
|
UnOp(r1);
|
|
|
|
r2 := GetAnyReg();
|
2021-06-15 19:33:16 +02:00
|
|
|
ldr(r2, r1);
|
2020-10-13 09:58:51 +02:00
|
|
|
addrc(r2, param2);
|
2021-06-15 19:33:16 +02:00
|
|
|
str(r1, r2);
|
2020-10-13 09:58:51 +02:00
|
|
|
drop;
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opINCCB, IL.opDECCB:
|
|
|
|
IF opcode = IL.opDECCB THEN
|
|
|
|
param2 := -param2
|
|
|
|
END;
|
|
|
|
UnOp(r1);
|
|
|
|
r2 := GetAnyReg();
|
|
|
|
ldr8(r2, r1);
|
|
|
|
addrc(r2, param2);
|
|
|
|
str8(r1, r2);
|
|
|
|
drop;
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opINCB, IL.opDECB:
|
|
|
|
BinOp(r2, r1);
|
|
|
|
r3 := GetAnyReg();
|
|
|
|
ldr8(r3, r1);
|
|
|
|
IF opcode = IL.opINCB THEN
|
|
|
|
add(r3, r2)
|
|
|
|
ELSE
|
|
|
|
sub(r3, r2)
|
|
|
|
END;
|
|
|
|
str8(r1, r3);
|
|
|
|
drop;
|
|
|
|
drop;
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opINC, IL.opDEC:
|
|
|
|
BinOp(r2, r1);
|
|
|
|
r3 := GetAnyReg();
|
2021-06-15 19:33:16 +02:00
|
|
|
ldr(r3, r1);
|
2020-10-13 09:58:51 +02:00
|
|
|
IF opcode = IL.opINC THEN
|
|
|
|
add(r3, r2)
|
|
|
|
ELSE
|
|
|
|
sub(r3, r2)
|
|
|
|
END;
|
2021-06-15 19:33:16 +02:00
|
|
|
str(r1, r3);
|
2020-10-13 09:58:51 +02:00
|
|
|
drop;
|
|
|
|
drop;
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opINCL, IL.opEXCL:
|
|
|
|
BinOp(r2, r1);
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opBIT, r2, r2);
|
|
|
|
r3 := GetAnyReg();
|
|
|
|
ldr(r3, r1);
|
2020-10-13 09:58:51 +02:00
|
|
|
IF opcode = IL.opINCL THEN
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opOR, r3, r2)
|
2020-10-13 09:58:51 +02:00
|
|
|
ELSE
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opNOT, r2, 0);
|
|
|
|
Emit(opAND, r3, r2)
|
2020-10-13 09:58:51 +02:00
|
|
|
END;
|
2021-06-15 19:33:16 +02:00
|
|
|
str(r1, r3);
|
|
|
|
drop;
|
2020-10-13 09:58:51 +02:00
|
|
|
drop;
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opINCLC, IL.opEXCLC:
|
|
|
|
UnOp(r1);
|
|
|
|
r2 := GetAnyReg();
|
2021-06-15 19:33:16 +02:00
|
|
|
ldr(r2, r1);
|
2020-10-13 09:58:51 +02:00
|
|
|
IF opcode = IL.opINCLC THEN
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opORC, r2, ORD({param2}))
|
2020-10-13 09:58:51 +02:00
|
|
|
ELSE
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opANDC, r2, ORD(-{param2}))
|
2020-10-13 09:58:51 +02:00
|
|
|
END;
|
2021-06-15 19:33:16 +02:00
|
|
|
str(r1, r2);
|
2020-10-13 09:58:51 +02:00
|
|
|
drop;
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opEQB, IL.opNEB:
|
|
|
|
BinOp(r1, r2);
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opCMPC, r1, 0);
|
|
|
|
Emit(opSNE, r1, 0);
|
|
|
|
Emit(opCMPC, r2, 0);
|
|
|
|
Emit(opSNE, r2, 0);
|
|
|
|
Emit(opCMP, r1, r2);
|
2020-10-13 09:58:51 +02:00
|
|
|
IF opcode = IL.opEQB THEN
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opSEQ, r1, 0)
|
2020-10-13 09:58:51 +02:00
|
|
|
ELSE
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opSNE, r1, 0)
|
2020-10-13 09:58:51 +02:00
|
|
|
END;
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opCHKBYTE:
|
|
|
|
BinOp(r1, r2);
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opCMPC, r1, 256);
|
|
|
|
Emit(opJBT, param1, 0)
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opCHKIDX:
|
|
|
|
UnOp(r1);
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opCMPC, r1, param2);
|
|
|
|
Emit(opJBT, param1, 0)
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opCHKIDX2:
|
|
|
|
BinOp(r1, r2);
|
|
|
|
IF param2 # -1 THEN
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opCMP, r2, r1);
|
|
|
|
Emit(opJBT, param1, 0)
|
2020-10-13 09:58:51 +02:00
|
|
|
END;
|
|
|
|
INCL(R.regs, r1);
|
|
|
|
DEC(R.top);
|
|
|
|
R.stk[R.top] := r2
|
|
|
|
|
|
|
|
|IL.opEQP, IL.opNEP:
|
|
|
|
ProcAdr(GetAnyReg(), param1);
|
|
|
|
BinOp(r1, r2);
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opCMP, r1, r2);
|
2020-10-13 09:58:51 +02:00
|
|
|
IF opcode = IL.opEQP THEN
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opSEQ, r1, 0)
|
2020-10-13 09:58:51 +02:00
|
|
|
ELSE
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opSNE, r1, 0)
|
2020-10-13 09:58:51 +02:00
|
|
|
END;
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opSAVEP:
|
|
|
|
UnOp(r1);
|
|
|
|
r2 := GetAnyReg();
|
|
|
|
ProcAdr(r2, param2);
|
2021-06-15 19:33:16 +02:00
|
|
|
str(r1, r2);
|
2020-10-13 09:58:51 +02:00
|
|
|
drop;
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opPUSHP:
|
|
|
|
ProcAdr(GetAnyReg(), param2)
|
|
|
|
|
|
|
|
|IL.opPUSHT:
|
|
|
|
UnOp(r1);
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opLD, r1 * 256 + GetAnyReg(), -szWord)
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opGET, IL.opGETC:
|
|
|
|
IF opcode = IL.opGET THEN
|
|
|
|
BinOp(r1, r2)
|
|
|
|
ELSIF opcode = IL.opGETC THEN
|
|
|
|
UnOp(r2);
|
|
|
|
r1 := GetAnyReg();
|
|
|
|
movrc(r1, param1)
|
|
|
|
END;
|
|
|
|
drop;
|
|
|
|
drop;
|
|
|
|
|
|
|
|
CASE param2 OF
|
|
|
|
|1: ldr8(r1, r1); str8(r2, r1)
|
|
|
|
|2: ldr16(r1, r1); str16(r2, r1)
|
|
|
|
|4: ldr32(r1, r1); str32(r2, r1)
|
2021-06-15 19:33:16 +02:00
|
|
|
|8: ldr64(r1, r1); str64(r2, r1)
|
2020-10-13 09:58:51 +02:00
|
|
|
END
|
|
|
|
|
|
|
|
|IL.opNOT:
|
|
|
|
UnOp(r1);
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opCMPC, r1, 0);
|
|
|
|
Emit(opSEQ, r1, 0)
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opORD:
|
|
|
|
UnOp(r1);
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opCMPC, r1, 0);
|
|
|
|
Emit(opSNE, r1, 0)
|
2020-10-13 09:58:51 +02:00
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
|IL.opMIN, IL.opMAX:
|
2020-10-13 09:58:51 +02:00
|
|
|
BinOp(r1, r2);
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opCMP, r1, r2);
|
|
|
|
label := IL.NewLabel();
|
|
|
|
IF opcode = IL.opMIN THEN
|
|
|
|
Emit(opJLE, label, 0)
|
|
|
|
ELSE
|
|
|
|
Emit(opJGE, label, 0)
|
|
|
|
END;
|
|
|
|
Emit(opMOV, r1, r2);
|
|
|
|
Emit(opLABEL, label, 0);
|
2020-10-13 09:58:51 +02:00
|
|
|
drop
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
|IL.opMINC, IL.opMAXC:
|
2020-10-13 09:58:51 +02:00
|
|
|
UnOp(r1);
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opCMPC, r1, param2);
|
|
|
|
label := IL.NewLabel();
|
|
|
|
IF opcode = IL.opMINC THEN
|
|
|
|
Emit(opJLE, label, 0)
|
|
|
|
ELSE
|
|
|
|
Emit(opJGE, label, 0)
|
|
|
|
END;
|
|
|
|
Emit(opMOVC, r1, param2);
|
|
|
|
Emit(opLABEL, label, 0)
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opIN:
|
|
|
|
BinOp(r1, r2);
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opBIT, r1, r1);
|
|
|
|
Emit(opAND, r1, r2);
|
|
|
|
Emit(opCMPC, r1, 0);
|
|
|
|
Emit(opSNE, r1, 0);
|
2020-10-13 09:58:51 +02:00
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opINL:
|
2021-06-15 19:33:16 +02:00
|
|
|
UnOp(r1);
|
|
|
|
Emit(opANDC, r1, ORD({param2}));
|
|
|
|
Emit(opCMPC, r1, 0);
|
|
|
|
Emit(opSNE, r1, 0)
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opINR:
|
|
|
|
UnOp(r1);
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opBIT, r1, r1);
|
|
|
|
Emit(opANDC, r1, param2);
|
|
|
|
Emit(opCMPC, r1, 0);
|
|
|
|
Emit(opSNE, r1, 0)
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opERR:
|
|
|
|
CallRTL(IL._error, 4)
|
|
|
|
|
|
|
|
|IL.opEQS .. IL.opGES:
|
|
|
|
PushAll(4);
|
|
|
|
pushc(opcode - IL.opEQS);
|
|
|
|
CallRTL(IL._strcmp, 5);
|
|
|
|
GetAcc
|
|
|
|
|
|
|
|
|IL.opEQSW .. IL.opGESW:
|
|
|
|
PushAll(4);
|
|
|
|
pushc(opcode - IL.opEQSW);
|
|
|
|
CallRTL(IL._strcmpw, 5);
|
|
|
|
GetAcc
|
|
|
|
|
|
|
|
|IL.opCOPY:
|
|
|
|
PushAll(2);
|
|
|
|
pushc(param2);
|
|
|
|
CallRTL(IL._move, 3)
|
|
|
|
|
|
|
|
|IL.opMOVE:
|
|
|
|
PushAll(3);
|
|
|
|
CallRTL(IL._move, 3)
|
|
|
|
|
|
|
|
|IL.opCOPYA:
|
|
|
|
PushAll(4);
|
|
|
|
pushc(param2);
|
|
|
|
CallRTL(IL._arrcpy, 5);
|
|
|
|
GetAcc
|
|
|
|
|
|
|
|
|IL.opCOPYS:
|
|
|
|
PushAll(4);
|
|
|
|
pushc(param2);
|
|
|
|
CallRTL(IL._strcpy, 5)
|
|
|
|
|
|
|
|
|IL.opROT:
|
|
|
|
PushAll(0);
|
|
|
|
mov(ACC, SP);
|
|
|
|
push(ACC);
|
|
|
|
pushc(param2);
|
|
|
|
CallRTL(IL._rot, 2)
|
|
|
|
|
|
|
|
|IL.opLENGTH:
|
|
|
|
PushAll(2);
|
|
|
|
CallRTL(IL._length, 2);
|
|
|
|
GetAcc
|
|
|
|
|
|
|
|
|IL.opLENGTHW:
|
|
|
|
PushAll(2);
|
|
|
|
CallRTL(IL._lengthw, 2);
|
|
|
|
GetAcc
|
|
|
|
|
|
|
|
|IL.opSAVES:
|
|
|
|
UnOp(r2);
|
|
|
|
REG.PushAll_1(R);
|
|
|
|
r1 := GetAnyReg();
|
|
|
|
StrAdr(r1, param2);
|
|
|
|
push(r1);
|
|
|
|
drop;
|
|
|
|
push(r2);
|
|
|
|
drop;
|
|
|
|
pushc(param1);
|
|
|
|
CallRTL(IL._move, 3)
|
|
|
|
|
|
|
|
|IL.opRSET:
|
|
|
|
PushAll(2);
|
|
|
|
CallRTL(IL._set, 2);
|
|
|
|
GetAcc
|
|
|
|
|
|
|
|
|IL.opRSETR:
|
|
|
|
PushAll(1);
|
|
|
|
pushc(param2);
|
|
|
|
CallRTL(IL._set, 2);
|
|
|
|
GetAcc
|
|
|
|
|
|
|
|
|IL.opRSETL:
|
|
|
|
UnOp(r1);
|
|
|
|
REG.PushAll_1(R);
|
|
|
|
pushc(param2);
|
|
|
|
push(r1);
|
|
|
|
drop;
|
|
|
|
CallRTL(IL._set, 2);
|
|
|
|
GetAcc
|
|
|
|
|
|
|
|
|IL.opRSET1:
|
|
|
|
PushAll(1);
|
|
|
|
CallRTL(IL._set1, 1);
|
|
|
|
GetAcc
|
|
|
|
|
|
|
|
|IL.opNEW:
|
|
|
|
PushAll(1);
|
2021-06-15 19:33:16 +02:00
|
|
|
INC(param2, szWord);
|
|
|
|
ASSERT(UTILS.Align(param2, szWord));
|
2020-10-13 09:58:51 +02:00
|
|
|
pushc(param2);
|
|
|
|
pushc(param1);
|
|
|
|
CallRTL(IL._new, 3)
|
|
|
|
|
|
|
|
|IL.opTYPEGP:
|
|
|
|
UnOp(r1);
|
|
|
|
PushAll(0);
|
|
|
|
push(r1);
|
|
|
|
pushc(param2);
|
|
|
|
CallRTL(IL._guard, 2);
|
|
|
|
GetAcc
|
|
|
|
|
|
|
|
|IL.opIS:
|
|
|
|
PushAll(1);
|
|
|
|
pushc(param2);
|
|
|
|
CallRTL(IL._is, 2);
|
|
|
|
GetAcc
|
|
|
|
|
|
|
|
|IL.opISREC:
|
|
|
|
PushAll(2);
|
|
|
|
pushc(param2);
|
|
|
|
CallRTL(IL._guardrec, 3);
|
|
|
|
GetAcc
|
|
|
|
|
|
|
|
|IL.opTYPEGR:
|
|
|
|
PushAll(1);
|
|
|
|
pushc(param2);
|
|
|
|
CallRTL(IL._guardrec, 2);
|
|
|
|
GetAcc
|
|
|
|
|
|
|
|
|IL.opTYPEGD:
|
|
|
|
UnOp(r1);
|
|
|
|
PushAll(0);
|
2021-06-15 19:33:16 +02:00
|
|
|
subrc(r1, szWord);
|
|
|
|
ldr(r1, r1);
|
2020-10-13 09:58:51 +02:00
|
|
|
push(r1);
|
|
|
|
pushc(param2);
|
|
|
|
CallRTL(IL._guardrec, 2);
|
|
|
|
GetAcc
|
|
|
|
|
|
|
|
|IL.opCASET:
|
|
|
|
push(R1);
|
|
|
|
push(R1);
|
|
|
|
pushc(param2);
|
|
|
|
CallRTL(IL._guardrec, 2);
|
|
|
|
pop(R1);
|
|
|
|
jnz(ACC, param1)
|
|
|
|
|
|
|
|
|IL.opCONSTF:
|
2021-06-15 19:33:16 +02:00
|
|
|
IF szWord = 8 THEN
|
|
|
|
movrc(GetAnyReg(), UTILS.splitf(cmd.float, a, b))
|
|
|
|
ELSE (* szWord = 4 *)
|
|
|
|
movrc(GetAnyReg(), UTILS.d2s(cmd.float))
|
|
|
|
END
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opMULF:
|
|
|
|
PushAll(2);
|
|
|
|
CallRTL(IL._fmul, 2);
|
|
|
|
GetAcc
|
|
|
|
|
|
|
|
|IL.opDIVF:
|
|
|
|
PushAll(2);
|
|
|
|
CallRTL(IL._fdiv, 2);
|
|
|
|
GetAcc
|
|
|
|
|
|
|
|
|IL.opDIVFI:
|
|
|
|
PushAll(2);
|
|
|
|
CallRTL(IL._fdivi, 2);
|
|
|
|
GetAcc
|
|
|
|
|
|
|
|
|IL.opADDF:
|
|
|
|
PushAll(2);
|
|
|
|
CallRTL(IL._fadd, 2);
|
|
|
|
GetAcc
|
|
|
|
|
|
|
|
|IL.opSUBFI:
|
|
|
|
PushAll(2);
|
|
|
|
CallRTL(IL._fsubi, 2);
|
|
|
|
GetAcc
|
|
|
|
|
|
|
|
|IL.opSUBF:
|
|
|
|
PushAll(2);
|
|
|
|
CallRTL(IL._fsub, 2);
|
|
|
|
GetAcc
|
|
|
|
|
|
|
|
|IL.opEQF..IL.opGEF:
|
|
|
|
PushAll(2);
|
|
|
|
pushc(opcode - IL.opEQF);
|
|
|
|
CallRTL(IL._fcmp, 3);
|
|
|
|
GetAcc
|
|
|
|
|
|
|
|
|IL.opFLOOR:
|
|
|
|
PushAll(1);
|
|
|
|
CallRTL(IL._floor, 1);
|
|
|
|
GetAcc
|
|
|
|
|
|
|
|
|IL.opFLT:
|
|
|
|
PushAll(1);
|
|
|
|
CallRTL(IL._flt, 1);
|
|
|
|
GetAcc
|
|
|
|
|
|
|
|
|IL.opUMINF:
|
|
|
|
UnOp(r1);
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opRORC, r1, -1);
|
|
|
|
Emit(opXORC, r1, 1);
|
|
|
|
Emit(opRORC, r1, 1)
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opFABS:
|
|
|
|
UnOp(r1);
|
2021-06-15 19:33:16 +02:00
|
|
|
Emit(opLSLC, r1, 1);
|
|
|
|
Emit(opLSRC, r1, 1)
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opINF:
|
2021-06-15 19:33:16 +02:00
|
|
|
r1 := GetAnyReg();
|
|
|
|
Emit(opMOVC, r1, 1);
|
|
|
|
Emit(opRORC, r1, 1);
|
|
|
|
Emit(opASRC, r1, 7 + 3 * ORD(szWord = 8));
|
|
|
|
Emit(opLSRC, r1, 1)
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
|IL.opPUSHF:
|
|
|
|
UnOp(r1);
|
|
|
|
push(r1);
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opPACK:
|
|
|
|
PushAll(2);
|
|
|
|
CallRTL(IL._pack, 2)
|
|
|
|
|
|
|
|
|IL.opPACKC:
|
|
|
|
PushAll(1);
|
|
|
|
pushc(param2);
|
|
|
|
CallRTL(IL._pack, 2)
|
|
|
|
|
|
|
|
|IL.opUNPK:
|
|
|
|
PushAll(2);
|
|
|
|
CallRTL(IL._unpk, 2)
|
|
|
|
|
|
|
|
|IL.opCODE:
|
|
|
|
OutInt(param2)
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
|IL.opLADR_SAVE:
|
|
|
|
UnOp(r1);
|
|
|
|
Emit(opST, BP * 256 + r1, param2 * szWord);
|
|
|
|
drop
|
|
|
|
|
|
|
|
|IL.opLADR_INCC:
|
|
|
|
r1 := GetAnyReg();
|
|
|
|
Emit(opLD, BP * 256 + r1, param1 * szWord);
|
|
|
|
Emit(opADDC, r1, param2);
|
|
|
|
Emit(opST, BP * 256 + r1, param1 * szWord);
|
|
|
|
drop
|
|
|
|
|
2020-10-13 09:58:51 +02:00
|
|
|
END;
|
|
|
|
|
|
|
|
cmd := cmd.next(IL.COMMAND)
|
|
|
|
END;
|
|
|
|
|
|
|
|
ASSERT(R.pushed = 0);
|
|
|
|
ASSERT(R.top = -1)
|
|
|
|
END translate;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE prolog;
|
|
|
|
BEGIN
|
|
|
|
Emit(opLEA, SP + LStack * 256, 0);
|
|
|
|
Emit(opLEA, ACC + LTypes * 256, 0);
|
|
|
|
push(ACC);
|
|
|
|
Emit(opLEA, ACC + LHeap * 256, 0);
|
|
|
|
push(ACC);
|
|
|
|
pushc(CHL.Length(IL.codes.types));
|
|
|
|
CallRTL(IL._init, 3)
|
|
|
|
END prolog;
|
|
|
|
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
PROCEDURE epilog (ram, szWord: INTEGER);
|
2020-10-13 09:58:51 +02:00
|
|
|
VAR
|
2021-06-15 19:33:16 +02:00
|
|
|
tcount, dcount, i, offTypes, offStrings,
|
|
|
|
szData, szGlobal, szHeapStack: INTEGER;
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
BEGIN
|
|
|
|
Emit(opSTOP, 0, 0);
|
|
|
|
|
|
|
|
offTypes := count;
|
|
|
|
|
|
|
|
tcount := CHL.Length(IL.codes.types);
|
|
|
|
FOR i := 0 TO tcount - 1 DO
|
|
|
|
OutInt(CHL.GetInt(IL.codes.types, i))
|
|
|
|
END;
|
|
|
|
|
|
|
|
offStrings := count;
|
|
|
|
dcount := CHL.Length(IL.codes.data);
|
|
|
|
FOR i := 0 TO dcount - 1 DO
|
|
|
|
OutByte(CHL.GetByte(IL.codes.data, i))
|
|
|
|
END;
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
IF dcount MOD szWord # 0 THEN
|
|
|
|
i := szWord - dcount MOD szWord;
|
2020-10-13 09:58:51 +02:00
|
|
|
WHILE i > 0 DO
|
|
|
|
OutByte(0);
|
|
|
|
DEC(i)
|
|
|
|
END
|
|
|
|
END;
|
|
|
|
|
|
|
|
szData := count - offTypes;
|
2021-06-15 19:33:16 +02:00
|
|
|
szGlobal := (IL.codes.bss DIV szWord + 1) * szWord;
|
2020-10-13 09:58:51 +02:00
|
|
|
szHeapStack := ram - szData - szGlobal;
|
|
|
|
|
|
|
|
OutInt(offTypes);
|
|
|
|
OutInt(offStrings);
|
2021-06-15 19:33:16 +02:00
|
|
|
OutInt(szGlobal DIV szWord);
|
|
|
|
OutInt(szHeapStack DIV szWord);
|
2020-10-13 09:58:51 +02:00
|
|
|
FOR i := 1 TO 8 DO
|
|
|
|
OutInt(0)
|
|
|
|
END
|
|
|
|
END epilog;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE CodeGen* (outname: ARRAY OF CHAR; target: INTEGER; options: PROG.OPTIONS);
|
|
|
|
CONST
|
|
|
|
minRAM = 32*1024;
|
|
|
|
maxRAM = 256*1024;
|
|
|
|
|
|
|
|
VAR
|
|
|
|
szData, szRAM: INTEGER;
|
|
|
|
|
|
|
|
BEGIN
|
2021-06-15 19:33:16 +02:00
|
|
|
szWord := TARGETS.WordSize;
|
|
|
|
IF szWord = 8 THEN
|
|
|
|
ldr := ldr64;
|
|
|
|
str := str64
|
|
|
|
ELSE
|
|
|
|
ldr := ldr32;
|
|
|
|
str := str32
|
|
|
|
END;
|
|
|
|
szData := (CHL.Length(IL.codes.types) + CHL.Length(IL.codes.data) DIV szWord + IL.codes.bss DIV szWord + 2) * szWord;
|
2020-10-13 09:58:51 +02:00
|
|
|
szRAM := MIN(MAX(options.ram, minRAM), maxRAM) * 1024;
|
|
|
|
|
|
|
|
IF szRAM - szData < 1024*1024 THEN
|
|
|
|
ERRORS.Error(208)
|
|
|
|
END;
|
|
|
|
|
|
|
|
count := 0;
|
|
|
|
WR.Create(outname);
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
REG.Init(R, push, pop, mov, xchg, GPRs);
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
prolog;
|
2021-06-15 19:33:16 +02:00
|
|
|
translate(szWord);
|
|
|
|
epilog(szRAM, szWord);
|
2020-10-13 09:58:51 +02:00
|
|
|
|
|
|
|
WR.Close
|
|
|
|
END CodeGen;
|
|
|
|
|
|
|
|
|
2021-06-15 19:33:16 +02:00
|
|
|
END RVMxI.
|