home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mitsch75.zip / scheme-7_5_17-src.zip / scheme-7.5.17 / src / compiler / machines / i386 / instr2.scm < prev    next >
Text File  |  1999-01-02  |  13KB  |  561 lines

  1. #| -*-Scheme-*-
  2.  
  3. $Id: instr2.scm,v 1.6 1999/01/02 06:06:43 cph Exp $
  4.  
  5. Copyright (c) 1992, 1999 Massachusetts Institute of Technology
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. |#
  21.  
  22. ;;;; Intel i386 Instruction Set, part II
  23. ;;; package: (compiler lap-syntaxer)
  24.  
  25. ;; Some of the instructions have their operands ill-specified in the
  26. ;; i486 book.  Check against the appendices or the i386 book.
  27.  
  28. (declare (usual-integrations))
  29.  
  30. ;; Utility
  31.  
  32. (define-macro (define-trivial-instruction mnemonic opcode . extra)
  33.   `(define-instruction ,mnemonic
  34.      (()
  35.       (BYTE (8 ,opcode))
  36.       ,@(map (lambda (extra)
  37.            `(BYTE (8 ,extra)))
  38.          extra))))
  39.  
  40. ;;;; Actual instructions
  41.  
  42. (let-syntax
  43.     ((define-load-segment
  44.        (macro (mnemonic . bytes)
  45.      `(define-instruction ,mnemonic
  46.         (((R (? reg)) (? pointer mW))
  47.          (BYTE ,@(map (lambda (byte)
  48.                 `(8 ,byte))
  49.               bytes))
  50.          (ModR/M reg pointer))))))
  51.  
  52.   (define-load-segment LDS #xc5)
  53.   (define-load-segment LSS #x0f #xb2)
  54.   (define-load-segment LES #xc4)
  55.   (define-load-segment LFS #x0f #xb4)
  56.   (define-load-segment LGS #x0f #xb5))
  57.  
  58. (define-instruction LSL
  59.   (((R (? reg)) (? source r/mW))
  60.    (BYTE (8 #x0f)
  61.      (8 #x03))
  62.    (ModR/M reg source)))
  63.  
  64. (let-syntax
  65.     ((define-data-extension
  66.        (macro (mnemonic opcode)
  67.      `(define-instruction ,mnemonic
  68.         ((B (R (? target)) (? source r/mB))
  69.          (BYTE (8 #x0f)
  70.            (8 ,opcode))
  71.          (ModR/M target source))
  72.  
  73.         ((H (R (? target)) (? source r/mW))
  74.          (BYTE (8 #x0f)
  75.            (8 ,(1+ opcode)))
  76.          (ModR/M target source))))))
  77.  
  78.   (define-data-extension MOVSX #xbe)
  79.   (define-data-extension MOVZX #xb6))
  80.  
  81. (let-syntax
  82.     ((define-unary
  83.        (macro (mnemonic digit)
  84.      `(define-instruction ,mnemonic
  85.         ((W (? operand r/mW))
  86.          (BYTE (8 #xf7))
  87.          (ModR/M ,digit operand))
  88.  
  89.         ((B (? operand r/mB))
  90.          (BYTE (8 #xf6))
  91.          (ModR/M ,digit operand))))))
  92.  
  93.   (define-unary NEG 3)
  94.   (define-unary NOT 2))
  95.  
  96. (define-instruction MOV
  97.   ((W (R (? target)) (? source r/mW))
  98.    (BYTE (8 #x8b))
  99.    (ModR/M target source))
  100.  
  101.   ((W (? target r/mW) (R (? source)))
  102.    (BYTE (8 #x89))
  103.    (ModR/M source target))
  104.  
  105.   ((W (R (? reg)) (& (? value)))
  106.    (BYTE (8 (+ #xb8 reg)))
  107.    (IMMEDIATE value))
  108.  
  109.   ((W (? target r/mW) (& (? value)))
  110.    (BYTE (8 #xc7))
  111.    (ModR/M 0 target)
  112.    (IMMEDIATE value))
  113.  
  114.   ((W (R (? reg)) (&U (? value)))
  115.    (BYTE (8 (+ #xb8 reg)))
  116.    (IMMEDIATE value OPERAND UNSIGNED))
  117.  
  118.   ((W (? target r/mW) (&U (? value)))
  119.    (BYTE (8 #xc7))
  120.    (ModR/M 0 target)
  121.    (IMMEDIATE value OPERAND UNSIGNED))
  122.  
  123.   ((B (R (? target)) (? source r/mB))
  124.    (BYTE (8 #x8a))
  125.    (ModR/M target source))
  126.  
  127.   ((B (? target r/mB) (R (? source)))
  128.    (BYTE (8 #x88))
  129.    (ModR/M source target))
  130.  
  131.   ((B (R (? reg)) (& (? value)))
  132.    (BYTE (8 (+ #xb0 reg))
  133.      (8 value SIGNED)))
  134.  
  135.   ((B (? target r/mB) (& (? value)))
  136.    (BYTE (8 #xc6))
  137.    (ModR/M 0 target)
  138.    (BYTE (8 value SIGNED)))
  139.  
  140.   ((B (R (? reg)) (&U (? value)))
  141.    (BYTE (8 (+ #xb0 reg))
  142.      (8 value UNSIGNED)))
  143.  
  144.   ((B (? target r/mB) (&U (? value)))
  145.    (BYTE (8 #xc6))
  146.    (ModR/M 0 target)
  147.    (BYTE (8 value UNSIGNED)))
  148.  
  149.   ((W (R 0) (@ (? offset)))
  150.    (BYTE (8 #xa1))
  151.    (IMMEDIATE offset))
  152.  
  153.   ((W (@ (? offset)) (R 0))
  154.    (BYTE (8 #xa3))
  155.    (IMMEDIATE offset))
  156.  
  157.   ((B (R 0) (@ (? offset)))
  158.    (BYTE (8 #xa0)
  159.      (8 offset SIGNED)))
  160.  
  161.   ((B (@ (? offset)) (R 0))
  162.    (BYTE (8 #xa2)
  163.      (8 offset SIGNED)))
  164.  
  165.   (((? target r/mW) (SR (? source)))
  166.    (BYTE (8 #x8c))
  167.    (ModR/M source target))
  168.  
  169.   (((SR (? target)) (? source r/mW))
  170.    (BYTE (8 #x8e))
  171.    (ModR/M target source))
  172.  
  173.   (((CR (? creg)) (R (? reg)))
  174.    (BYTE (8 #x0f)
  175.      (8 #x22))
  176.    (ModR/M creg `(R ,reg)))
  177.  
  178.   (((R (? reg)) (CR (? creg)))
  179.    (BYTE (8 #x0f)
  180.      (8 #x20))
  181.    (ModR/M creg `(R ,reg)))
  182.  
  183.   (((DR (? dreg)) (R (? reg)))
  184.    (BYTE (8 #x0f)
  185.      (8 #x23))
  186.    (ModR/M dreg `(R ,reg)))
  187.  
  188.   (((R (? reg)) (DR (? dreg)))
  189.    (BYTE (8 #x0f)
  190.      (8 #x21))
  191.    (ModR/M dreg `(R ,reg)))
  192.  
  193.   (((TR (? treg)) (R (? reg)))
  194.    (BYTE (8 #x0f)
  195.      (8 #x26))
  196.    (ModR/M treg `(R ,reg)))
  197.  
  198.   (((R (? reg)) (TR (? treg)))
  199.    (BYTE (8 #x0f)
  200.      (8 #x24))
  201.    (ModR/M treg `(R ,reg))))
  202.  
  203. (define-trivial-instruction NOP #x90)
  204.  
  205. (define-instruction OUT
  206.   ((W (& (? port)) (R 0))
  207.    (BYTE (8 #xe7)
  208.      (8 port)))
  209.  
  210.   ((W (R 2) (R 0))
  211.    (BYTE (8 #xef)))
  212.  
  213.   ((B (& (? port)) (R 0))
  214.    (BYTE (8 #xe6)
  215.      (8 port)))
  216.  
  217.   ((B (R 2) (R 0))
  218.    (BYTE (8 #xee))))
  219.  
  220. (define-instruction POP
  221.   (((R (? target)))
  222.    (BYTE (8 (+ #x58 target))))
  223.  
  224.   (((? target mW))
  225.    (BYTE (8 #x8f))
  226.    (ModR/M 0 target))
  227.  
  228.   ((ES)
  229.    (BYTE (8 #x07)))
  230.  
  231.   ((SS)
  232.    (BYTE (8 #x17)))
  233.  
  234.   ((DS)
  235.    (BYTE (8 #x1f)))
  236.  
  237.   ((FS)
  238.    (BYTE (8 #x0f)
  239.      (8 #xa1)))
  240.  
  241.   ((GS)
  242.    (BYTE (8 #x0f)
  243.      (8 #xa9)))
  244.  
  245.   (((SR 0))
  246.    (BYTE (8 #x07)))
  247.  
  248.   (((SR 2))
  249.    (BYTE (8 #x17)))
  250.  
  251.   (((SR 3))
  252.    (BYTE (8 #x1f)))
  253.  
  254.   (((SR 4))
  255.    (BYTE (8 #x0f)
  256.      (8 #xa1)))
  257.  
  258.   (((SR 5))
  259.    (BYTE (8 #x0f)
  260.      (8 #xa9))))
  261.  
  262. (define-trivial-instruction POPA #x61)
  263. (define-trivial-instruction POPAD #x61)
  264. (define-trivial-instruction POPF #x9d)
  265. (define-trivial-instruction POPFD #x9d)
  266.  
  267. (define-instruction PUSH
  268.   (((R (? source)))
  269.    (BYTE (8 (+ #x50 source))))
  270.  
  271.   (((? source mW))
  272.    (BYTE (8 #xff))
  273.    (ModR/M 6 source))
  274.  
  275.   ((W (& (? value)))
  276.    (BYTE (8 #x68))
  277.    (IMMEDIATE value))
  278.  
  279.   ((W (&U (? value)))
  280.    (BYTE (8 #x68))
  281.    (IMMEDIATE value OPERAND UNSIGNED))
  282.  
  283.   ((B (& (? value)))
  284.    (BYTE (8 #x6a)
  285.      (8 value)))
  286.  
  287.   ((B (&U (? value)))
  288.    (BYTE (8 #x6a)
  289.      (8 value UNSIGNED)))
  290.  
  291.   ((ES)
  292.    (BYTE (8 #x06)))
  293.  
  294.   ((CS)
  295.    (BYTE (8 #x0e)))
  296.  
  297.   ((SS)
  298.    (BYTE (8 #x16)))
  299.  
  300.   ((DS)
  301.    (BYTE (8 #x1e)))
  302.  
  303.   ((FS)
  304.    (BYTE (8 #x0f)
  305.      (8 #xa0)))
  306.  
  307.   ((GS)
  308.    (BYTE (8 #x0f)
  309.      (8 #xa8)))
  310.  
  311.   (((SR 0))
  312.    (BYTE (8 #x06)))
  313.  
  314.   (((SR 1))
  315.    (BYTE (8 #x0e)))
  316.  
  317.   (((SR 2))
  318.    (BYTE (8 #x16)))
  319.  
  320.   (((SR 3))
  321.    (BYTE (8 #x1e)))
  322.  
  323.   (((SR 4))
  324.    (BYTE (8 #x0f)
  325.      (8 #xa0)))
  326.  
  327.   (((SR 5))
  328.    (BYTE (8 #x0f)
  329.      (8 #xa8))))
  330.  
  331. (define-trivial-instruction PUSHA  #x60)
  332. (define-trivial-instruction PUSHAD #x60)
  333. (define-trivial-instruction PUSHF  #x9c)
  334. (define-trivial-instruction PUSHFD #x9c)
  335.  
  336. (let-syntax
  337.     ((define-rotate/shift
  338.        (macro (mnemonic digit)
  339.      `(define-instruction ,mnemonic
  340.        ((W (? operand r/mW) (& 1))
  341.         (BYTE (8 #xd1))
  342.         (ModR/M ,digit operand))
  343.  
  344.        ((W (? operand r/mW) (& (? value)))
  345.         (BYTE (8 #xc1))
  346.         (ModR/M ,digit operand)
  347.         (BYTE (8 value)))
  348.  
  349.        ((W (? operand r/mW) (R 1))
  350.         (BYTE (8 #xd3))
  351.         (ModR/M ,digit operand))
  352.  
  353.        ((B (? operand r/mB) (& 1))
  354.         (BYTE (8 #xd0))
  355.         (ModR/M ,digit operand))
  356.  
  357.        ((B (? operand r/mB) (& (? value)))
  358.         (BYTE (8 #xc0))
  359.         (ModR/M ,digit operand)
  360.         (BYTE (8 value)))
  361.  
  362.        ((B (? operand r/mB) (R 1))
  363.         (BYTE (8 #xd2))
  364.         (ModR/M ,digit operand))))))
  365.  
  366.   (define-rotate/shift RCL 2)
  367.   (define-rotate/shift RCR 3)
  368.   (define-rotate/shift ROL 0)
  369.   (define-rotate/shift ROR 1)
  370.   (define-rotate/shift SAL 4)
  371.   (define-rotate/shift SAR 7)
  372.   (define-rotate/shift SHL 4)
  373.   (define-rotate/shift SHR 5))
  374.  
  375. (let-syntax
  376.     ((define-double-shift
  377.        (macro (mnemonic opcode)
  378.      `(define-instruction ,mnemonic
  379.         ((W (? target r/mW) (R (? source)) (& (? count)))
  380.          (BYTE (8 #x0f)
  381.            (8 ,opcode))
  382.          (ModR/M target source)
  383.          (BYTE (8 count)))
  384.  
  385.         ((W (? target r/mW) (R (? source)) (R 1))
  386.          (BYTE (8 #x0f)
  387.            (8 ,(1+ opcode)))
  388.          (ModR/M target source))))))
  389.  
  390.   (define-double-shift SHLD #xa4)
  391.   (define-double-shift SHRD #xac))
  392.  
  393. (define-instruction RET
  394.   (()
  395.    (BYTE (8 #xc3)))
  396.  
  397.   ((F)
  398.    (BYTE (8 #xcb)))
  399.  
  400.   (((& (? frame-size)))
  401.    (BYTE (8 #xc2)
  402.      (16 frame-size)))
  403.  
  404.   ((F (& (? frame-size)))
  405.    (BYTE (8 #xca)
  406.      (16 frame-size))))
  407.  
  408. (define-trivial-instruction SAHF #x9e)
  409.  
  410. (let-syntax
  411.     ((define-setcc-instruction
  412.        (macro (mnemonic opcode)
  413.      `(define-instruction ,mnemonic
  414.         (((? target r/mB))
  415.          (BYTE (8 #x0f)
  416.            (8 ,opcode))
  417.          (ModR/M 0 target))))))        ; 0?
  418.  
  419.   (define-setcc-instruction SETA   #x97)
  420.   (define-setcc-instruction SETAE  #x93)
  421.   (define-setcc-instruction SETB   #x92)
  422.   (define-setcc-instruction SETBE  #x96)
  423.   (define-setcc-instruction SETC   #x92)
  424.   (define-setcc-instruction SETE   #x94)
  425.   (define-setcc-instruction SETG   #x9f)
  426.   (define-setcc-instruction SETGE  #x9d)
  427.   (define-setcc-instruction SETL   #x9c)
  428.   (define-setcc-instruction SETLE  #x9e)
  429.   (define-setcc-instruction SETNA  #x96)
  430.   (define-setcc-instruction SETNAE #x92)
  431.   (define-setcc-instruction SETNB  #x93)
  432.   (define-setcc-instruction SETNBE #x97)
  433.   (define-setcc-instruction SETNC  #x93)
  434.   (define-setcc-instruction SETNE  #x95)
  435.   (define-setcc-instruction SETNG  #x9e)
  436.   (define-setcc-instruction SETNGE #x9c)
  437.   (define-setcc-instruction SETNL  #x9d)
  438.   (define-setcc-instruction SETNLE #x9f)
  439.   (define-setcc-instruction SETNO  #x91)
  440.   (define-setcc-instruction SETNP  #x9b)
  441.   (define-setcc-instruction SETNS  #x99)
  442.   (define-setcc-instruction SETNZ  #x95)
  443.   (define-setcc-instruction SETO   #x90)
  444.   (define-setcc-instruction SETP   #x9a)
  445.   (define-setcc-instruction SETPE  #x9a)
  446.   (define-setcc-instruction SETPO  #x9b)
  447.   (define-setcc-instruction SETS   #x98)
  448.   (define-setcc-instruction SETZ   #x94))
  449.  
  450. (define-trivial-instruction STC #xf9)
  451. (define-trivial-instruction STD #xfd)
  452. (define-trivial-instruction STI #xfb)
  453.  
  454. (define-instruction TEST
  455.   ((W (? op1 r/mW) (R (? op2)))
  456.    (BYTE (8 #x85))
  457.    (ModR/M op2 op1))
  458.  
  459.   ((W (R 0) (& (? value)))
  460.    (BYTE (8 #xa9))
  461.    (IMMEDIATE value))
  462.  
  463.   ((W (R 0) (&U (? value)))
  464.    (BYTE (8 #xa9))
  465.    (IMMEDIATE value OPERAND UNSIGNED))
  466.  
  467.   ((W (? op1 r/mW) (& (? value)))
  468.    (BYTE (8 #xf7))
  469.    (ModR/M 0 op1)
  470.    (IMMEDIATE value))
  471.  
  472.   ((W (? op1 r/mW) (&U (? value)))
  473.    (BYTE (8 #xf7))
  474.    (ModR/M 0 op1)
  475.    (IMMEDIATE value OPERAND UNSIGNED))
  476.  
  477.   ((B (? op1 r/mB) (R (? op2)))
  478.    (BYTE (8 #x84))
  479.    (ModR/M op2 op1))
  480.  
  481.   ((B (R 0) (& (? value)))
  482.    (BYTE (8 #xa8)
  483.      (8 value SIGNED)))
  484.  
  485.   ((B (R 0) (&U (? value)))
  486.    (BYTE (8 #xa8)
  487.      (8 value UNSIGNED)))
  488.  
  489.   ((B (? op1 r/mB) (& (? value)))
  490.    (BYTE (8 #xf6))
  491.    (ModR/M 0 op1)
  492.    (BYTE (8 value SIGNED)))
  493.  
  494.   ((B (? op1 r/mB) (&U (? value)))
  495.    (BYTE (8 #xf6))
  496.    (ModR/M 0 op1)
  497.    (BYTE (8 value UNSIGNED))))
  498.  
  499. (define-trivial-instruction WAIT #x9b)        ; = (FWAIT)
  500. (define-trivial-instruction WBINVD #x0f #x09)    ; 486 only
  501.  
  502. (define-instruction XADD            ; 486 only
  503.   ((W (? target r/mW) (R (? source)))
  504.    (BYTE (8 #x0f)
  505.      (8 #xc1))
  506.    (ModR/M source target))
  507.  
  508.   ((B (? target r/mB) (R (? source)))
  509.    (BYTE (8 #x0f)
  510.      (8 #xc0))
  511.    (ModR/M source target)))
  512.  
  513. (define-instruction XCHG
  514.   ((W (R 0) (R (? reg)))
  515.    (BYTE (8 (+ #x90 reg))))
  516.  
  517.   ((W (R (? reg)) (R 0))
  518.    (BYTE (8 (+ #x90 reg))))
  519.  
  520.   ((W (R (? reg)) (? op r/mW))
  521.    (BYTE (8 #x87))
  522.    (ModR/M reg op))
  523.  
  524.   ((W (? op r/mW) (R (? reg)))
  525.    (BYTE (8 #x87))
  526.    (ModR/M reg op))
  527.  
  528.   ((B (R (? reg)) (? op r/mB))
  529.    (BYTE (8 #x86))
  530.    (ModR/M reg op))
  531.  
  532.   ((B (? op r/mB) (R (? reg)))
  533.    (BYTE (8 #x86))
  534.    (ModR/M reg op)))
  535.  
  536. (define-trivial-instruction XLAT #xd7)
  537.  
  538. ;;;; Instruction prefixes.  Treated as separate instructions.
  539.  
  540. (define-trivial-instruction LOCK #xf0)
  541.  
  542. (define-trivial-instruction REP   #xf3)        ; or #xf2 trust which appendix?
  543. (define-trivial-instruction REPE  #xf3)
  544. (define-trivial-instruction REPNE #xf2)
  545. (define-trivial-instruction REPNZ #xf2)
  546. (define-trivial-instruction REPZ  #xf3)
  547.  
  548. (define-trivial-instruction CSSEG #x2e)
  549. (define-trivial-instruction SSSEG #x36)
  550. (define-trivial-instruction DSSEG #x3e)
  551. (define-trivial-instruction ESSEG #x26)
  552. (define-trivial-instruction FSSEG #x64)
  553. (define-trivial-instruction GSSEG #x65)
  554.  
  555. ;; **** These are broken.  The assembler needs to change state, i.e.
  556. ;; fluid-let *OPERAND-SIZE* or *ADDRESS-SIZE*. ****
  557.  
  558. (define-trivial-instruction OPSIZE #x66)
  559. (define-trivial-instruction ADSIZE #x67)
  560.  
  561. ;; **** Missing MOV instruction to/from special registers. ****