/* Copyright (C) 1996-1997 Id Software, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ // // gas to MASM source code converter // #include #include #include #define MAX_TOKENS 100 #define MAX_TOKEN_LENGTH 1024 #define LF 0x0A typedef enum {NOT_WHITESPACE, WHITESPACE, TOKEN_AVAILABLE, LINE_DONE, FILE_DONE, PARSED_OKAY} tokenstat; typedef enum {NOSEG, DATASEG, TEXTSEG} segtype; int tokennum; int linein, lineout; char *token; char tokens[MAX_TOKENS][MAX_TOKEN_LENGTH+1]; segtype currentseg = NOSEG; typedef struct { char *text; char *emit; int numtokens; void (*parsefunc) (void); } parsefield; void errorexit (void); //============================================== typedef struct { char *text; char *emit; int len; } regdesc; regdesc reglist[] = { {"%eax", "eax", 4}, {"%ebx", "ebx", 4}, {"%ecx", "ecx", 4}, {"%edx", "edx", 4}, {"%esi", "esi", 4}, {"%edi", "edi", 4}, {"%ebp", "ebp", 4}, {"%esp", "esp", 4}, {"%ax", "ax", 3}, {"%bx", "bx", 3}, {"%cx", "cx", 3}, {"%dx", "dx", 3}, {"%si", "si", 3}, {"%di", "di", 3}, {"%bp", "bp", 3}, {"%sp", "sp", 3}, {"%al", "al", 3}, {"%bl", "bl", 3}, {"%cl", "cl", 3}, {"%dl", "dl", 3}, {"%ah", "ah", 3}, {"%bh", "bh", 3}, {"%ch", "ch", 3}, {"%dh", "dh", 3}, {"%st(0)", "st(0)", 6}, {"%st(1)", "st(1)", 6}, {"%st(2)", "st(2)", 6}, {"%st(3)", "st(3)", 6}, {"%st(4)", "st(4)", 6}, {"%st(5)", "st(5)", 6}, {"%st(6)", "st(6)", 6}, {"%st(7)", "st(7)", 6}, }; int numregs = sizeof (reglist) / sizeof (reglist[0]); //============================================== void emitanoperand (int tnum, char *type, int notdata) { int i, index, something_outside_parens, regfound; int parencount; char *pt; char temp[MAX_TOKEN_LENGTH+1]; pt = tokens[tnum]; if (pt[0] == '%') { // register for (i=0 ; i '9') || (pt[2] < '0')) { i = 2; printf ("offset "); parencount = 1; while ((pt[i] != ')') || (parencount > 1)) { if (!pt[i]) { fprintf (stderr, "mismatched parens"); errorexit (); } if (pt[i] == ')') parencount--; else if (pt[i] == '(') parencount++; printf ("%c", pt[i]); i++; } } else { pt++; parencount = 1; for (i=1 ; (pt[i] != ')') || (parencount > 1) ; i++) { if (!pt[i]) { fprintf (stderr, "mismatched parens"); errorexit (); } if (pt[i] == ')') parencount--; else if (pt[i] == '(') parencount++; } pt[i] = 0; if ((pt[1] == '0') && ((pt[2] == 'x') || (pt[2] == 'X'))) { printf ("0%sh", &pt[3]); } else { printf ("%s", &pt[1]); } } } else if ((pt[1] == '0') && ((pt[2] == 'x') || (pt[2] == 'X'))) { printf ("0%sh", &pt[3]); } else if ((pt[1] >= '0') && (pt[1] <= '9')) { printf ("%s", &pt[1]); } else { printf ("offset %s", &pt[1]); } } else if (!notdata && ((pt[0] >= '0') && (pt[0] <= '9'))) { pt--; if ((pt[1] == '0') && ((pt[2] == 'x') || (pt[2] == 'X'))) { printf ("0%sh", &pt[3]); } else { printf ("%s", &pt[1]); } } else { // must be a memory location strcpy (temp, type); index = strlen (temp); if (notdata) temp[index++] = '['; something_outside_parens = 0; while (*pt) { if (index > (MAX_TOKEN_LENGTH - 10)) { fprintf (stderr, "Error: operand too long %s\n", tokens[tnum]); errorexit (); } if (*pt != ')') { if (*pt == '(') { if (something_outside_parens) temp[index++] = '+'; } else if (*pt == '%') { regfound = 0; for (i=0 ; i= '1') && (*pt <= '8')) { temp[index++] = '*'; temp[index++] = *pt; } else if (*pt != ')') { if (temp[index-1] != '+') temp[index++] = '+'; } } else { something_outside_parens = 1; // handle hexadecimal constants in addresses if ((*pt == '0') && ((*(pt+1) == 'x') || (*(pt+1) == 'X'))) { pt += 2; do { temp[index++] = *pt++; } while (((*pt >= '0') && (*pt <= '9')) || ((*pt >= 'a') && (*pt <= 'f')) || ((*pt >= 'A') && (*pt <= 'F'))); pt--; temp[index++] = 'h'; } else { temp[index++] = *pt; } } } pt++; } if (notdata) temp[index++] = ']'; temp[index] = 0; printf ("%s", temp); } } void datasegstart (void) { if (currentseg == DATASEG) return; if (currentseg == TEXTSEG) printf ("_TEXT ENDS\n"); printf ("_DATA SEGMENT"); currentseg = DATASEG; } void textsegstart (void) { if (currentseg == TEXTSEG) return; if (currentseg == DATASEG) printf ("_DATA ENDS\n"); printf ("_TEXT SEGMENT"); currentseg = TEXTSEG; } void emitdata (void) { int i; for (i=1 ; i<(tokennum-1) ; i++) printf (" %s,", tokens[i]); printf (" %s", tokens[tokennum-1]); } void emitonedata (void) { printf (" %s", tokens[1]); } void emitonecalldata (void) { int i, isaddr, len; if (tokens[1][0] == '*') { printf (" dword ptr[%s]", &tokens[1][1]); } else { isaddr = 0; len = strlen(tokens[1]); for (i=0 ; i 127) || (c == ',')) { return WHITESPACE; } return NOT_WHITESPACE; } int gettoken (void) { char c; int count, parencount; tokenstat stat; do { if ((c = getchar ()) == EOF) return FILE_DONE; if ((stat = whitespace (c)) == LINE_DONE) return LINE_DONE; } while (stat == WHITESPACE); token[0] = c; count = 1; if (c == '~') { count--; token[count++] = 'n'; token[count++] = 'o'; token[count++] = 't'; token[count++] = ' '; } if (c == '(') { do { if ((c = getchar ()) == EOF) { fprintf (stderr, "EOF in middle of parentheses\n"); errorexit (); } token[count++] = c; } while (c != ')'); } for ( ;; ) { if ((c = getchar ()) == EOF) { token[count] = 0; return TOKEN_AVAILABLE; } if (whitespace (c) == LINE_DONE) { if (ungetc (c, stdin) == EOF) { fprintf (stderr, "Couldn't unget character\n"); errorexit (); } token[count] = 0; return TOKEN_AVAILABLE; } if (whitespace (c) == WHITESPACE) { token[count] = 0; return TOKEN_AVAILABLE; } if (count >= MAX_TOKEN_LENGTH) { fprintf (stderr, "Error: token too long\n"); errorexit (); } token[count++] = c; if (c == '~') { count--; token[count++] = 'n'; token[count++] = 'o'; token[count++] = 't'; token[count++] = ' '; } else if (c == '(') { parencount = 1; do { if ((c = getchar ()) == EOF) { fprintf (stderr, "EOF in middle of parentheses\n"); errorexit (); } if (c == '(') parencount++; else if (c == ')') parencount--; if (c == '~') { token[count++] = 'n'; token[count++] = 'o'; token[count++] = 't'; token[count++] = ' '; } else { token[count++] = c; } } while ((c != ')') || (parencount > 0)); } } } tokenstat parseline (void) { tokenstat stat; int i, j, firsttoken, labelfound; int mnemfound; firsttoken = 1; tokennum = 0; labelfound = 0; for ( ;; ) { token = tokens[tokennum]; stat = gettoken (); switch (stat) { case FILE_DONE: return FILE_DONE; case LINE_DONE: if (!firsttoken && tokennum) { mnemfound = 0; for (i=0 ; i 0) && (parsedata[i].numtokens != tokennum)) || ((parsedata[i].numtokens < 0) && (tokennum < -parsedata[i].numtokens))) { fprintf (stderr, "mismatched number of tokens\n"); for (j=0 ; j= MAX_TOKENS) { fprintf (stderr, "Error: too many tokens\n"); exit (0); } tokennum++; break; default: fprintf (stderr, "Error: unknown tokenstat %d\n", stat); exit (0); } } } int main (int argc, char **argv) { tokenstat stat; printf (" .386P\n" " .model FLAT\n"); linein = 1; lineout = 3; for ( ;; ) { stat = parseline (); linein++; switch (stat) { case FILE_DONE: if (currentseg == TEXTSEG) printf ("_TEXT ENDS\n"); else if (currentseg == DATASEG) printf ("_DATA ENDS\n"); printf (" END\n"); return 0; case PARSED_OKAY: break; default: fprintf (stderr, "Error: unknown tokenstat %d\n", stat); return 1; } } }