home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / CROSSASM / AS68K.ZIP / AS6.C < prev   
Encoding:
C/C++ Source or Header  |  1987-01-10  |  16.1 KB  |  499 lines

  1. #include "a:printf.h"
  2. #include "a:stdio.h"
  3. #include "as.h"
  4.  
  5.  
  6. decopc(opidx,opskel,opsk2)
  7. unsigned    *opidx,*opskel,*opsk2;
  8. {
  9. /*
  10.        Lookup Op-code
  11.        Op-code starts at srclne[opptr]
  12. */
  13.  
  14.         static char psuop3[] = {'O','R','G','E','N','D','E','Q','U',
  15.                                 'N','A','M','S','E','T'};
  16.  
  17.         static char psuop4[] = {'R','O','R','G','P','A','G','E',
  18.                                 'L','I','S','T'};
  19.  
  20.         static char psuop5[] = {'N','L','I','S','T'};
  21.  
  22.         static char op4big[] = {'M','O','V','E','B','C','H','G',
  23.                 'B','C','L','R','B','S','E','T','B','T','S','T',
  24.                 'R','O','X','L','R','O','X','R'};
  25.  
  26.         static int op4opc[]  = {0x0000,0x0000,0x0140,0x0840,0x0180,
  27.                                 0x0880,0x01c0,0x08c0,0x0100,0x0800,
  28.                                 0xe110,0xe5c0,0xe010,0xe4c0};
  29.  
  30.         static int op4pty[]  = {3,10,10,10,10,8,8};
  31.  
  32.         static char op3big[] = {'A','D','D','A','S','L','A','S','R',
  33.                     'S','U','B','A','N','D','C','M','P','E','O','R',
  34.                     'L','S','L','L','S','R','R','O','L','R','O','R'};
  35.  
  36.         static int op3opc[]  = {0xd000,0x0600,0xe100,0xe1c0,0xe000,
  37.                                 0xe0c0,0x9000,0x0400,0xc000,0x0200,
  38.                                 0xb000,0x0c00,0xb100,0x0a00,0xe108,
  39.                                 0xe3c0,0xe008,0xe2c0,0xe118,0xe7c0,
  40.                                 0xe018,0xe6c0};
  41.  
  42.         static int op3pty[]  = {4,8,8,4,6,5,7,8,8,8,8};
  43.  
  44.         static char op3nam[] = {
  45.                 'B','E','Q','B','N','E','B','P','L',
  46.                 'B','M','I','B','G','T','B','L','T',
  47.                 'B','G','E','B','L','E','B','H','I',
  48.                 'B','L','S','B','C','S','B','C','C',
  49.                 'B','V','S','B','V','C','B','R','A',
  50.                 'B','S','R','C','H','K','C','L','R',
  51.                 'E','X','G','E','X','T','J','M','P',
  52.                 'J','S','R','L','D','M','L','E','A',
  53.                 'N','E','G','N','O','P','N','O','T',
  54.                 'P','E','A','R','T','E','R','T','R',
  55.                 'R','T','S','S','E','Q','S','N','E',
  56.                 'S','P','L','S','M','I','S','G','T',
  57.                 'S','L','T','S','G','E','S','L','E',
  58.                 'S','H','I','S','L','S','S','C','S',
  59.                 'S','C','C','S','T','M','S','V','S',
  60.                 'S','V','C','T','A','S','T','S','T'};
  61.  
  62.         static int op3ops[]  = {0x6700,0x6600,0x6a00,0x6b00,
  63.                                 0x6e00,0x6d00,0x6c00,0x6f00,
  64.                                 0x6200,0x6300,0x6500,0x6400,
  65.                                 0x6900,0x6800,0x6000,0x6100,
  66.                                 0x4180,0x4200,0xc000,0x4880,
  67.                                 0x4ec0,0x4e80,0x4c80,0x41c0,
  68.                                 0x4400,0x4e71,0x4600,0x4840,
  69.                                 0x4e73,0x4e77,0x4e75,0x57c0,
  70.                                 0x56c0,0x5ac0,0x5bc0,0x5ec0,
  71.                                 0x5dc0,0x5cc0,0x5fc0,0x52c0,
  72.                                 0x53c0,0x55c0,0x54c0,0x4880,
  73.                                 0x59c0,0x58c0,0x4ac0,0x4a00};
  74.  
  75.         static int op3typ[]  = {9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,11,12,
  76.                                 14,15,12,12,21,16,12,2,12,12,2,2,2,12,
  77.                                 12,12,12,12,12,12,12,12,12,12,12,12,12,
  78.                                 12,12,12};
  79.  
  80.         static char op4nam[] = {'A','B','C','D','A','D','D','X',
  81.                 'D','B','R','A','D','B','H','I','D','B','L','S',
  82.                 'D','B','C','C','D','B','C','S','D','B','N','E',
  83.                 'D','B','E','Q','D','B','V','C','D','B','V','S',
  84.                 'D','B','P','L','D','B','M','I','D','B','G','E',
  85.                 'D','B','L','T','D','B','G','T','D','B','L','E',
  86.                 'D','I','V','S','D','I','V','U','L','I','N','K',
  87.                 'M','U','L','S','M','U','L','U','N','B','C','D',
  88.                 'N','E','G','X','S','B','C','D','S','T','O','P',
  89.                 'S','U','B','X','S','W','A','P','T','R','A','P',
  90.                 'U','N','L','K'};
  91.  
  92.         static int op4ops[]  = {0xc100,0xd100,0x51c8,0x52c8,
  93.                                 0x53c8,0x54c8,0x55c8,0x56c8,
  94.                                 0x57c8,0x58c8,0x59c8,0x5ac8,
  95.                                 0x5bc8,0x5cc8,0x5dc8,0x5ec8,
  96.                                 0x5fc8,0x81c0,0x80c0,0x4e50,
  97.                                 0xc1c0,0xc0c0,0x4800,0x4000,
  98.                                 0x8100,0x4e72,0x9100,0x4840,
  99.                                 0x4e40,0x4e58};
  100.  
  101.         static int op4typ[]  = {19,19,13,13,13,13,13,13,13,13,
  102.                                 13,13,13,13,13,13,13,11,11,17,
  103.                                 11,11,12,12,19,22,19,15,18,20};
  104.  
  105.         static char op5nam[] = {'M','O','V','E','M',
  106.                                 'R','E','S','E','T',
  107.                                 'T','R','A','P','V'};
  108.  
  109.         static int op5ops[]  = {0x4880,0x4e70,0x4e76};
  110.  
  111.         int     i,optyp;
  112.         
  113.         /* start of op-code processing */
  114.  
  115.         *opskel = *opsk2 = 0;
  116.         scanpt = opptr;
  117.  
  118.         if (opclen <= 1 || opclen > 5)
  119.                 return 0;
  120.  
  121.         /* process op-code by size */
  122.  
  123.         switch (opclen) {
  124.  
  125.                 /* two character Op-codes */
  126.  
  127.                 case 2:
  128.  
  129.                         if (srclne[opptr] == 'O' && srclne[opptr+1] == 'R') {
  130.                                 *opidx = 0;
  131.                                 *opskel = 0x8000;
  132.                                 return 6;
  133.                         }
  134.  
  135.                         if (srclne[opptr] == 'D') {
  136.                                 if (srclne[opptr+1] == 'C') {
  137.                                         *opidx = 1;
  138.                                         return 1;
  139.                                 }
  140.  
  141.                                 if (srclne[opptr+1] == 'S') {
  142.                                         *opidx = 2;
  143.                                         return 1;
  144.                                 }
  145.                         }
  146.                         return 0;
  147.  
  148.                 /* three character Op-codes */
  149.  
  150.                 case 3:
  151.  
  152.                         optyp = oplook(5,3,psuop3,op3typ,0,opidx);
  153.                         if (optyp == 1) {
  154.                                 *opidx += 3;
  155.                                 return 1;
  156.                         }
  157.  
  158.                         optyp = oplook(11,3,op3big,op3pty,1,opidx);
  159.                         if (optyp != 0) {
  160.                                 *opskel = op3opc[(*opidx)*2];
  161.                                 *opsk2 = op3opc[((*opidx)*2)+1];
  162.                                 return optyp;
  163.                         }
  164.  
  165.                         optyp = oplook(48,3,op3nam,op3typ,1,opidx);
  166.                         *opskel = op3ops[*opidx];
  167.                         return optyp;
  168.  
  169.                 /* four character Op-codes */
  170.  
  171.                 case 4:
  172.                         optyp = oplook(3,4,psuop4,op3nam,0,opidx);
  173.                         if (optyp == 1) {
  174.                                 *opidx += 8;
  175.                                 return 1;
  176.                         }
  177.  
  178.                         optyp = oplook(7,4,op4big,op4pty,1,opidx);
  179.                         if (optyp != 0) {
  180.                                 *opskel = op4opc[(*opidx)*2];
  181.                                 *opsk2 = op4opc[((*opidx)*2)+1];
  182.                                 return optyp;
  183.                         }
  184.  
  185.                         optyp = oplook(30,4,op4nam,op4typ,1,opidx);
  186.                         if (optyp == 0) 
  187.                                 return 0;
  188.  
  189.                         *opskel = op4ops[*opidx];
  190.                         return optyp;
  191.  
  192.                 /* five character Op-codes */
  193.  
  194.                 case 5:
  195.                         optyp = oplook(1,5,psuop5,op3typ,0,opidx);
  196.                         if (optyp == 1) {
  197.                                 *opidx = 11;
  198.                                 return 1;
  199.                         }
  200.  
  201.                         optyp = oplook(3,5,op5nam,op5ops,1,opidx);
  202.                         if (optyp == 0)
  203.                                 return 0;
  204.  
  205.                         if (*opidx == 0)
  206.                                 optyp = 21;
  207.                         else
  208.                                 optyp = 2;
  209.  
  210.                         *opskel = op5ops[*opidx];
  211.                         return optyp;
  212.         }
  213. }
  214.  
  215.  
  216. oplook(isiz,istep,itbl,ityp,ipsf,opidx)
  217. unsigned    isiz,istep,ipsf,*ityp,*opidx;
  218. char            *itbl;
  219. {
  220.     int     i,j,is,found;
  221.  
  222.         for (i = 0; i < isiz; ++i) {
  223.                 is = i * istep;
  224.                 found = TRUE;
  225.  
  226.                 for (j = 0; j < istep; ++j) {
  227.                         if (srclne[opptr+j] != itbl[is+j]) {
  228.                                 found = FALSE;
  229.                                 break;
  230.                         }
  231.                 }
  232.  
  233.                 if (found)
  234.                         break;
  235.         }
  236.  
  237.         if (!found)
  238.                 return 0;
  239.  
  240.         *opidx = i;
  241.  
  242.         if (!ipsf)
  243.                 return 1;
  244.         else
  245.                 return ityp[i];
  246. }
  247.  
  248.  
  249. parse()
  250. {
  251.  
  252. /*
  253.     Parse incoming Source line
  254.  
  255.     IN:
  256.         srclne = line to be parsed
  257.         lnelen = length of source line
  258.  
  259.     OUT:
  260.         label    = Label field (label[0] = 0 if no label)
  261.         opptr    = pointer to Op-code field
  262.         opclen    = length of Op-code field not including mode
  263.         modptr    = pointer to Mode field
  264.         imode    = 0  no Mode field
  265.             = 1 .B
  266.             = 2 .W
  267.             = 3 .L
  268.             = 4 .S
  269.  
  270.         opnptr    = pointer to first operand
  271.         opnpt2    = pointer to second operand
  272.         cmtptr    = pointer to comment field
  273.         cmtflg    = comment flag - TRUE if line is comment only
  274.         prflg    = parse flag - zero if error detected
  275. */
  276.  
  277.         int     i,ic,ipf;
  278.  
  279.         ipf = imode = label[0] = scanpt = opclen =
  280.     opptr = opnptr = opnpt2 = modptr = cmtptr = 0;
  281.     cmtflg = FALSE;
  282.     prflg = 1;
  283.  
  284.         /* if null line ignore it */
  285.  
  286.         if (!lnelen) {
  287.                 prflg = 0;
  288.                 return;
  289.         }
  290.  
  291.         /* check for a comment line */
  292.  
  293.         if (srclne[0] == '*') {
  294.                 cmtflg = TRUE;
  295.                 return;
  296.         }
  297.  
  298.         /* see if Label present */
  299.  
  300.         if (srclne[scanpt] != ' ' && srclne[scanpt] != '\t') {
  301.  
  302.         /* Labels must start with A-Z */
  303.  
  304.                 if ((srclne[0] < 'A' || srclne[0] > 'Z') &&
  305.                     (srclne[0] != '@' && srclne[0] != '_')) {
  306.                         error(202);
  307.                         return;
  308.                 }
  309.  
  310.                 for (scanpt = 0; scanpt < SYMSIZ; ++scanpt) {
  311.                         if ((srclne[scanpt] >= '0' &&
  312.                             srclne[scanpt] <= '9') ||
  313.                            (srclne[scanpt] >= 'A' &&
  314.                             srclne[scanpt] <= 'Z') ||
  315.                             srclne[scanpt] == '_' ||
  316.                             srclne[scanpt] == '@')
  317.                                 label[scanpt] = srclne[scanpt];
  318.                         else
  319.                                 break;
  320.                 }
  321.  
  322.                 label[scanpt] = EOS;
  323.  
  324.                 if (scanpt == 2) {
  325.                         if (label[0] == 'A' || label[0] == 'D') {
  326.                                 if (label[1] >= '0' && label[1] <= '7') {
  327.                                         prflg = 0;
  328.                                         error(204);
  329.                                         return;
  330.                                 }
  331.                         }
  332.  
  333.                         if (label[0] == 'S') {
  334.                                 if (label[1] == 'P' || label[1] == 'R') {
  335.                                         prflg = 0;
  336.                                         error(204);
  337.                                         return;
  338.                                 }
  339.                         }
  340.                 }
  341.                 else if (scanpt == 3) {
  342.                         if (label[0] == 'C' && label[1] == 'C' &&
  343.                             label[2] == 'R') {
  344.                                 prflg = 0;
  345.                                 error(204);
  346.                                 return;
  347.                         }
  348.  
  349.                         if (label[0] == 'U' && label[1] == 'S' &&
  350.                             label[2] == 'P') {
  351.                                 prflg = 0;
  352.                                 error(204);
  353.                                 return;
  354.                         }
  355.                 }
  356.         }
  357.  
  358.         if (srclne[scanpt] != ' ' && srclne[scanpt] != ':' &&
  359.             srclne[scanpt] != '\t' && srclne[scanpt] != 0) {
  360.                 prflg = 0;
  361.                 error(205);
  362.                 return;
  363.         }
  364.  
  365.         if (srclne[scanpt] == EOS) {
  366.                 prflg = 1;
  367.                 return;
  368.         }
  369.  
  370.         ++scanpt;
  371.         prflg = 1;
  372.         while (srclne[scanpt] == ' ' || srclne[scanpt] == '\t')
  373.                 ++scanpt;
  374.  
  375.         if (srclne[scanpt] == EOS)
  376.                 return;
  377.  
  378.         opptr = scanpt;
  379.         for (i = 0; i < 5; ++i) {
  380.                 if (srclne[scanpt] < 'A' || srclne[scanpt] > 'Z')
  381.                         break;
  382.  
  383.                 ++scanpt;
  384.         }
  385.  
  386.         /* length of op-code is one less then scanned */
  387.  
  388.         opclen = i;
  389.  
  390.         /* check for end of line */
  391.  
  392.         if (srclne[scanpt] == EOS)
  393.                 return;
  394.  
  395.         /* check for XXX.X */
  396.  
  397.         if (srclne[scanpt] == '.') {
  398.                 ++scanpt;
  399.                 switch (srclne[scanpt]) {
  400.                         case 'S':       ++imode;
  401.                         case 'L':       ++imode;
  402.                         case 'W':       ++imode;
  403.                         case 'B':       ++imode;
  404.                                         modptr = scanpt++;
  405.                                         break;
  406.                         default:        opptr = prflg = 0;
  407.                                         error(207);
  408.                                         return;
  409.                 }
  410.         }
  411.  
  412.         if (srclne[scanpt] != ' ' && srclne[scanpt] != '\t') {
  413.                 opptr = prflg = 0;
  414.                 error(206);
  415.                 return;
  416.         }
  417.  
  418.         /* parse possible first operand */
  419.  
  420.         while ((ic = srclne[++scanpt]) == ' ' || ic == '\t') {
  421.                 if (ic == EOS)
  422.                         return;
  423.         }
  424.  
  425.         if (ic == '$' || ic == '*' || ic == '(' || ic == '\'' ||
  426.             ic == '-' || ic == '#' || (ic >= '0' && ic <= '9') ||
  427.             ic == '@' || ic == '_' || (ic >= 'A' && ic <= 'Z'))
  428.  
  429.     /* save pointer to start of first operand */
  430.  
  431.                 opnptr = scanpt;
  432.         else {
  433.                 opptr = prflg = 0;
  434.                 error(206);
  435.                 return;
  436.         }
  437.  
  438.     /* look for quotes */
  439.  
  440.         if (srclne[scanpt] == '\'' || srclne[scanpt+1] == '\'') {
  441.                 if (srclne[scanpt+1] == '\'')
  442.                         ++scanpt;
  443.  
  444.                 while (srclne[++scanpt] != '\'')
  445.                         if (srclne[scanpt] == EOS)
  446.                                 return;
  447.         }
  448.  
  449.         while (ic = srclne[++scanpt]) {
  450.                 if (ic == ' ')
  451.                         break;
  452.  
  453.                 if (ic == '(')
  454.                         ipf = 1;
  455.  
  456.                 if (ic == ')')
  457.                         ipf = 0;
  458.  
  459.                 if (ic == ',' && ipf == 0)
  460.                         break;
  461.         }
  462.  
  463.         if (ic == EOS)
  464.                 return;
  465.  
  466.         if (ic == ' ') {
  467.                 while (srclne[++scanpt] == ' ')
  468.             ;
  469.                 if (srclne[scanpt] == EOS)
  470.                         return;
  471.  
  472.                 cmtptr = scanpt;
  473.                 return;
  474.         }
  475.  
  476.         /* save start of possible second operand */
  477.  
  478.         opnpt2 = ++scanpt;
  479.         if (srclne[scanpt] == EOS)
  480.                 return;
  481.  
  482.         if (srclne[scanpt] == '\'') {
  483.                 while (srclne[++scanpt] != '\'')
  484.                         if (srclne[scanpt] == EOS)
  485.                                 return;
  486.                 ++scanpt;
  487.         }
  488.  
  489.         while (srclne[++scanpt] != ' ')
  490.                 if (srclne[scanpt] == EOS)
  491.                         return;
  492.  
  493.         while (srclne[++scanpt] == ' ')
  494.         ;
  495.  
  496.         cmtptr = scanpt;
  497.         return;
  498. }
  499.