Subversion Repositories Kolibri OS

Rev

Rev 9847 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. (*
  2.     BSD 2-Clause License
  3.  
  4.     Copyright (c) 2018-2022, Anton Krotov
  5.     All rights reserved.
  6. *)
  7.  
  8. MODULE X86;
  9.  
  10. IMPORT IL, REG, UTILS, LISTS, BIN, PE32, KOS, MSCOFF, ELF, PROG,
  11.        CHL := CHUNKLISTS, PATHS, TARGETS, ERRORS;
  12.  
  13.  
  14. CONST
  15.  
  16.     eax = REG.R0; ecx = REG.R1; edx = REG.R2;
  17.  
  18.     al = eax; cl = ecx; dl = edx; ah = 4;
  19.  
  20.     ax = eax; cx = ecx; dx = edx;
  21.  
  22.     esp = 4;
  23.     ebp = 5;
  24.  
  25.     MAX_FR = 7;
  26.  
  27.     sete = 94H; setne = 95H; setl = 9CH; setge = 9DH; setle = 9EH; setg = 9FH; setc = 92H; setnc = 93H;
  28.  
  29.     je = 84H; jne = 85H; jl = 8CH; jge = 8DH; jle = 8EH; jg = 8FH; jb = 82H; jnb = 83H;
  30.  
  31.  
  32.     CODECHUNK = 8;
  33.  
  34.     FPR_ERR = 41;
  35.  
  36.  
  37. TYPE
  38.  
  39.     COMMAND = IL.COMMAND;
  40.  
  41.  
  42.     ANYCODE = POINTER TO RECORD (LISTS.ITEM)
  43.  
  44.         offset: INTEGER
  45.  
  46.     END;
  47.  
  48.     CODE = POINTER TO RECORD (ANYCODE)
  49.  
  50.         code:    ARRAY CODECHUNK OF BYTE;
  51.         length:  INTEGER
  52.  
  53.     END;
  54.  
  55.     LABEL = POINTER TO RECORD (ANYCODE)
  56.  
  57.         label: INTEGER
  58.  
  59.     END;
  60.  
  61.     JUMP = POINTER TO RECORD (ANYCODE)
  62.  
  63.         label, diff: INTEGER;
  64.         short: BOOLEAN
  65.  
  66.     END;
  67.  
  68.     JMP = POINTER TO RECORD (JUMP)
  69.  
  70.     END;
  71.  
  72.     JCC = POINTER TO RECORD (JUMP)
  73.  
  74.         jmp: INTEGER
  75.  
  76.     END;
  77.  
  78.     CALL = POINTER TO RECORD (JUMP)
  79.  
  80.     END;
  81.  
  82.     RELOC = POINTER TO RECORD (ANYCODE)
  83.  
  84.         op, value: INTEGER
  85.  
  86.     END;
  87.  
  88.  
  89. VAR
  90.  
  91.     R: REG.REGS;
  92.  
  93.     program: BIN.PROGRAM;
  94.  
  95.     CodeList: LISTS.LIST;
  96.  
  97.     tcount, LocVarSize, mainLocVarSize: INTEGER;
  98.  
  99.     FR: ARRAY 1000 OF INTEGER;
  100.  
  101.     fname: PATHS.PATH;
  102.  
  103.     FltConstLabel, mainFltConstLabel: LABEL;
  104.  
  105.  
  106. PROCEDURE OutByte* (n: BYTE);
  107. VAR
  108.     c: CODE;
  109.     last: ANYCODE;
  110.  
  111. BEGIN
  112.     last := CodeList.last(ANYCODE);
  113.  
  114.     IF (last IS CODE) & (last(CODE).length < CODECHUNK) THEN
  115.         c := last(CODE);
  116.         c.code[c.length] := n;
  117.         INC(c.length)
  118.     ELSE
  119.         NEW(c);
  120.         c.code[0] := n;
  121.         c.length  := 1;
  122.         LISTS.push(CodeList, c)
  123.     END
  124.  
  125. END OutByte;
  126.  
  127.  
  128. PROCEDURE OutInt (n: INTEGER);
  129. BEGIN
  130.     OutByte(n MOD 256);
  131.     OutByte(UTILS.Byte(n, 1));
  132.     OutByte(UTILS.Byte(n, 2));
  133.     OutByte(UTILS.Byte(n, 3))
  134. END OutInt;
  135.  
  136.  
  137. PROCEDURE OutByte2 (a, b: BYTE);
  138. BEGIN
  139.     OutByte(a);
  140.     OutByte(b)
  141. END OutByte2;
  142.  
  143.  
  144. PROCEDURE OutByte3 (a, b, c: BYTE);
  145. BEGIN
  146.     OutByte(a);
  147.     OutByte(b);
  148.     OutByte(c)
  149. END OutByte3;
  150.  
  151.  
  152. PROCEDURE OutWord (n: INTEGER);
  153. BEGIN
  154.     ASSERT((0 <= n) & (n <= 65535));
  155.     OutByte2(n MOD 256, n DIV 256)
  156. END OutWord;
  157.  
  158.  
  159. PROCEDURE isByte* (n: INTEGER): BOOLEAN;
  160.     RETURN (-128 <= n) & (n <= 127)
  161. END isByte;
  162.  
  163.  
  164. PROCEDURE short (n: INTEGER): INTEGER;
  165.     RETURN 2 * ORD(isByte(n))
  166. END short;
  167.  
  168.  
  169. PROCEDURE long (n: INTEGER): INTEGER;
  170.     RETURN 40H * ORD(~isByte(n))
  171. END long;
  172.  
  173.  
  174. PROCEDURE OutIntByte (n: INTEGER);
  175. BEGIN
  176.     IF isByte(n) THEN
  177.         OutByte(n MOD 256)
  178.     ELSE
  179.         OutInt(n)
  180.     END
  181. END OutIntByte;
  182.  
  183.  
  184. PROCEDURE shift* (op, reg: INTEGER);
  185. BEGIN
  186.     CASE op OF
  187.     |IL.opASR, IL.opASR1, IL.opASR2: OutByte(0F8H + reg)
  188.     |IL.opROR, IL.opROR1, IL.opROR2: OutByte(0C8H + reg)
  189.     |IL.opLSL, IL.opLSL1, IL.opLSL2: OutByte(0E0H + reg)
  190.     |IL.opLSR, IL.opLSR1, IL.opLSR2: OutByte(0E8H + reg)
  191.     END
  192. END shift;
  193.  
  194.  
  195. PROCEDURE oprr (op: BYTE; reg1, reg2: INTEGER); (* op reg1, reg2 *)
  196. BEGIN
  197.     OutByte2(op, 0C0H + 8 * reg2 + reg1)
  198. END oprr;
  199.  
  200.  
  201. PROCEDURE mov (reg1, reg2: INTEGER); (* mov reg1, reg2 *)
  202. BEGIN
  203.     oprr(89H, reg1, reg2)
  204. END mov;
  205.  
  206.  
  207. PROCEDURE xchg (reg1, reg2: INTEGER); (* xchg reg1, reg2 *)
  208. BEGIN
  209.     IF eax IN {reg1, reg2} THEN
  210.         OutByte(90H + reg1 + reg2)
  211.     ELSE
  212.         oprr(87H, reg1, reg2)
  213.     END
  214. END xchg;
  215.  
  216.  
  217. PROCEDURE pop (reg: INTEGER);
  218. BEGIN
  219.     OutByte(58H + reg) (* pop reg *)
  220. END pop;
  221.  
  222.  
  223. PROCEDURE push (reg: INTEGER);
  224. BEGIN
  225.     OutByte(50H + reg) (* push reg *)
  226. END push;
  227.  
  228.  
  229. PROCEDURE xor (reg1, reg2: INTEGER); (* xor reg1, reg2 *)
  230. BEGIN
  231.     oprr(31H, reg1, reg2)
  232. END xor;
  233.  
  234.  
  235. PROCEDURE movrc (reg, n: INTEGER);
  236. BEGIN
  237.     IF n = 0 THEN
  238.         xor(reg, reg)
  239.     ELSE
  240.         OutByte(0B8H + reg); (* mov reg, n *)
  241.         OutInt(n)
  242.     END
  243. END movrc;
  244.  
  245.  
  246. PROCEDURE pushc* (n: INTEGER);
  247. BEGIN
  248.     OutByte(68H + short(n)); (* push n *)
  249.     OutIntByte(n)
  250. END pushc;
  251.  
  252.  
  253. PROCEDURE test (reg: INTEGER);
  254. BEGIN
  255.     OutByte2(85H, 0C0H + reg * 9)  (* test reg, reg *)
  256. END test;
  257.  
  258.  
  259. PROCEDURE neg (reg: INTEGER);
  260. BEGIN
  261.     OutByte2(0F7H, 0D8H + reg)  (* neg reg *)
  262. END neg;
  263.  
  264.  
  265. PROCEDURE not (reg: INTEGER);
  266. BEGIN
  267.     OutByte2(0F7H, 0D0H + reg)  (* not reg *)
  268. END not;
  269.  
  270.  
  271. PROCEDURE add (reg1, reg2: INTEGER); (* add reg1, reg2 *)
  272. BEGIN
  273.     oprr(01H, reg1, reg2)
  274. END add;
  275.  
  276.  
  277. PROCEDURE oprc* (op, reg, n: INTEGER);
  278. BEGIN
  279.     IF (reg = eax) & ~isByte(n) THEN
  280.         CASE op OF
  281.         |0C0H: op := 05H (* add *)
  282.         |0E8H: op := 2DH (* sub *)
  283.         |0F8H: op := 3DH (* cmp *)
  284.         |0E0H: op := 25H (* and *)
  285.         |0C8H: op := 0DH (* or  *)
  286.         |0F0H: op := 35H (* xor *)
  287.         END;
  288.         OutByte(op);
  289.         OutInt(n)
  290.     ELSE
  291.         OutByte2(81H + short(n), op + reg MOD 8);
  292.         OutIntByte(n)
  293.     END
  294. END oprc;
  295.  
  296.  
  297. PROCEDURE andrc (reg, n: INTEGER); (* and reg, n *)
  298. BEGIN
  299.     oprc(0E0H, reg, n)
  300. END andrc;
  301.  
  302.  
  303. PROCEDURE orrc (reg, n: INTEGER); (* or reg, n *)
  304. BEGIN
  305.     oprc(0C8H, reg, n)
  306. END orrc;
  307.  
  308.  
  309. PROCEDURE xorrc (reg, n: INTEGER); (* xor reg, n *)
  310. BEGIN
  311.     oprc(0F0H, reg, n)
  312. END xorrc;
  313.  
  314.  
  315. PROCEDURE addrc (reg, n: INTEGER); (* add reg, n *)
  316. BEGIN
  317.     oprc(0C0H, reg, n)
  318. END addrc;
  319.  
  320.  
  321. PROCEDURE subrc (reg, n: INTEGER); (* sub reg, n *)
  322. BEGIN
  323.     oprc(0E8H, reg, n)
  324. END subrc;
  325.  
  326.  
  327. PROCEDURE cmprc (reg, n: INTEGER); (* cmp reg, n *)
  328. BEGIN
  329.     IF n = 0 THEN
  330.         test(reg)
  331.     ELSE
  332.         oprc(0F8H, reg, n)
  333.     END
  334. END cmprc;
  335.  
  336.  
  337. PROCEDURE cmprr (reg1, reg2: INTEGER); (* cmp reg1, reg2 *)
  338. BEGIN
  339.     oprr(39H, reg1, reg2)
  340. END cmprr;
  341.  
  342.  
  343. PROCEDURE setcc* (cc, reg: INTEGER); (* setcc reg *)
  344. BEGIN
  345.     IF reg >= 8 THEN
  346.         OutByte(41H)
  347.     END;
  348.     OutByte3(0FH, cc, 0C0H + reg MOD 8)
  349. END setcc;
  350.  
  351.  
  352. PROCEDURE ret*;
  353. BEGIN
  354.     OutByte(0C3H)
  355. END ret;
  356.  
  357.  
  358. PROCEDURE drop;
  359. BEGIN
  360.     REG.Drop(R)
  361. END drop;
  362.  
  363.  
  364. PROCEDURE GetAnyReg (): INTEGER;
  365.     RETURN REG.GetAnyReg(R)
  366. END GetAnyReg;
  367.  
  368.  
  369. PROCEDURE cond* (op: INTEGER): INTEGER;
  370. VAR
  371.     res: INTEGER;
  372.  
  373. BEGIN
  374.     CASE op OF
  375.     |IL.opGT, IL.opGTC: res := jg
  376.     |IL.opGE, IL.opGEC: res := jge
  377.     |IL.opLT, IL.opLTC: res := jl
  378.     |IL.opLE, IL.opLEC: res := jle
  379.     |IL.opEQ, IL.opEQC: res := je
  380.     |IL.opNE, IL.opNEC: res := jne
  381.     END
  382.  
  383.     RETURN res
  384. END cond;
  385.  
  386.  
  387. PROCEDURE inv0* (op: INTEGER): INTEGER;
  388.     RETURN ORD(BITS(op) / {0})
  389. END inv0;
  390.  
  391.  
  392. PROCEDURE Reloc* (op, value: INTEGER);
  393. VAR
  394.     reloc: RELOC;
  395.  
  396. BEGIN
  397.     NEW(reloc);
  398.     reloc.op := op;
  399.     reloc.value := value;
  400.     LISTS.push(CodeList, reloc)
  401. END Reloc;
  402.  
  403.  
  404. PROCEDURE PushFlt (label: LABEL; value: REAL);
  405. VAR
  406.     a, b, n: INTEGER;
  407.  
  408.  
  409.     PROCEDURE pushImm (label: LABEL; value: INTEGER);
  410.     VAR
  411.         c: CODE;
  412.         i: INTEGER;
  413.  
  414.     BEGIN
  415.         NEW(c);
  416.         IF isByte(value) THEN
  417.             c.code[0] := 6AH;
  418.             c.code[1] := value MOD 256;
  419.             c.length := 2
  420.         ELSE
  421.             c.code[0] := 68H;
  422.             FOR i := 1 TO 4 DO
  423.                 c.code[i] := UTILS.Byte(value, i - 1)
  424.             END;
  425.             c.length := 5
  426.         END;
  427.         LISTS.insertL(CodeList, label, c)
  428.     END pushImm;
  429.  
  430.  
  431. BEGIN
  432.     n := UTILS.splitf(value, a, b);
  433.     pushImm(label, b);
  434.     pushImm(label, a)
  435. END PushFlt;
  436.  
  437.  
  438. PROCEDURE jcc* (cc, label: INTEGER);
  439. VAR
  440.     j: JCC;
  441.  
  442. BEGIN
  443.     NEW(j);
  444.     j.label := label;
  445.     j.jmp   := cc;
  446.     j.short := FALSE;
  447.     LISTS.push(CodeList, j)
  448. END jcc;
  449.  
  450.  
  451. PROCEDURE jmp* (label: INTEGER);
  452. VAR
  453.     j: JMP;
  454.  
  455. BEGIN
  456.     NEW(j);
  457.     j.label := label;
  458.     j.short := FALSE;
  459.     LISTS.push(CodeList, j)
  460. END jmp;
  461.  
  462.  
  463. PROCEDURE call* (label: INTEGER);
  464. VAR
  465.     c: CALL;
  466.  
  467. BEGIN
  468.     NEW(c);
  469.     c.label := label;
  470.     c.short := TRUE;
  471.     LISTS.push(CodeList, c)
  472. END call;
  473.  
  474.  
  475. PROCEDURE Pic (reg, opcode, value: INTEGER);
  476. BEGIN
  477.     OutByte(0E8H); OutInt(0); (* call L
  478.                                  L: *)
  479.     pop(reg);
  480.     OutByte2(081H, 0C0H + reg);  (* add reg, ... *)
  481.     Reloc(opcode, value)
  482. END Pic;
  483.  
  484.  
  485. PROCEDURE CallRTL (pic: BOOLEAN; proc: INTEGER);
  486. VAR
  487.     label: INTEGER;
  488.     reg1:  INTEGER;
  489.  
  490. BEGIN
  491.     label := IL.codes.rtl[proc];
  492.  
  493.     IF label < 0 THEN
  494.         label := -label;
  495.         IF pic THEN
  496.             reg1 := GetAnyReg();
  497.             Pic(reg1, BIN.PICIMP, label);
  498.             OutByte2(0FFH, 010H + reg1);  (* call dword[reg1] *)
  499.             drop
  500.         ELSE
  501.             OutByte2(0FFH, 015H);  (* call dword[label] *)
  502.             Reloc(BIN.RIMP, label)
  503.         END
  504.     ELSE
  505.         call(label)
  506.     END
  507. END CallRTL;
  508.  
  509.  
  510. PROCEDURE SetLabel* (label: INTEGER);
  511. VAR
  512.     L: LABEL;
  513.  
  514. BEGIN
  515.     NEW(L);
  516.     L.label := label;
  517.     LISTS.push(CodeList, L)
  518. END SetLabel;
  519.  
  520.  
  521. PROCEDURE fixup*;
  522. VAR
  523.     code:      ANYCODE;
  524.     count, i:  INTEGER;
  525.     shorted:   BOOLEAN;
  526.     jump:      JUMP;
  527.  
  528. BEGIN
  529.  
  530.     REPEAT
  531.  
  532.         shorted := FALSE;
  533.         count := 0;
  534.  
  535.         code := CodeList.first(ANYCODE);
  536.         WHILE code # NIL DO
  537.             code.offset := count;
  538.  
  539.             CASE code OF
  540.             |CODE:   INC(count, code.length)
  541.             |LABEL:  BIN.SetLabel(program, code.label, count)
  542.             |JMP:    IF code.short THEN INC(count, 2) ELSE INC(count, 5) END; code.offset := count
  543.             |JCC:    IF code.short THEN INC(count, 2) ELSE INC(count, 6) END; code.offset := count
  544.             |CALL:   INC(count, 5); code.offset := count
  545.             |RELOC:  INC(count, 4)
  546.             END;
  547.  
  548.             code := code.next(ANYCODE)
  549.         END;
  550.  
  551.         code := CodeList.first(ANYCODE);
  552.         WHILE code # NIL DO
  553.  
  554.             IF code IS JUMP THEN
  555.                 jump := code(JUMP);
  556.                 jump.diff := BIN.GetLabel(program, jump.label) - code.offset;
  557.                 IF ~jump.short & isByte(jump.diff) THEN
  558.                     jump.short := TRUE;
  559.                     shorted := TRUE
  560.                 END
  561.             END;
  562.  
  563.             code := code.next(ANYCODE)
  564.         END
  565.  
  566.     UNTIL ~shorted;
  567.  
  568.     code := CodeList.first(ANYCODE);
  569.     WHILE code # NIL DO
  570.  
  571.         CASE code OF
  572.  
  573.         |CODE:
  574.                 FOR i := 0 TO code.length - 1 DO
  575.                     BIN.PutCode(program, code.code[i])
  576.                 END
  577.  
  578.         |LABEL:
  579.  
  580.         |JMP:
  581.                 IF code.short THEN
  582.                     BIN.PutCode(program, 0EBH);
  583.                     BIN.PutCode(program, code.diff MOD 256)
  584.                 ELSE
  585.                     BIN.PutCode(program, 0E9H);
  586.                     BIN.PutCode32LE(program, code.diff)
  587.                 END
  588.  
  589.         |JCC:
  590.                 IF code.short THEN
  591.                     BIN.PutCode(program, code.jmp - 16);
  592.                     BIN.PutCode(program, code.diff MOD 256)
  593.                 ELSE
  594.                     BIN.PutCode(program, 0FH);
  595.                     BIN.PutCode(program, code.jmp);
  596.                     BIN.PutCode32LE(program, code.diff)
  597.                 END
  598.  
  599.         |CALL:
  600.                 BIN.PutCode(program, 0E8H);
  601.                 BIN.PutCode32LE(program, code.diff)
  602.  
  603.         |RELOC:
  604.                 BIN.PutReloc(program, code.op);
  605.                 BIN.PutCode32LE(program, code.value)
  606.  
  607.         END;
  608.  
  609.         code := code.next(ANYCODE)
  610.     END
  611.  
  612. END fixup;
  613.  
  614.  
  615. PROCEDURE UnOp (VAR reg: INTEGER);
  616. BEGIN
  617.     REG.UnOp(R, reg)
  618. END UnOp;
  619.  
  620.  
  621. PROCEDURE BinOp (VAR reg1, reg2: INTEGER);
  622. BEGIN
  623.     REG.BinOp(R, reg1, reg2)
  624. END BinOp;
  625.  
  626.  
  627. PROCEDURE PushAll (NumberOfParameters: INTEGER);
  628. BEGIN
  629.     REG.PushAll(R);
  630.     DEC(R.pushed, NumberOfParameters)
  631. END PushAll;
  632.  
  633.  
  634. PROCEDURE NewLabel (): INTEGER;
  635. BEGIN
  636.     BIN.NewLabel(program)
  637.     RETURN IL.NewLabel()
  638. END NewLabel;
  639.  
  640.  
  641. PROCEDURE GetRegA;
  642. BEGIN
  643.     ASSERT(REG.GetReg(R, eax))
  644. END GetRegA;
  645.  
  646.  
  647. PROCEDURE fcmp;
  648. BEGIN
  649.     GetRegA;
  650.     OutByte2(0DAH, 0E9H);       (* fucompp *)
  651.     OutByte3(09BH, 0DFH, 0E0H); (* fstsw ax *)
  652.     OutByte(09EH);              (* sahf *)
  653.     OutByte(0B8H); OutInt(0)    (* mov eax, 0 *)
  654. END fcmp;
  655.  
  656.  
  657. PROCEDURE movzx* (reg1, reg2, offs: INTEGER; word: BOOLEAN); (* movzx reg1, byte/word[reg2 + offs] *)
  658. VAR
  659.     b: BYTE;
  660.  
  661. BEGIN
  662.     OutByte2(0FH, 0B6H + ORD(word));
  663.     IF (offs = 0) & (reg2 # ebp) THEN
  664.         b := 0
  665.     ELSE
  666.         b := 40H + long(offs)
  667.     END;
  668.     OutByte(b + (reg1 MOD 8) * 8 + reg2 MOD 8);
  669.     IF reg2 = esp THEN
  670.         OutByte(24H)
  671.     END;
  672.     IF b # 0 THEN
  673.         OutIntByte(offs)
  674.     END
  675. END movzx;
  676.  
  677.  
  678. PROCEDURE _movrm* (reg1, reg2, offs, size: INTEGER; mr: BOOLEAN);
  679. VAR
  680.     b: BYTE;
  681.  
  682. BEGIN
  683.     IF size = 16 THEN
  684.         OutByte(66H)
  685.     END;
  686.     IF (reg1 >= 8) OR (reg2 >= 8) OR (size = 64) THEN
  687.         OutByte(40H + reg2 DIV 8 + 4 * (reg1 DIV 8) + 8 * ORD(size = 64))
  688.     END;
  689.     OutByte(8BH - 2 * ORD(mr) - ORD(size = 8));
  690.     IF (offs = 0) & (reg2 # ebp) THEN
  691.         b := 0
  692.     ELSE
  693.         b := 40H + long(offs)
  694.     END;
  695.     OutByte(b + (reg1 MOD 8) * 8 + reg2 MOD 8);
  696.     IF reg2 = esp THEN
  697.         OutByte(24H)
  698.     END;
  699.     IF b # 0 THEN
  700.         OutIntByte(offs)
  701.     END
  702. END _movrm;
  703.  
  704.  
  705. PROCEDURE movmr (reg1, offs, reg2: INTEGER); (* mov dword[reg1+offs], reg2 *)
  706. BEGIN
  707.     _movrm(reg2, reg1, offs, 32, TRUE)
  708. END movmr;
  709.  
  710.  
  711. PROCEDURE movrm (reg1, reg2, offs: INTEGER); (* mov reg1, dword[reg2 + offs] *)
  712. BEGIN
  713.     _movrm(reg1, reg2, offs, 32, FALSE)
  714. END movrm;
  715.  
  716.  
  717. PROCEDURE movmr8* (reg1, offs, reg2: INTEGER); (* mov byte[reg1+offs], reg2_8 *)
  718. BEGIN
  719.     _movrm(reg2, reg1, offs, 8, TRUE)
  720. END movmr8;
  721.  
  722.  
  723. PROCEDURE movrm8* (reg1, reg2, offs: INTEGER); (* mov reg1_8, byte[reg2+offs] *)
  724. BEGIN
  725.     _movrm(reg1, reg2, offs, 8, FALSE)
  726. END movrm8;
  727.  
  728.  
  729. PROCEDURE movmr16* (reg1, offs, reg2: INTEGER); (* mov word[reg1+offs], reg2_16 *)
  730. BEGIN
  731.     _movrm(reg2, reg1, offs, 16, TRUE)
  732. END movmr16;
  733.  
  734.  
  735. PROCEDURE movrm16* (reg1, reg2, offs: INTEGER); (* mov reg1_16, word[reg2+offs] *)
  736. BEGIN
  737.     _movrm(reg1, reg2, offs, 16, FALSE)
  738. END movrm16;
  739.  
  740.  
  741. PROCEDURE pushm* (reg, offs: INTEGER); (* push qword[reg+offs] *)
  742. VAR
  743.     b: BYTE;
  744.  
  745. BEGIN
  746.     IF reg >= 8 THEN
  747.         OutByte(41H)
  748.     END;
  749.     OutByte(0FFH);
  750.     IF (offs = 0) & (reg # ebp) THEN
  751.         b := 30H
  752.     ELSE
  753.         b := 70H + long(offs)
  754.     END;
  755.     OutByte(b + reg MOD 8);
  756.     IF reg = esp THEN
  757.         OutByte(24H)
  758.     END;
  759.     IF b # 30H THEN
  760.         OutIntByte(offs)
  761.     END
  762. END pushm;
  763.  
  764.  
  765. PROCEDURE LoadFltConst (value: REAL);
  766. BEGIN
  767.     PushFlt(FltConstLabel, value);
  768.     INC(LocVarSize, 8);
  769.     IF FltConstLabel = mainFltConstLabel THEN
  770.         mainLocVarSize := LocVarSize
  771.     END;
  772.     OutByte2(0DDH, 045H + long(-LocVarSize)); (* fld qword[ebp - LocVarSize] *)
  773.     OutIntByte(-LocVarSize)
  774. END LoadFltConst;
  775.  
  776.  
  777. PROCEDURE translate (pic: BOOLEAN; stroffs: INTEGER);
  778. VAR
  779.     cmd, next: COMMAND;
  780.  
  781.     reg1, reg2, reg3, fr: INTEGER;
  782.  
  783.     n, a, label, cc: INTEGER;
  784.  
  785.     opcode, param1, param2: INTEGER;
  786.  
  787.     float: REAL;
  788.  
  789. BEGIN
  790.     cmd := IL.codes.commands.first(COMMAND);
  791.  
  792.     fr := -1;
  793.  
  794.     WHILE cmd # NIL DO
  795.  
  796.         param1 := cmd.param1;
  797.         param2 := cmd.param2;
  798.  
  799.         opcode := cmd.opcode;
  800.  
  801.         CASE opcode OF
  802.  
  803.         |IL.opJMP:
  804.             jmp(param1)
  805.  
  806.         |IL.opCALL:
  807.             call(param1)
  808.  
  809.         |IL.opCALLI:
  810.             IF pic THEN
  811.                 reg1 := GetAnyReg();
  812.                 Pic(reg1, BIN.PICIMP, param1);
  813.                 OutByte2(0FFH, 010H + reg1);  (* call dword[reg1] *)
  814.                 drop
  815.             ELSE
  816.                 OutByte2(0FFH, 015H);  (* call dword[L] *)
  817.                 Reloc(BIN.RIMP, param1)
  818.             END
  819.  
  820.         |IL.opCALLP:
  821.             UnOp(reg1);
  822.             OutByte2(0FFH, 0D0H + reg1);  (* call reg1 *)
  823.             drop;
  824.             ASSERT(R.top = -1)
  825.  
  826.         |IL.opFASTCALL:
  827.             IF param2 = 1 THEN
  828.                 pop(ecx)
  829.             ELSIF param2 = 2 THEN
  830.                 pop(ecx);
  831.                 pop(edx)
  832.             END
  833.  
  834.         |IL.opPRECALL:
  835.             PushAll(0);
  836.             IF (param2 # 0) & (fr >= 0) THEN
  837.                 subrc(esp, 8)
  838.             END;
  839.             INC(FR[0]);
  840.             FR[FR[0]] := fr + 1;
  841.             WHILE fr >= 0 DO
  842.                 subrc(esp, 8);
  843.                 OutByte3(0DDH, 01CH, 024H); (* fstp qword[esp] *)
  844.                 DEC(fr)
  845.             END;
  846.             ASSERT(fr = -1)
  847.  
  848.         |IL.opALIGN16:
  849.             ASSERT(eax IN R.regs);
  850.             mov(eax, esp);
  851.             andrc(esp, -16);
  852.             n := (3 - param2 MOD 4) * 4;
  853.             IF n > 0 THEN
  854.                 subrc(esp, n)
  855.             END;
  856.             push(eax)
  857.  
  858.         |IL.opRESF, IL.opRES:
  859.             ASSERT(R.top = -1);
  860.             ASSERT(fr = -1);
  861.             n := FR[FR[0]]; DEC(FR[0]);
  862.  
  863.             IF opcode = IL.opRESF THEN
  864.                 INC(fr);
  865.                 IF n > 0 THEN
  866.                     OutByte3(0DDH, 5CH + long(n * 8), 24H);
  867.                     OutIntByte(n * 8); (* fstp qword[esp + n*8] *)
  868.                     DEC(fr);
  869.                     INC(n)
  870.                 END;
  871.  
  872.                 IF fr + n > MAX_FR THEN
  873.                     ERRORS.ErrorMsg(fname, param1, param2, FPR_ERR)
  874.                 END
  875.             ELSE
  876.                 GetRegA
  877.             END;
  878.  
  879.             WHILE n > 0 DO
  880.                 OutByte3(0DDH, 004H, 024H); (* fld qword[esp] *)
  881.                 addrc(esp, 8);
  882.                 INC(fr);
  883.                 DEC(n)
  884.             END
  885.  
  886.         |IL.opENTER:
  887.             ASSERT(R.top = -1);
  888.  
  889.             SetLabel(param1);
  890.  
  891.             IF cmd.param3 > 0 THEN
  892.                 pop(eax);
  893.                 IF cmd.param3 >= 2 THEN
  894.                     push(edx)
  895.                 END;
  896.                 push(ecx);
  897.                 push(eax)
  898.             END;
  899.  
  900.             push(ebp);
  901.             mov(ebp, esp);
  902.  
  903.             n := param2;
  904.             IF n > 4 THEN
  905.                 movrc(ecx, n);
  906.                 pushc(0);             (* L: push 0 *)
  907.                 OutByte2(0E2H, 0FCH)  (* loop L    *)
  908.             ELSE
  909.                 WHILE n > 0 DO
  910.                     pushc(0);
  911.                     DEC(n)
  912.                 END
  913.             END;
  914.             SetLabel(NewLabel());
  915.             FltConstLabel := CodeList.last(LABEL);
  916.             LocVarSize := param2 * 4
  917.  
  918.         |IL.opLEAVE, IL.opLEAVER, IL.opLEAVEF:
  919.             IF opcode = IL.opLEAVER THEN
  920.                 UnOp(reg1);
  921.                 IF reg1 # eax THEN
  922.                     mov(eax, reg1)
  923.                 END;
  924.                 drop
  925.             END;
  926.  
  927.             ASSERT(R.top = -1);
  928.  
  929.             IF opcode = IL.opLEAVEF THEN
  930.                 DEC(fr)
  931.             END;
  932.  
  933.             ASSERT(fr = -1);
  934.  
  935.             IF LocVarSize > 0 THEN
  936.                 mov(esp, ebp)
  937.             END;
  938.  
  939.             pop(ebp);
  940.  
  941.             IF param2 > 0 THEN
  942.                 OutByte(0C2H); OutWord(param2 * 4 MOD 65536) (* ret param2*4 *)
  943.             ELSE
  944.                 ret
  945.             END;
  946.             FltConstLabel := mainFltConstLabel;
  947.             LocVarSize := mainLocVarSize
  948.  
  949.         |IL.opPUSHC:
  950.             pushc(param2)
  951.  
  952.         |IL.opONERR:
  953.             pushc(param2);
  954.             jmp(param1)
  955.  
  956.         |IL.opPARAM:
  957.             n := param2;
  958.             IF n = 1 THEN
  959.                 UnOp(reg1);
  960.                 push(reg1);
  961.                 drop
  962.             ELSE
  963.                 ASSERT(R.top + 1 <= n);
  964.                 PushAll(n)
  965.             END
  966.  
  967.         |IL.opCLEANUP:
  968.             IF param2 # 0 THEN
  969.                 addrc(esp, param2 * 4)
  970.             END
  971.  
  972.         |IL.opPOPSP:
  973.             pop(esp)
  974.  
  975.         |IL.opCONST:
  976.             movrc(GetAnyReg(), param2)
  977.  
  978.         |IL.opLABEL:
  979.             SetLabel(param1) (* L: *)
  980.  
  981.         |IL.opNOP, IL.opAND, IL.opOR:
  982.  
  983.         |IL.opGADR:
  984.             next := cmd.next(COMMAND);
  985.             IF next.opcode = IL.opADDC THEN
  986.                 INC(param2, next.param2);
  987.                 cmd := next
  988.             END;
  989.             reg1 := GetAnyReg();
  990.             IF pic THEN
  991.                 Pic(reg1, BIN.PICBSS, param2)
  992.             ELSE
  993.                 OutByte(0B8H + reg1);  (* mov reg1, _bss + param2 *)
  994.                 Reloc(BIN.RBSS, param2)
  995.             END
  996.  
  997.         |IL.opLADR:
  998.             next := cmd.next(COMMAND);
  999.             n := param2 * 4;
  1000.             IF next.opcode = IL.opADDC THEN
  1001.                 INC(n, next.param2);
  1002.                 cmd := next
  1003.             END;
  1004.             OutByte2(8DH, 45H + GetAnyReg() * 8 + long(n));  (* lea reg1, dword[ebp + n] *)
  1005.             OutIntByte(n)
  1006.  
  1007.         |IL.opVADR, IL.opLLOAD32:
  1008.             movrm(GetAnyReg(), ebp, param2 * 4)
  1009.  
  1010.         |IL.opSADR:
  1011.             reg1 := GetAnyReg();
  1012.             IF pic THEN
  1013.                 Pic(reg1, BIN.PICDATA, stroffs + param2);
  1014.             ELSE
  1015.                 OutByte(0B8H + reg1);  (* mov reg1, _data + stroffs + param2 *)
  1016.                 Reloc(BIN.RDATA, stroffs + param2)
  1017.             END
  1018.  
  1019.         |IL.opSAVEC:
  1020.             UnOp(reg1);
  1021.             OutByte2(0C7H, reg1); OutInt(param2);  (* mov dword[reg1], param2 *)
  1022.             drop
  1023.  
  1024.         |IL.opSAVE8C:
  1025.             UnOp(reg1);
  1026.             OutByte3(0C6H, reg1, param2 MOD 256);  (* mov byte[reg1], param2 *)
  1027.             drop
  1028.  
  1029.         |IL.opSAVE16C:
  1030.             UnOp(reg1);
  1031.             OutByte3(66H, 0C7H, reg1); OutWord(param2 MOD 65536);  (* mov word[reg1], param2 *)
  1032.             drop
  1033.  
  1034.         |IL.opVLOAD32:
  1035.             reg1 := GetAnyReg();
  1036.             movrm(reg1, ebp, param2 * 4);
  1037.             movrm(reg1, reg1, 0)
  1038.  
  1039.         |IL.opGLOAD32:
  1040.             reg1 := GetAnyReg();
  1041.             IF pic THEN
  1042.                 Pic(reg1, BIN.PICBSS, param2);
  1043.                 movrm(reg1, reg1, 0)
  1044.             ELSE
  1045.                 OutByte2(08BH, 05H + reg1 * 8);  (* mov reg1, dword[_bss + param2] *)
  1046.                 Reloc(BIN.RBSS, param2)
  1047.             END
  1048.  
  1049.         |IL.opLOAD32:
  1050.             UnOp(reg1);
  1051.             movrm(reg1, reg1, 0)
  1052.  
  1053.         |IL.opVLOAD8:
  1054.             reg1 := GetAnyReg();
  1055.             movrm(reg1, ebp, param2 * 4);
  1056.             movzx(reg1, reg1, 0, FALSE)
  1057.  
  1058.         |IL.opGLOAD8:
  1059.             reg1 := GetAnyReg();
  1060.             IF pic THEN
  1061.                 Pic(reg1, BIN.PICBSS, param2);
  1062.                 movzx(reg1, reg1, 0, FALSE)
  1063.             ELSE
  1064.                 OutByte3(00FH, 0B6H, 05H + reg1 * 8); (* movzx reg1, byte[_bss + param2] *)
  1065.                 Reloc(BIN.RBSS, param2)
  1066.             END
  1067.  
  1068.         |IL.opLLOAD8:
  1069.             movzx(GetAnyReg(), ebp, param2 * 4, FALSE)
  1070.  
  1071.         |IL.opLOAD8:
  1072.             UnOp(reg1);
  1073.             movzx(reg1, reg1, 0, FALSE)
  1074.  
  1075.         |IL.opVLOAD16:
  1076.             reg1 := GetAnyReg();
  1077.             movrm(reg1, ebp, param2 * 4);
  1078.             movzx(reg1, reg1, 0, TRUE)
  1079.  
  1080.         |IL.opGLOAD16:
  1081.             reg1 := GetAnyReg();
  1082.             IF pic THEN
  1083.                 Pic(reg1, BIN.PICBSS, param2);
  1084.                 movzx(reg1, reg1, 0, TRUE)
  1085.             ELSE
  1086.                 OutByte3(00FH, 0B7H, 05H + reg1 * 8);  (* movzx reg1, word[_bss + param2] *)
  1087.                 Reloc(BIN.RBSS, param2)
  1088.             END
  1089.  
  1090.         |IL.opLLOAD16:
  1091.             movzx(GetAnyReg(), ebp, param2 * 4, TRUE)
  1092.  
  1093.         |IL.opLOAD16:
  1094.             UnOp(reg1);
  1095.             movzx(reg1, reg1, 0, TRUE)
  1096.  
  1097.         |IL.opUMINUS:
  1098.             UnOp(reg1);
  1099.             neg(reg1)
  1100.  
  1101.         |IL.opADD:
  1102.             BinOp(reg1, reg2);
  1103.             add(reg1, reg2);
  1104.             drop
  1105.  
  1106.         |IL.opADDC:
  1107.             IF param2 # 0 THEN
  1108.                 UnOp(reg1);
  1109.                 next := cmd.next(COMMAND);
  1110.                 CASE next.opcode OF
  1111.                 |IL.opLOAD32:
  1112.                     movrm(reg1, reg1, param2);
  1113.                     cmd := next
  1114.                 |IL.opLOAD16:
  1115.                     movzx(reg1, reg1, param2, TRUE);
  1116.                     cmd := next
  1117.                 |IL.opLOAD8:
  1118.                     movzx(reg1, reg1, param2, FALSE);
  1119.                     cmd := next
  1120.                 |IL.opLOAD32_PARAM:
  1121.                     pushm(reg1, param2);
  1122.                     drop;
  1123.                     cmd := next
  1124.                 ELSE
  1125.                     IF param2 = 1 THEN
  1126.                         OutByte(40H + reg1) (* inc reg1 *)
  1127.                     ELSIF param2 = -1 THEN
  1128.                         OutByte(48H + reg1) (* dec reg1 *)
  1129.                     ELSE
  1130.                         addrc(reg1, param2)
  1131.                     END
  1132.                 END
  1133.             END
  1134.  
  1135.         |IL.opSUB:
  1136.             BinOp(reg1, reg2);
  1137.             oprr(29H, reg1, reg2); (* sub reg1, reg2 *)
  1138.             drop
  1139.  
  1140.         |IL.opSUBR, IL.opSUBL:
  1141.             UnOp(reg1);
  1142.             IF param2 = 1 THEN
  1143.                 OutByte(48H + reg1) (* dec reg1 *)
  1144.             ELSIF param2 = -1 THEN
  1145.                 OutByte(40H + reg1) (* inc reg1 *)
  1146.             ELSIF param2 # 0 THEN
  1147.                 subrc(reg1, param2)
  1148.             END;
  1149.             IF opcode = IL.opSUBL THEN
  1150.                 neg(reg1)
  1151.             END
  1152.  
  1153.         |IL.opMULC:
  1154.             IF (cmd.next(COMMAND).opcode = IL.opADD) & ((param2 = 2) OR (param2 = 4) OR (param2 = 8)) THEN
  1155.                 BinOp(reg1, reg2);
  1156.                 OutByte3(8DH, 04H + reg1 * 8, reg1 + reg2 * 8 + 40H * UTILS.Log2(param2)); (* lea reg1, [reg1 + reg2 * param2] *)
  1157.                 drop;
  1158.                 cmd := cmd.next(COMMAND)
  1159.             ELSE
  1160.                 UnOp(reg1);
  1161.  
  1162.                 a := param2;
  1163.                 IF a > 1 THEN
  1164.                     n := UTILS.Log2(a)
  1165.                 ELSIF a < -1 THEN
  1166.                     n := UTILS.Log2(-a)
  1167.                 ELSE
  1168.                     n := -1
  1169.                 END;
  1170.  
  1171.                 IF a = 1 THEN
  1172.  
  1173.                 ELSIF a = -1 THEN
  1174.                     neg(reg1)
  1175.                 ELSIF a = 0 THEN
  1176.                     xor(reg1, reg1)
  1177.                 ELSE
  1178.                     IF n > 0 THEN
  1179.                         IF a < 0 THEN
  1180.                             neg(reg1)
  1181.                         END;
  1182.  
  1183.                         IF n # 1 THEN
  1184.                             OutByte3(0C1H, 0E0H + reg1, n)   (* shl reg1, n *)
  1185.                         ELSE
  1186.                             OutByte2(0D1H, 0E0H + reg1)      (* shl reg1, 1 *)
  1187.                         END
  1188.                     ELSE
  1189.                         OutByte2(69H + short(a), 0C0H + reg1 * 9); (* imul reg1, a *)
  1190.                         OutIntByte(a)
  1191.                     END
  1192.                 END
  1193.             END
  1194.  
  1195.         |IL.opMUL:
  1196.             BinOp(reg1, reg2);
  1197.             OutByte3(0FH, 0AFH, 0C0H + reg1 * 8 + reg2); (* imul reg1, reg2 *)
  1198.             drop
  1199.  
  1200.         |IL.opSAVE, IL.opSAVE32:
  1201.             BinOp(reg2, reg1);
  1202.             movmr(reg1, 0, reg2);
  1203.             drop;
  1204.             drop
  1205.  
  1206.         |IL.opSAVE8:
  1207.             BinOp(reg2, reg1);
  1208.             movmr8(reg1, 0, reg2);
  1209.             drop;
  1210.             drop
  1211.  
  1212.         |IL.opSAVE16:
  1213.             BinOp(reg2, reg1);
  1214.             movmr16(reg1, 0, reg2);
  1215.             drop;
  1216.             drop
  1217.  
  1218.         |IL.opSAVEP:
  1219.             UnOp(reg1);
  1220.             IF pic THEN
  1221.                 reg2 := GetAnyReg();
  1222.                 Pic(reg2, BIN.PICCODE, param2);
  1223.                 movmr(reg1, 0, reg2);
  1224.                 drop
  1225.             ELSE
  1226.                 OutByte2(0C7H, reg1);  (* mov dword[reg1], L *)
  1227.                 Reloc(BIN.RCODE, param2)
  1228.             END;
  1229.             drop
  1230.  
  1231.         |IL.opSAVEIP:
  1232.             UnOp(reg1);
  1233.             IF pic THEN
  1234.                 reg2 := GetAnyReg();
  1235.                 Pic(reg2, BIN.PICIMP, param2);
  1236.                 pushm(reg2, 0);
  1237.                 OutByte2(08FH, reg1);  (* pop dword[reg1] *)
  1238.                 drop
  1239.             ELSE
  1240.                 OutByte2(0FFH, 035H);  (* push dword[L] *)
  1241.                 Reloc(BIN.RIMP, param2);
  1242.                 OutByte2(08FH, reg1)   (* pop dword[reg1] *)
  1243.             END;
  1244.             drop
  1245.  
  1246.         |IL.opPUSHP:
  1247.             reg1 := GetAnyReg();
  1248.             IF pic THEN
  1249.                 Pic(reg1, BIN.PICCODE, param2)
  1250.             ELSE
  1251.                 OutByte(0B8H + reg1);  (* mov reg1, L *)
  1252.                 Reloc(BIN.RCODE, param2)
  1253.             END
  1254.  
  1255.         |IL.opPUSHIP:
  1256.             reg1 := GetAnyReg();
  1257.             IF pic THEN
  1258.                 Pic(reg1, BIN.PICIMP, param2);
  1259.                 movrm(reg1, reg1, 0)
  1260.             ELSE
  1261.                 OutByte2(08BH, 05H + reg1 * 8);  (* mov reg1, dword[L] *)
  1262.                 Reloc(BIN.RIMP, param2)
  1263.             END
  1264.  
  1265.         |IL.opNOT:
  1266.             UnOp(reg1);
  1267.             test(reg1);
  1268.             setcc(sete, reg1);
  1269.             andrc(reg1, 1)
  1270.  
  1271.         |IL.opORD:
  1272.             UnOp(reg1);
  1273.             test(reg1);
  1274.             setcc(setne, reg1);
  1275.             andrc(reg1, 1)
  1276.  
  1277.         |IL.opSBOOL:
  1278.             BinOp(reg2, reg1);
  1279.             test(reg2);
  1280.             OutByte3(0FH, 95H, reg1); (* setne byte[reg1] *)
  1281.             drop;
  1282.             drop
  1283.  
  1284.         |IL.opSBOOLC:
  1285.             UnOp(reg1);
  1286.             OutByte3(0C6H, reg1, ORD(param2 # 0)); (* mov byte[reg1], 0/1 *)
  1287.             drop
  1288.  
  1289.         |IL.opEQ..IL.opGE,
  1290.          IL.opEQC..IL.opGEC:
  1291.  
  1292.             IF (IL.opEQ <= opcode) & (opcode <= IL.opGE) THEN
  1293.                 BinOp(reg1, reg2);
  1294.                 cmprr(reg1, reg2);
  1295.                 drop
  1296.             ELSE
  1297.                 UnOp(reg1);
  1298.                 cmprc(reg1, param2)
  1299.             END;
  1300.  
  1301.             drop;
  1302.             cc := cond(opcode);
  1303.             next := cmd.next(COMMAND);
  1304.  
  1305.             IF next.opcode = IL.opJNZ THEN
  1306.                 jcc(cc, next.param1);
  1307.                 cmd := next
  1308.             ELSIF next.opcode = IL.opJZ THEN
  1309.                 jcc(inv0(cc), next.param1);
  1310.                 cmd := next
  1311.             ELSE
  1312.                 reg1 := GetAnyReg();
  1313.                 setcc(cc + 16, reg1);
  1314.                 andrc(reg1, 1)
  1315.             END
  1316.  
  1317.         |IL.opEQB, IL.opNEB:
  1318.             BinOp(reg1, reg2);
  1319.             drop;
  1320.  
  1321.             test(reg1);
  1322.             OutByte2(74H, 5);  (* je @f *)
  1323.             movrc(reg1, 1);    (* mov reg1, 1
  1324.                                   @@: *)
  1325.             test(reg2);
  1326.             OutByte2(74H, 5);  (* je @f *)
  1327.             movrc(reg2, 1);    (* mov reg2, 1
  1328.                                   @@: *)
  1329.  
  1330.             cmprr(reg1, reg2);
  1331.             IF opcode = IL.opEQB THEN
  1332.                 setcc(sete, reg1)
  1333.             ELSE
  1334.                 setcc(setne, reg1)
  1335.             END;
  1336.             andrc(reg1, 1)
  1337.  
  1338.         |IL.opDROP:
  1339.             UnOp(reg1);
  1340.             drop
  1341.  
  1342.         |IL.opJNZ1:
  1343.             UnOp(reg1);
  1344.             test(reg1);
  1345.             jcc(jne, param1)
  1346.  
  1347.         |IL.opJG:
  1348.             UnOp(reg1);
  1349.             test(reg1);
  1350.             jcc(jg, param1)
  1351.  
  1352.         |IL.opJNZ:
  1353.             UnOp(reg1);
  1354.             test(reg1);
  1355.             jcc(jne, param1);
  1356.             drop
  1357.  
  1358.         |IL.opJZ:
  1359.             UnOp(reg1);
  1360.             test(reg1);
  1361.             jcc(je, param1);
  1362.             drop
  1363.  
  1364.         |IL.opSWITCH:
  1365.             UnOp(reg1);
  1366.             IF param2 = 0 THEN
  1367.                 reg2 := eax
  1368.             ELSE
  1369.                 reg2 := ecx
  1370.             END;
  1371.             IF reg1 # reg2 THEN
  1372.                 ASSERT(REG.GetReg(R, reg2));
  1373.                 ASSERT(REG.Exchange(R, reg1, reg2));
  1374.                 drop
  1375.             END;
  1376.             drop
  1377.  
  1378.         |IL.opENDSW:
  1379.  
  1380.         |IL.opCASEL:
  1381.             cmprc(eax, param1);
  1382.             jcc(jl, param2)
  1383.  
  1384.         |IL.opCASER:
  1385.             cmprc(eax, param1);
  1386.             jcc(jg, param2)
  1387.  
  1388.         |IL.opCASELR:
  1389.             cmprc(eax, param1);
  1390.             IF param2 = cmd.param3 THEN
  1391.                 jcc(jne, param2)
  1392.             ELSE
  1393.                 jcc(jl, param2);
  1394.                 jcc(jg, cmd.param3)
  1395.             END
  1396.  
  1397.         |IL.opCODE:
  1398.             OutByte(param2)
  1399.  
  1400.         |IL.opGET, IL.opGETC:
  1401.             IF opcode = IL.opGET THEN
  1402.                 BinOp(reg1, reg2)
  1403.             ELSIF opcode = IL.opGETC THEN
  1404.                 UnOp(reg2);
  1405.                 reg1 := GetAnyReg();
  1406.                 movrc(reg1, param1)
  1407.             END;
  1408.             drop;
  1409.             drop;
  1410.  
  1411.             IF param2 # 8 THEN
  1412.                 _movrm(reg1, reg1, 0, param2 * 8, FALSE);
  1413.                 _movrm(reg1, reg2, 0, param2 * 8, TRUE)
  1414.             ELSE
  1415.                 PushAll(0);
  1416.                 push(reg1);
  1417.                 push(reg2);
  1418.                 pushc(8);
  1419.                 CallRTL(pic, IL._move)
  1420.             END
  1421.  
  1422.         |IL.opSAVES:
  1423.             UnOp(reg2);
  1424.             REG.PushAll_1(R);
  1425.  
  1426.             IF pic THEN
  1427.                 reg1 := GetAnyReg();
  1428.                 Pic(reg1, BIN.PICDATA, stroffs + param2);
  1429.                 push(reg1);
  1430.                 drop
  1431.             ELSE
  1432.                 OutByte(068H);  (* push _data + stroffs + param2 *)
  1433.                 Reloc(BIN.RDATA, stroffs + param2);
  1434.             END;
  1435.  
  1436.             push(reg2);
  1437.             drop;
  1438.             pushc(param1);
  1439.             CallRTL(pic, IL._move)
  1440.  
  1441.         |IL.opCHKBYTE:
  1442.             BinOp(reg1, reg2);
  1443.             cmprc(reg1, 256);
  1444.             jcc(jb, param1)
  1445.  
  1446.         |IL.opCHKIDX:
  1447.             UnOp(reg1);
  1448.             cmprc(reg1, param2);
  1449.             jcc(jb, param1)
  1450.  
  1451.         |IL.opCHKIDX2:
  1452.             BinOp(reg1, reg2);
  1453.             IF param2 # -1 THEN
  1454.                 cmprr(reg2, reg1);
  1455.                 jcc(jb, param1)
  1456.             END;
  1457.             INCL(R.regs, reg1);
  1458.             DEC(R.top);
  1459.             R.stk[R.top] := reg2
  1460.  
  1461.         |IL.opLEN:
  1462.             n := param2;
  1463.             UnOp(reg1);
  1464.             drop;
  1465.             EXCL(R.regs, reg1);
  1466.  
  1467.             WHILE n > 0 DO
  1468.                 UnOp(reg2);
  1469.                 drop;
  1470.                 DEC(n)
  1471.             END;
  1472.  
  1473.             INCL(R.regs, reg1);
  1474.             ASSERT(REG.GetReg(R, reg1))
  1475.  
  1476.         |IL.opINCC:
  1477.             UnOp(reg1);
  1478.             IF param2 = 1 THEN
  1479.                 OutByte2(0FFH, reg1) (* inc dword[reg1] *)
  1480.             ELSIF param2 = -1 THEN
  1481.                 OutByte2(0FFH, reg1 + 8) (* dec dword[reg1] *)
  1482.             ELSE
  1483.                 OutByte2(81H + short(param2), reg1); OutIntByte(param2) (* add dword[reg1], param2 *)
  1484.             END;
  1485.             drop
  1486.  
  1487.         |IL.opINC, IL.opDEC:
  1488.             BinOp(reg1, reg2);
  1489.             OutByte2(01H + 28H * ORD(opcode = IL.opDEC), reg1 * 8 + reg2); (* add/sub dword[reg2], reg1 *)
  1490.             drop;
  1491.             drop
  1492.  
  1493.         |IL.opINCCB, IL.opDECCB:
  1494.             UnOp(reg1);
  1495.             OutByte3(80H, 28H * ORD(opcode = IL.opDECCB) + reg1, param2 MOD 256); (* add/sub byte[reg1], n *)
  1496.             drop
  1497.  
  1498.         |IL.opINCB, IL.opDECB:
  1499.             BinOp(reg1, reg2);
  1500.             OutByte2(28H * ORD(opcode = IL.opDECB), reg1 * 8 + reg2); (* add/sub byte[reg2], reg1 *)
  1501.             drop;
  1502.             drop
  1503.  
  1504.         |IL.opMULS:
  1505.             BinOp(reg1, reg2);
  1506.             oprr(21H, reg1, reg2); (* and reg1, reg2 *)
  1507.             drop
  1508.  
  1509.         |IL.opMULSC:
  1510.             UnOp(reg1);
  1511.             andrc(reg1, param2)
  1512.  
  1513.         |IL.opDIVS:
  1514.             BinOp(reg1, reg2);
  1515.             xor(reg1, reg2);
  1516.             drop
  1517.  
  1518.         |IL.opDIVSC:
  1519.             UnOp(reg1);
  1520.             xorrc(reg1, param2)
  1521.  
  1522.         |IL.opADDS:
  1523.             BinOp(reg1, reg2);
  1524.             oprr(9H, reg1, reg2); (* or reg1, reg2 *)
  1525.             drop
  1526.  
  1527.         |IL.opSUBS:
  1528.             BinOp(reg1, reg2);
  1529.             not(reg2);
  1530.             oprr(21H, reg1, reg2); (* and reg1, reg2 *)
  1531.             drop
  1532.  
  1533.         |IL.opADDSC:
  1534.             UnOp(reg1);
  1535.             orrc(reg1, param2)
  1536.  
  1537.         |IL.opSUBSL:
  1538.             UnOp(reg1);
  1539.             not(reg1);
  1540.             andrc(reg1, param2)
  1541.  
  1542.         |IL.opSUBSR:
  1543.             UnOp(reg1);
  1544.             andrc(reg1, ORD(-BITS(param2)))
  1545.  
  1546.         |IL.opUMINS:
  1547.             UnOp(reg1);
  1548.             not(reg1)
  1549.  
  1550.         |IL.opLENGTH:
  1551.             PushAll(2);
  1552.             CallRTL(pic, IL._length);
  1553.             GetRegA
  1554.  
  1555.         |IL.opLENGTHW:
  1556.             PushAll(2);
  1557.             CallRTL(pic, IL._lengthw);
  1558.             GetRegA
  1559.  
  1560.         |IL.opCHR:
  1561.             UnOp(reg1);
  1562.             andrc(reg1, 255)
  1563.  
  1564.         |IL.opWCHR:
  1565.             UnOp(reg1);
  1566.             andrc(reg1, 65535)
  1567.  
  1568.         |IL.opASR, IL.opROR, IL.opLSL, IL.opLSR:
  1569.             UnOp(reg1);
  1570.             IF reg1 # ecx THEN
  1571.                 ASSERT(REG.GetReg(R, ecx));
  1572.                 ASSERT(REG.Exchange(R, reg1, ecx));
  1573.                 drop
  1574.             END;
  1575.  
  1576.             BinOp(reg1, reg2);
  1577.             ASSERT(reg2 = ecx);
  1578.             OutByte(0D3H);
  1579.             shift(opcode, reg1); (* shift reg1, cl *)
  1580.             drop
  1581.  
  1582.         |IL.opASR1, IL.opROR1, IL.opLSL1, IL.opLSR1:
  1583.             UnOp(reg1);
  1584.             IF reg1 # ecx THEN
  1585.                 ASSERT(REG.GetReg(R, ecx));
  1586.                 ASSERT(REG.Exchange(R, reg1, ecx));
  1587.                 drop
  1588.             END;
  1589.  
  1590.             reg1 := GetAnyReg();
  1591.             movrc(reg1, param2);
  1592.             BinOp(reg1, reg2);
  1593.             ASSERT(reg1 = ecx);
  1594.             OutByte(0D3H);
  1595.             shift(opcode, reg2); (* shift reg2, cl *)
  1596.             drop;
  1597.             drop;
  1598.             ASSERT(REG.GetReg(R, reg2))
  1599.  
  1600.         |IL.opASR2, IL.opROR2, IL.opLSL2, IL.opLSR2:
  1601.             UnOp(reg1);
  1602.             n := param2 MOD 32;
  1603.             IF n # 1 THEN
  1604.                 OutByte(0C1H)
  1605.             ELSE
  1606.                 OutByte(0D1H)
  1607.             END;
  1608.             shift(opcode, reg1); (* shift reg1, n *)
  1609.             IF n # 1 THEN
  1610.                 OutByte(n)
  1611.             END
  1612.  
  1613.         |IL.opMAX, IL.opMIN:
  1614.             BinOp(reg1, reg2);
  1615.             cmprr(reg1, reg2);
  1616.             OutByte2(07DH + ORD(opcode = IL.opMIN), 2);  (* jge/jle L *)
  1617.             mov(reg1, reg2);
  1618.             (* L: *)
  1619.             drop
  1620.  
  1621.         |IL.opMAXC, IL.opMINC:
  1622.             UnOp(reg1);
  1623.             cmprc(reg1, param2);
  1624.             label := NewLabel();
  1625.             IF opcode = IL.opMINC THEN
  1626.                 cc := jle
  1627.             ELSE
  1628.                 cc := jge
  1629.             END;
  1630.             jcc(cc, label);
  1631.             movrc(reg1, param2);
  1632.             SetLabel(label)
  1633.  
  1634.         |IL.opIN, IL.opINR:
  1635.             IF opcode = IL.opINR THEN
  1636.                 reg2 := GetAnyReg();
  1637.                 movrc(reg2, param2)
  1638.             END;
  1639.             label := NewLabel();
  1640.             BinOp(reg1, reg2);
  1641.             cmprc(reg1, 32);
  1642.             OutByte2(72H, 4); (* jb L *)
  1643.             xor(reg1, reg1);
  1644.             jmp(label);
  1645.             (* L: *)
  1646.             OutByte3(0FH, 0A3H, 0C0H + reg2 + 8 * reg1); (* bt reg2, reg1 *)
  1647.             setcc(setc, reg1);
  1648.             andrc(reg1, 1);
  1649.             SetLabel(label);
  1650.             drop
  1651.  
  1652.         |IL.opINL:
  1653.             UnOp(reg1);
  1654.             OutByte3(0FH, 0BAH, 0E0H + reg1); OutByte(param2); (* bt reg1, param2 *)
  1655.             setcc(setc, reg1);
  1656.             andrc(reg1, 1)
  1657.  
  1658.         |IL.opRSET:
  1659.             PushAll(2);
  1660.             CallRTL(pic, IL._set);
  1661.             GetRegA
  1662.  
  1663.         |IL.opRSETR:
  1664.             PushAll(1);
  1665.             pushc(param2);
  1666.             CallRTL(pic, IL._set);
  1667.             GetRegA
  1668.  
  1669.         |IL.opRSETL:
  1670.             UnOp(reg1);
  1671.             REG.PushAll_1(R);
  1672.             pushc(param2);
  1673.             push(reg1);
  1674.             drop;
  1675.             CallRTL(pic, IL._set);
  1676.             GetRegA
  1677.  
  1678.         |IL.opRSET1:
  1679.             PushAll(1);
  1680.             CallRTL(pic, IL._set1);
  1681.             GetRegA
  1682.  
  1683.         |IL.opINCL, IL.opEXCL:
  1684.             BinOp(reg1, reg2);
  1685.             cmprc(reg1, 32);
  1686.             OutByte2(73H, 03H); (* jnb L *)
  1687.             OutByte(0FH);
  1688.             IF opcode = IL.opINCL THEN
  1689.                 OutByte(0ABH) (* bts dword[reg2], reg1 *)
  1690.             ELSE
  1691.                 OutByte(0B3H) (* btr dword[reg2], reg1 *)
  1692.             END;
  1693.             OutByte(reg2 + 8 * reg1);
  1694.             (* L: *)
  1695.             drop;
  1696.             drop
  1697.  
  1698.         |IL.opINCLC:
  1699.             UnOp(reg1);
  1700.             OutByte3(0FH, 0BAH, 28H + reg1); OutByte(param2); (* bts dword[reg1], param2 *)
  1701.             drop
  1702.  
  1703.         |IL.opEXCLC:
  1704.             UnOp(reg1);
  1705.             OutByte3(0FH, 0BAH, 30H + reg1); OutByte(param2); (* btr dword[reg1], param2 *)
  1706.             drop
  1707.  
  1708.         |IL.opDIV:
  1709.             PushAll(2);
  1710.             CallRTL(pic, IL._divmod);
  1711.             GetRegA
  1712.  
  1713.         |IL.opDIVR:
  1714.             n := UTILS.Log2(param2);
  1715.             IF n > 0 THEN
  1716.                 UnOp(reg1);
  1717.                 IF n # 1 THEN
  1718.                     OutByte3(0C1H, 0F8H + reg1, n) (* sar reg1, n *)
  1719.                 ELSE
  1720.                     OutByte2(0D1H, 0F8H + reg1)    (* sar reg1, 1 *)
  1721.                 END
  1722.             ELSIF n < 0 THEN
  1723.                 PushAll(1);
  1724.                 pushc(param2);
  1725.                 CallRTL(pic, IL._divmod);
  1726.                 GetRegA
  1727.             END
  1728.  
  1729.         |IL.opDIVL:
  1730.             UnOp(reg1);
  1731.             REG.PushAll_1(R);
  1732.             pushc(param2);
  1733.             push(reg1);
  1734.             drop;
  1735.             CallRTL(pic, IL._divmod);
  1736.             GetRegA
  1737.  
  1738.         |IL.opMOD:
  1739.             PushAll(2);
  1740.             CallRTL(pic, IL._divmod);
  1741.             mov(eax, edx);
  1742.             GetRegA
  1743.  
  1744.         |IL.opMODR:
  1745.             n := UTILS.Log2(param2);
  1746.             IF n > 0 THEN
  1747.                 UnOp(reg1);
  1748.                 andrc(reg1, param2 - 1);
  1749.             ELSIF n < 0 THEN
  1750.                 PushAll(1);
  1751.                 pushc(param2);
  1752.                 CallRTL(pic, IL._divmod);
  1753.                 mov(eax, edx);
  1754.                 GetRegA
  1755.             ELSE
  1756.                 UnOp(reg1);
  1757.                 xor(reg1, reg1)
  1758.             END
  1759.  
  1760.         |IL.opMODL:
  1761.             UnOp(reg1);
  1762.             REG.PushAll_1(R);
  1763.             pushc(param2);
  1764.             push(reg1);
  1765.             drop;
  1766.             CallRTL(pic, IL._divmod);
  1767.             mov(eax, edx);
  1768.             GetRegA
  1769.  
  1770.         |IL.opERR:
  1771.             CallRTL(pic, IL._error)
  1772.  
  1773.         |IL.opABS:
  1774.             UnOp(reg1);
  1775.             test(reg1);
  1776.             OutByte2(07DH, 002H); (* jge L *)
  1777.             neg(reg1)             (* neg reg1
  1778.                                      L: *)
  1779.  
  1780.         |IL.opCOPY:
  1781.             IF (0 < param2) & (param2 <= 64) THEN
  1782.                 BinOp(reg1, reg2);
  1783.                 reg3 := GetAnyReg();
  1784.                 FOR n := 0 TO param2 - param2 MOD 4 - 1 BY 4 DO
  1785.                     movrm(reg3, reg1, n);
  1786.                     movmr(reg2, n, reg3)
  1787.                 END;
  1788.                 n := param2 - param2 MOD 4;
  1789.                 IF param2 MOD 4 >= 2 THEN
  1790.                     movrm16(reg3, reg1, n);
  1791.                     movmr16(reg2, n, reg3);
  1792.                     INC(n, 2);
  1793.                     DEC(param2, 2)
  1794.                 END;
  1795.                 IF param2 MOD 4 = 1 THEN
  1796.                     movrm8(reg3, reg1, n);
  1797.                     movmr8(reg2, n, reg3);
  1798.                 END;
  1799.                 drop;
  1800.                 drop;
  1801.                 drop
  1802.             ELSE
  1803.                 PushAll(2);
  1804.                 pushc(param2);
  1805.                 CallRTL(pic, IL._move)
  1806.             END
  1807.  
  1808.         |IL.opMOVE:
  1809.             PushAll(3);
  1810.             CallRTL(pic, IL._move)
  1811.  
  1812.         |IL.opCOPYA:
  1813.             PushAll(4);
  1814.             pushc(param2);
  1815.             CallRTL(pic, IL._arrcpy);
  1816.             GetRegA
  1817.  
  1818.         |IL.opCOPYS:
  1819.             PushAll(4);
  1820.             pushc(param2);
  1821.             CallRTL(pic, IL._strcpy)
  1822.  
  1823.         |IL.opROT:
  1824.             PushAll(0);
  1825.             push(esp);
  1826.             pushc(param2);
  1827.             CallRTL(pic, IL._rot)
  1828.  
  1829.         |IL.opNEW:
  1830.             PushAll(1);
  1831.             n := param2 + 8;
  1832.             ASSERT(UTILS.Align(n, 32));
  1833.             pushc(n);
  1834.             pushc(param1);
  1835.             CallRTL(pic, IL._new)
  1836.  
  1837.         |IL.opDISP:
  1838.             PushAll(1);
  1839.             CallRTL(pic, IL._dispose)
  1840.  
  1841.         |IL.opEQS .. IL.opGES:
  1842.             PushAll(4);
  1843.             pushc(opcode - IL.opEQS);
  1844.             CallRTL(pic, IL._strcmp);
  1845.             GetRegA
  1846.  
  1847.         |IL.opEQSW .. IL.opGESW:
  1848.             PushAll(4);
  1849.             pushc(opcode - IL.opEQSW);
  1850.             CallRTL(pic, IL._strcmpw);
  1851.             GetRegA
  1852.  
  1853.         |IL.opEQP, IL.opNEP, IL.opEQIP, IL.opNEIP:
  1854.             UnOp(reg1);
  1855.             CASE opcode OF
  1856.             |IL.opEQP, IL.opNEP:
  1857.                 IF pic THEN
  1858.                     reg2 := GetAnyReg();
  1859.                     Pic(reg2, BIN.PICCODE, param1);
  1860.                     cmprr(reg1, reg2);
  1861.                     drop
  1862.                 ELSE
  1863.                     OutByte2(081H, 0F8H + reg1);  (* cmp reg1, L *)
  1864.                     Reloc(BIN.RCODE, param1)
  1865.                 END
  1866.  
  1867.             |IL.opEQIP, IL.opNEIP:
  1868.                 IF pic THEN
  1869.                     reg2 := GetAnyReg();
  1870.                     Pic(reg2, BIN.PICIMP, param1);
  1871.                     OutByte2(03BH, reg1 * 8 + reg2);  (* cmp reg1, dword [reg2] *)
  1872.                     drop
  1873.                 ELSE
  1874.                     OutByte2(3BH, 05H + reg1 * 8);    (* cmp reg1, dword[L] *)
  1875.                     Reloc(BIN.RIMP, param1)
  1876.                 END
  1877.  
  1878.             END;
  1879.             drop;
  1880.             reg1 := GetAnyReg();
  1881.  
  1882.             CASE opcode OF
  1883.             |IL.opEQP, IL.opEQIP: setcc(sete,  reg1)
  1884.             |IL.opNEP, IL.opNEIP: setcc(setne, reg1)
  1885.             END;
  1886.  
  1887.             andrc(reg1, 1)
  1888.  
  1889.         |IL.opPUSHT:
  1890.             UnOp(reg1);
  1891.             movrm(GetAnyReg(), reg1, -4)
  1892.  
  1893.         |IL.opISREC:
  1894.             PushAll(2);
  1895.             pushc(param2 * tcount);
  1896.             CallRTL(pic, IL._isrec);
  1897.             GetRegA
  1898.  
  1899.         |IL.opIS:
  1900.             PushAll(1);
  1901.             pushc(param2 * tcount);
  1902.             CallRTL(pic, IL._is);
  1903.             GetRegA
  1904.  
  1905.         |IL.opTYPEGR:
  1906.             PushAll(1);
  1907.             pushc(param2 * tcount);
  1908.             CallRTL(pic, IL._guardrec);
  1909.             GetRegA
  1910.  
  1911.         |IL.opTYPEGP:
  1912.             UnOp(reg1);
  1913.             PushAll(0);
  1914.             push(reg1);
  1915.             pushc(param2 * tcount);
  1916.             CallRTL(pic, IL._guard);
  1917.             GetRegA
  1918.  
  1919.         |IL.opTYPEGD:
  1920.             UnOp(reg1);
  1921.             PushAll(0);
  1922.             pushm(reg1, -4);
  1923.             pushc(param2 * tcount);
  1924.             CallRTL(pic, IL._guardrec);
  1925.             GetRegA
  1926.  
  1927.         |IL.opCASET:
  1928.             push(ecx);
  1929.             push(ecx);
  1930.             pushc(param2 * tcount);
  1931.             CallRTL(pic, IL._guardrec);
  1932.             pop(ecx);
  1933.             test(eax);
  1934.             jcc(jne, param1)
  1935.  
  1936.         |IL.opPACK:
  1937.             BinOp(reg1, reg2);
  1938.             push(reg2);
  1939.             OutByte3(0DBH, 004H, 024H);   (* fild dword[esp]  *)
  1940.             OutByte2(0DDH, reg1);         (* fld qword[reg1]  *)
  1941.             OutByte2(0D9H, 0FDH);         (* fscale           *)
  1942.             OutByte2(0DDH, 018H + reg1);  (* fstp qword[reg1] *)
  1943.             OutByte3(0DBH, 01CH, 024H);   (* fistp dword[esp] *)
  1944.             pop(reg2);
  1945.             drop;
  1946.             drop
  1947.  
  1948.         |IL.opPACKC:
  1949.             UnOp(reg1);
  1950.             pushc(param2);
  1951.             OutByte3(0DBH, 004H, 024H);   (* fild dword[esp]  *)
  1952.             OutByte2(0DDH, reg1);         (* fld qword[reg1]  *)
  1953.             OutByte2(0D9H, 0FDH);         (* fscale           *)
  1954.             OutByte2(0DDH, 018H + reg1);  (* fstp qword[reg1] *)
  1955.             OutByte3(0DBH, 01CH, 024H);   (* fistp dword[esp] *)
  1956.             pop(reg1);
  1957.             drop
  1958.  
  1959.         |IL.opUNPK:
  1960.             BinOp(reg1, reg2);
  1961.             OutByte2(0DDH, reg1);         (* fld qword[reg1]   *)
  1962.             OutByte2(0D9H, 0F4H);         (* fxtract           *)
  1963.             OutByte2(0DDH, 018H + reg1);  (* fstp qword[reg1]  *)
  1964.             OutByte2(0DBH, 018H + reg2);  (* fistp dword[reg2] *)
  1965.             drop;
  1966.             drop
  1967.  
  1968.         |IL.opPUSHF:
  1969.             ASSERT(fr >= 0);
  1970.             DEC(fr);
  1971.             subrc(esp, 8);
  1972.             OutByte3(0DDH, 01CH, 024H)    (* fstp qword[esp] *)
  1973.  
  1974.         |IL.opLOADF:
  1975.             INC(fr);
  1976.             IF fr > MAX_FR THEN
  1977.                 ERRORS.ErrorMsg(fname, param1, param2, FPR_ERR)
  1978.             END;
  1979.             UnOp(reg1);
  1980.             OutByte2(0DDH, reg1);         (* fld qword[reg1] *)
  1981.             drop
  1982.  
  1983.         |IL.opCONSTF:
  1984.             INC(fr);
  1985.             IF fr > MAX_FR THEN
  1986.                 ERRORS.ErrorMsg(fname, param1, param2, FPR_ERR)
  1987.             END;
  1988.             float := cmd.float;
  1989.             IF float = 0.0 THEN
  1990.                 OutByte2(0D9H, 0EEH)      (* fldz *)
  1991.             ELSIF float = 1.0 THEN
  1992.                 OutByte2(0D9H, 0E8H)      (* fld1 *)
  1993.             ELSIF float = -1.0 THEN
  1994.                 OutByte2(0D9H, 0E8H);     (* fld1 *)
  1995.                 OutByte2(0D9H, 0E0H)      (* fchs *)
  1996.             ELSE
  1997.                 LoadFltConst(float)
  1998.             END
  1999.  
  2000.         |IL.opSAVEF, IL.opSAVEFI:
  2001.             ASSERT(fr >= 0);
  2002.             DEC(fr);
  2003.             UnOp(reg1);
  2004.             OutByte2(0DDH, 018H + reg1); (* fstp qword[reg1] *)
  2005.             drop
  2006.  
  2007.         |IL.opADDF:
  2008.             ASSERT(fr >= 1);
  2009.             DEC(fr);
  2010.             OutByte2(0DEH, 0C1H)  (* faddp st1, st *)
  2011.  
  2012.         |IL.opSUBF:
  2013.             ASSERT(fr >= 1);
  2014.             DEC(fr);
  2015.             OutByte2(0DEH, 0E9H)  (* fsubp st1, st *)
  2016.  
  2017.         |IL.opSUBFI:
  2018.             ASSERT(fr >= 1);
  2019.             DEC(fr);
  2020.             OutByte2(0DEH, 0E1H)  (* fsubrp st1, st *)
  2021.  
  2022.         |IL.opMULF:
  2023.             ASSERT(fr >= 1);
  2024.             DEC(fr);
  2025.             OutByte2(0DEH, 0C9H)  (* fmulp st1, st *)
  2026.  
  2027.         |IL.opDIVF:
  2028.             ASSERT(fr >= 1);
  2029.             DEC(fr);
  2030.             OutByte2(0DEH, 0F9H)  (* fdivp st1, st *)
  2031.  
  2032.         |IL.opDIVFI:
  2033.             ASSERT(fr >= 1);
  2034.             DEC(fr);
  2035.             OutByte2(0DEH, 0F1H)  (* fdivrp st1, st *)
  2036.  
  2037.         |IL.opUMINF:
  2038.             ASSERT(fr >= 0);
  2039.             OutByte2(0D9H, 0E0H)  (* fchs *)
  2040.  
  2041.         |IL.opFABS:
  2042.             ASSERT(fr >= 0);
  2043.             OutByte2(0D9H, 0E1H)  (* fabs *)
  2044.  
  2045.         |IL.opFLT:
  2046.             INC(fr);
  2047.             IF fr > MAX_FR THEN
  2048.                 ERRORS.ErrorMsg(fname, param1, param2, FPR_ERR)
  2049.             END;
  2050.             UnOp(reg1);
  2051.             push(reg1);
  2052.             OutByte3(0DBH, 004H, 024H); (* fild dword[esp] *)
  2053.             pop(reg1);
  2054.             drop
  2055.  
  2056.         |IL.opFLOOR:
  2057.             ASSERT(fr >= 0);
  2058.             DEC(fr);
  2059.             subrc(esp, 8);
  2060.             OutByte2(09BH, 0D9H); OutByte3(07CH, 024H, 004H);                   (* fstcw word[esp+4]                    *)
  2061.             OutByte2(09BH, 0D9H); OutByte3(07CH, 024H, 006H);                   (* fstcw word[esp+6]                    *)
  2062.             OutByte2(066H, 081H); OutByte3(064H, 024H, 004H); OutWord(0F3FFH);  (* and   word[esp+4], 1111001111111111b *)
  2063.             OutByte2(066H, 081H); OutByte3(04CH, 024H, 004H); OutWord(00400H);  (* or    word[esp+4], 0000010000000000b *)
  2064.             OutByte2(0D9H, 06CH); OutByte2(024H, 004H);                         (* fldcw word[esp+4]                    *)
  2065.             OutByte2(0D9H, 0FCH);                                               (* frndint                              *)
  2066.             OutByte3(0DBH, 01CH, 024H);                                         (* fistp dword[esp]                     *)
  2067.             pop(GetAnyReg());
  2068.             OutByte2(0D9H, 06CH); OutByte2(024H, 002H);                         (* fldcw word[esp+2]                    *)
  2069.             addrc(esp, 4)
  2070.  
  2071.         |IL.opEQF:
  2072.             ASSERT(fr >= 1);
  2073.             DEC(fr, 2);
  2074.             fcmp;
  2075.             OutByte2(07AH, 003H);       (* jp L *)
  2076.             setcc(sete, al)
  2077.                                         (* L: *)
  2078.  
  2079.         |IL.opNEF:
  2080.             ASSERT(fr >= 1);
  2081.             DEC(fr, 2);
  2082.             fcmp;
  2083.             OutByte2(07AH, 003H);       (* jp L *)
  2084.             setcc(setne, al)
  2085.                                         (* L: *)
  2086.  
  2087.         |IL.opLTF:
  2088.             ASSERT(fr >= 1);
  2089.             DEC(fr, 2);
  2090.             fcmp;
  2091.             OutByte2(07AH, 00EH);       (* jp L *)
  2092.             setcc(setc, al);
  2093.             setcc(sete, ah);
  2094.             test(eax);
  2095.             setcc(sete, al);
  2096.             andrc(eax, 1)
  2097.                                         (* L: *)
  2098.  
  2099.         |IL.opGTF:
  2100.             ASSERT(fr >= 1);
  2101.             DEC(fr, 2);
  2102.             fcmp;
  2103.             OutByte2(07AH, 00FH);       (* jp L *)
  2104.             setcc(setc, al);
  2105.             setcc(sete, ah);
  2106.             cmprc(eax, 1);
  2107.             setcc(sete, al);
  2108.             andrc(eax, 1)
  2109.                                         (* L: *)
  2110.  
  2111.         |IL.opLEF:
  2112.             ASSERT(fr >= 1);
  2113.             DEC(fr, 2);
  2114.             fcmp;
  2115.             OutByte2(07AH, 003H);       (* jp L *)
  2116.             setcc(setnc, al)
  2117.                                         (* L: *)
  2118.  
  2119.         |IL.opGEF:
  2120.             ASSERT(fr >= 1);
  2121.             DEC(fr, 2);
  2122.             fcmp;
  2123.             OutByte2(07AH, 010H);       (* jp L *)
  2124.             setcc(setc, al);
  2125.             setcc(sete, ah);
  2126.             OutByte2(000H, 0E0H);       (* add al, ah *)
  2127.             OutByte2(03CH, 001H);       (* cmp al, 1 *)
  2128.             setcc(sete, al);
  2129.             andrc(eax, 1)
  2130.                                         (* L: *)
  2131.  
  2132.         |IL.opINF:
  2133.             INC(fr);
  2134.             IF fr > MAX_FR THEN
  2135.                 ERRORS.ErrorMsg(fname, param1, param2, FPR_ERR)
  2136.             END;
  2137.             LoadFltConst(UTILS.inf)
  2138.  
  2139.  
  2140.         |IL.opLADR_UNPK:
  2141.             n := param2 * 4;
  2142.             reg1 := GetAnyReg();
  2143.             OutByte2(8DH, 45H + reg1 * 8 + long(n));  (* lea reg1, dword[ebp + n] *)
  2144.             OutIntByte(n);
  2145.             BinOp(reg1, reg2);
  2146.             OutByte2(0DDH, reg1);         (* fld qword[reg1]   *)
  2147.             OutByte2(0D9H, 0F4H);         (* fxtract           *)
  2148.             OutByte2(0DDH, 018H + reg1);  (* fstp qword[reg1]  *)
  2149.             OutByte2(0DBH, 018H + reg2);  (* fistp dword[reg2] *)
  2150.             drop;
  2151.             drop
  2152.  
  2153.         |IL.opSADR_PARAM:
  2154.             IF pic THEN
  2155.                 reg1 := GetAnyReg();
  2156.                 Pic(reg1, BIN.PICDATA, stroffs + param2);
  2157.                 push(reg1);
  2158.                 drop
  2159.             ELSE
  2160.                 OutByte(068H);  (* push _data + stroffs + param2 *)
  2161.                 Reloc(BIN.RDATA, stroffs + param2)
  2162.             END
  2163.  
  2164.         |IL.opVADR_PARAM, IL.opLLOAD32_PARAM:
  2165.             pushm(ebp, param2 * 4)
  2166.  
  2167.         |IL.opCONST_PARAM:
  2168.             pushc(param2)
  2169.  
  2170.         |IL.opGLOAD32_PARAM:
  2171.             IF pic THEN
  2172.                 reg1 := GetAnyReg();
  2173.                 Pic(reg1, BIN.PICBSS, param2);
  2174.                 pushm(reg1, 0);
  2175.                 drop
  2176.             ELSE
  2177.                 OutByte2(0FFH, 035H);  (* push dword[_bss + param2] *)
  2178.                 Reloc(BIN.RBSS, param2)
  2179.             END
  2180.  
  2181.         |IL.opLOAD32_PARAM:
  2182.             UnOp(reg1);
  2183.             pushm(reg1, 0);
  2184.             drop
  2185.  
  2186.         |IL.opGADR_SAVEC:
  2187.             IF pic THEN
  2188.                 reg1 := GetAnyReg();
  2189.                 Pic(reg1, BIN.PICBSS, param1);
  2190.                 OutByte2(0C7H, reg1);  (* mov dword[reg1], param2 *)
  2191.                 OutInt(param2);
  2192.                 drop
  2193.             ELSE
  2194.                 OutByte2(0C7H, 05H);  (* mov dword[_bss + param1], param2 *)
  2195.                 Reloc(BIN.RBSS, param1);
  2196.                 OutInt(param2)
  2197.             END
  2198.  
  2199.         |IL.opLADR_SAVEC:
  2200.             n := param1 * 4;
  2201.             OutByte2(0C7H, 45H + long(n));  (* mov dword[ebp + n], param2 *)
  2202.             OutIntByte(n);
  2203.             OutInt(param2)
  2204.  
  2205.         |IL.opLADR_SAVE:
  2206.             UnOp(reg1);
  2207.             movmr(ebp, param2 * 4, reg1);
  2208.             drop
  2209.  
  2210.         |IL.opLADR_INCC:
  2211.             n := param1 * 4;
  2212.             IF ABS(param2) = 1 THEN
  2213.                 OutByte2(0FFH, 45H + 8 * ORD(param2 = -1) + long(n));  (* inc/dec dword[ebp + n] *)
  2214.                 OutIntByte(n)
  2215.             ELSE
  2216.                 OutByte2(81H + short(param2), 45H + long(n)); (* add dword[ebp + n], param2 *)
  2217.                 OutIntByte(n);
  2218.                 OutIntByte(param2)
  2219.             END
  2220.  
  2221.         |IL.opLADR_INCCB, IL.opLADR_DECCB:
  2222.             n := param1 * 4;
  2223.             IF param2 = 1 THEN
  2224.                 OutByte2(0FEH, 45H + 8 * ORD(opcode = IL.opLADR_DECCB) + long(n));  (* inc/dec byte[ebp + n] *)
  2225.                 OutIntByte(n)
  2226.             ELSE
  2227.                 OutByte2(80H, 45H + 28H * ORD(opcode = IL.opLADR_DECCB) + long(n)); (* add/sub byte[ebp + n], param2 *)
  2228.                 OutIntByte(n);
  2229.                 OutByte(param2 MOD 256)
  2230.             END
  2231.  
  2232.         |IL.opLADR_INC, IL.opLADR_DEC:
  2233.             n := param2 * 4;
  2234.             UnOp(reg1);
  2235.             OutByte2(01H + 28H * ORD(opcode = IL.opLADR_DEC), 45H + long(n) + reg1 * 8); (* add/sub dword[ebp + n], reg1 *)
  2236.             OutIntByte(n);
  2237.             drop
  2238.  
  2239.         |IL.opLADR_INCB, IL.opLADR_DECB:
  2240.             n := param2 * 4;
  2241.             UnOp(reg1);
  2242.             OutByte2(28H * ORD(opcode = IL.opLADR_DECB), 45H + long(n) + reg1 * 8); (* add/sub byte[ebp + n], reg1 *)
  2243.             OutIntByte(n);
  2244.             drop
  2245.  
  2246.         |IL.opLADR_INCL, IL.opLADR_EXCL:
  2247.             n := param2 * 4;
  2248.             UnOp(reg1);
  2249.             cmprc(reg1, 32);
  2250.             label := NewLabel();
  2251.             jcc(jnb, label);
  2252.             OutByte3(0FH, 0ABH + 8 * ORD(opcode = IL.opLADR_EXCL), 45H + long(n) + reg1 * 8); (* bts(r) dword[ebp + n], reg1 *)
  2253.             OutIntByte(n);
  2254.             SetLabel(label);
  2255.             drop
  2256.  
  2257.         |IL.opLADR_INCLC, IL.opLADR_EXCLC:
  2258.             n := param1 * 4;
  2259.             OutByte3(0FH, 0BAH, 6DH + long(n) + 8 * ORD(opcode = IL.opLADR_EXCLC)); (* bts(r) dword[ebp + n], param2 *)
  2260.             OutIntByte(n);
  2261.             OutByte(param2)
  2262.  
  2263.         |IL.opFNAME:
  2264.             fname := cmd(IL.FNAMECMD).fname
  2265.  
  2266.         END;
  2267.  
  2268.         cmd := cmd.next(COMMAND)
  2269.     END;
  2270.  
  2271.     ASSERT(R.pushed = 0);
  2272.     ASSERT(R.top = -1);
  2273.     ASSERT(fr = -1)
  2274. END translate;
  2275.  
  2276.  
  2277. PROCEDURE prolog (pic: BOOLEAN; target, stack, dllret: INTEGER): INTEGER;
  2278. VAR
  2279.     reg1, entry, L, dcount: INTEGER;
  2280.  
  2281. BEGIN
  2282.     entry := NewLabel();
  2283.     SetLabel(entry);
  2284.     dcount := CHL.Length(IL.codes.data);
  2285.  
  2286.     push(ebp);
  2287.     mov(ebp, esp);
  2288.     SetLabel(NewLabel());
  2289.     mainFltConstLabel := CodeList.last(LABEL);
  2290.     FltConstLabel := mainFltConstLabel;
  2291.     mainLocVarSize := 0;
  2292.     LocVarSize := 0;
  2293.  
  2294.     IF target = TARGETS.Win32DLL THEN
  2295.         pushm(ebp, 16);
  2296.         pushm(ebp, 12);
  2297.         pushm(ebp, 8);
  2298.         CallRTL(pic, IL._dllentry);
  2299.         test(eax);
  2300.         jcc(je, dllret);
  2301.         pushc(0)
  2302.     ELSIF target = TARGETS.KolibriOSDLL THEN
  2303.         OutByte(68H);  (* push IMPORT *)
  2304.         Reloc(BIN.IMPTAB, 0)
  2305.     ELSIF target = TARGETS.KolibriOS THEN
  2306.         reg1 := GetAnyReg();
  2307.         Pic(reg1, BIN.IMPTAB, 0);
  2308.         push(reg1);    (* push IMPORT *)
  2309.         drop
  2310.     ELSIF target = TARGETS.Linux32 THEN
  2311.         mov(eax, ebp);
  2312.         addrc(eax, 4);
  2313.         push(eax)
  2314.     ELSE
  2315.         pushc(0)
  2316.     END;
  2317.  
  2318.     IF pic THEN
  2319.         reg1 := GetAnyReg();
  2320.         Pic(reg1, BIN.PICCODE, entry);
  2321.         push(reg1);    (* push CODE *)
  2322.         Pic(reg1, BIN.PICDATA, 0);
  2323.         push(reg1);    (* push _data *)
  2324.         pushc(tcount);
  2325.         Pic(reg1, BIN.PICDATA, tcount * 4 + dcount);
  2326.         push(reg1);    (* push _data + tcount * 4 + dcount *)
  2327.         drop
  2328.     ELSE
  2329.         OutByte(68H);  (* push CODE *)
  2330.         Reloc(BIN.RCODE, entry);
  2331.         OutByte(68H);  (* push _data *)
  2332.         Reloc(BIN.RDATA, 0);
  2333.         pushc(tcount);
  2334.         OutByte(68H);  (* push _data + tcount * 4 + dcount *)
  2335.         Reloc(BIN.RDATA, tcount * 4 + dcount)
  2336.     END;
  2337.  
  2338.     CallRTL(pic, IL._init);
  2339.  
  2340.     IF target IN {TARGETS.Win32C, TARGETS.Win32GUI, TARGETS.Linux32} THEN
  2341.         L := NewLabel();
  2342.         pushc(0);
  2343.         push(esp);
  2344.         pushc(1024 * 1024 * stack);
  2345.         pushc(0);
  2346.         CallRTL(pic, IL._new);
  2347.         pop(eax);
  2348.         test(eax);
  2349.         jcc(je, L);
  2350.         addrc(eax, 1024 * 1024 * stack - 4);
  2351.         mov(esp, eax);
  2352.         SetLabel(L)
  2353.     END
  2354.  
  2355.     RETURN entry
  2356. END prolog;
  2357.  
  2358.  
  2359. PROCEDURE epilog (pic: BOOLEAN; modname: ARRAY OF CHAR; target, stack, ver, dllinit, dllret, sofinit: INTEGER);
  2360. VAR
  2361.     exp:  IL.EXPORT_PROC;
  2362.     path, name, ext: PATHS.PATH;
  2363.  
  2364.     dcount, i: INTEGER;
  2365.  
  2366.  
  2367.     PROCEDURE _import (imp: LISTS.LIST);
  2368.     VAR
  2369.         lib:  IL.IMPORT_LIB;
  2370.         proc: IL.IMPORT_PROC;
  2371.  
  2372.     BEGIN
  2373.  
  2374.         lib := imp.first(IL.IMPORT_LIB);
  2375.         WHILE lib # NIL DO
  2376.             BIN.Import(program, lib.name, 0);
  2377.             proc := lib.procs.first(IL.IMPORT_PROC);
  2378.             WHILE proc # NIL DO
  2379.                 BIN.Import(program, proc.name, proc.label);
  2380.                 proc := proc.next(IL.IMPORT_PROC)
  2381.             END;
  2382.             lib := lib.next(IL.IMPORT_LIB)
  2383.         END
  2384.  
  2385.     END _import;
  2386.  
  2387.  
  2388. BEGIN
  2389.  
  2390.     IF target IN {TARGETS.Win32C, TARGETS.Win32GUI, TARGETS.KolibriOS, TARGETS.Linux32} THEN
  2391.         pushc(0);
  2392.         CallRTL(pic, IL._exit);
  2393.     ELSIF target = TARGETS.Win32DLL THEN
  2394.         SetLabel(dllret);
  2395.         movrc(eax, 1);
  2396.         OutByte(0C9H); (* leave *)
  2397.         OutByte3(0C2H, 0CH, 0) (* ret 12 *)
  2398.     ELSIF target = TARGETS.KolibriOSDLL THEN
  2399.         movrc(eax, 1);
  2400.         OutByte(0C9H); (* leave *)
  2401.         ret
  2402.     ELSIF target = TARGETS.Linux32SO THEN
  2403.         OutByte(0C9H); (* leave *)
  2404.         ret;
  2405.         SetLabel(sofinit);
  2406.         CallRTL(pic, IL._sofinit);
  2407.         ret
  2408.     END;
  2409.  
  2410.     fixup;
  2411.  
  2412.     dcount := CHL.Length(IL.codes.data);
  2413.  
  2414.     FOR i := 0 TO tcount - 1 DO
  2415.         BIN.PutData32LE(program, CHL.GetInt(IL.codes.types, i))
  2416.     END;
  2417.  
  2418.     FOR i := 0 TO dcount - 1 DO
  2419.         BIN.PutData(program, CHL.GetByte(IL.codes.data, i))
  2420.     END;
  2421.  
  2422.     program.modname := CHL.Length(program.data);
  2423.  
  2424.     PATHS.split(modname, path, name, ext);
  2425.     BIN.PutDataStr(program, name);
  2426.     BIN.PutDataStr(program, ext);
  2427.     BIN.PutData(program, 0);
  2428.  
  2429.     IF target = TARGETS.KolibriOSDLL THEN
  2430.         BIN.Export(program, "lib_init", dllinit);
  2431.     END;
  2432.  
  2433.     exp := IL.codes.export.first(IL.EXPORT_PROC);
  2434.     WHILE exp # NIL DO
  2435.         BIN.Export(program, exp.name, exp.label);
  2436.         exp := exp.next(IL.EXPORT_PROC)
  2437.     END;
  2438.  
  2439.     _import(IL.codes._import);
  2440.  
  2441.     IL.set_bss(MAX(IL.codes.bss, MAX(IL.codes.dmin - CHL.Length(IL.codes.data), 4)));
  2442.  
  2443.     BIN.SetParams(program, IL.codes.bss, stack * (1024 * 1024), WCHR(ver DIV 65536), WCHR(ver MOD 65536))
  2444. END epilog;
  2445.  
  2446.  
  2447. PROCEDURE CodeGen* (outname: ARRAY OF CHAR; target: INTEGER; options: PROG.OPTIONS);
  2448. VAR
  2449.     dllret, dllinit, sofinit: INTEGER;
  2450.     opt: PROG.OPTIONS;
  2451.  
  2452. BEGIN
  2453.     FR[0] := 0;
  2454.     tcount := CHL.Length(IL.codes.types);
  2455.  
  2456.     opt := options;
  2457.     CodeList := LISTS.create(NIL);
  2458.  
  2459.     program := BIN.create(IL.codes.lcount);
  2460.  
  2461.     dllret  := NewLabel();
  2462.     sofinit := NewLabel();
  2463.  
  2464.     IF target = TARGETS.KolibriOSDLL THEN
  2465.         opt.pic := FALSE
  2466.     END;
  2467.  
  2468.     IF TARGETS.OS IN {TARGETS.osWIN32, TARGETS.osLINUX32} THEN
  2469.         opt.pic := TRUE
  2470.     END;
  2471.  
  2472.     REG.Init(R, push, pop, mov, xchg, {eax, ecx, edx});
  2473.  
  2474.     dllinit := prolog(opt.pic, target, opt.stack, dllret);
  2475.     translate(opt.pic, tcount * 4);
  2476.     epilog(opt.pic, outname, target, opt.stack, opt.version, dllinit, dllret, sofinit);
  2477.  
  2478.     BIN.fixup(program);
  2479.  
  2480.     IF TARGETS.OS = TARGETS.osWIN32 THEN
  2481.         PE32.write(program, outname, target = TARGETS.Win32C, target = TARGETS.Win32DLL, FALSE)
  2482.     ELSIF target = TARGETS.KolibriOS THEN
  2483.         KOS.write(program, outname)
  2484.     ELSIF target = TARGETS.KolibriOSDLL THEN
  2485.         MSCOFF.write(program, outname, opt.version)
  2486.     ELSIF TARGETS.OS = TARGETS.osLINUX32 THEN
  2487.         ELF.write(program, outname, sofinit, target = TARGETS.Linux32SO, FALSE)
  2488.     END
  2489.  
  2490. END CodeGen;
  2491.  
  2492.  
  2493. PROCEDURE SetProgram* (prog: BIN.PROGRAM);
  2494. BEGIN
  2495.     program := prog;
  2496.     CodeList := LISTS.create(NIL)
  2497. END SetProgram;
  2498.  
  2499.  
  2500. END X86.