diff --git a/include/locident.h b/include/locident.h index 0abd668..f161ab9 100644 --- a/include/locident.h +++ b/include/locident.h @@ -11,7 +11,7 @@ #include #include #include - +#include "types.h" #include "Enums.h" #include "machine_x86.h" @@ -28,12 +28,6 @@ struct IDX_ARRAY : public std::vector } }; -static constexpr const char * hlTypes[13] = { - "", "char", "unsigned char", "int", "unsigned int", - "long", "unsigned long", "record", "int *", "char *", - "", "float", "double" -}; - typedef enum { STK_FRAME, /* For stack vars */ @@ -97,14 +91,13 @@ struct ID bool isSigned() const { return (type==TYPE_BYTE_SIGN)||(type==TYPE_WORD_SIGN)||(type==TYPE_LONG_SIGN);} uint16_t typeBitsize() const { - switch(type) - { - case TYPE_WORD_SIGN: case TYPE_WORD_UNSIGN: - return 16; - case TYPE_BYTE_SIGN: case TYPE_BYTE_UNSIGN: - return 8; - } - return ~0; + return TypeContainer::typeSize(type)*8; + } + void setLocalName(int i) + { + // char buf[32]; + //sprintf (buf, "loc%ld", i); + //name=buf; } }; diff --git a/include/types.h b/include/types.h index ba6dd62..ab0cedd 100644 --- a/include/types.h +++ b/include/types.h @@ -3,9 +3,10 @@ * (C) Cristina Cifuentes, Mike van Emmerik ****************************************************************************/ #pragma once +#include #include +#include "Enums.h" /**** Common definitions and macros ****/ -typedef unsigned int uint32_t; /* 32 bits */ #define MAX 0x7FFFFFFF /* Type definitions used in the program */ @@ -14,14 +15,6 @@ typedef unsigned short word;/* 16 bits */ typedef short int16; /* 16 bits */ typedef unsigned char boolT; /* 8 bits */ -#if defined(__MSDOS__) | defined(WIN32) -#define unlink _unlink // Compiler is picky about non Ansi names -#endif - - -#define TRUE 1 -#define FALSE 0 - #define SYNTHESIZED_MIN 0x100000 /* Synthesized labs use bits 21..32 */ /* These are for C library signature detection */ @@ -73,3 +66,41 @@ struct eDuVal } bool isUSE_VAL() {return use&&val;} /* Use and Val */ }; +static constexpr const char * hlTypes[13] = { + "", "char", "unsigned char", "int", "unsigned int", + "long", "unsigned long", "record", "int *", "char *", + "", "float", "double" +}; + +struct TypeContainer +{ + hlType m_type; + size_t m_size; + TypeContainer(hlType t,size_t sz) : m_type(t),m_size(sz) + { + } + static size_t typeSize(hlType t) + { + switch(t) + { + case TYPE_WORD_SIGN: case TYPE_WORD_UNSIGN: + return 2; + case TYPE_BYTE_SIGN: case TYPE_BYTE_UNSIGN: + return 1; + } + return 0; + } + static hlType defaultTypeForSize(size_t x) + { + /* Type of the symbol according to the number of bytes it uses */ + static hlType cbType[] = {TYPE_UNKNOWN, TYPE_BYTE_UNSIGN, TYPE_WORD_SIGN, + TYPE_UNKNOWN, TYPE_LONG_SIGN}; + + assert(x < sizeof(cbType)/sizeof(hlType)); + return cbType[x]; + } + static constexpr const char *typeName(hlType t) + { + return hlTypes[t]; + } +}; diff --git a/src/ast.cpp b/src/ast.cpp index 2b772f0..40725b9 100644 --- a/src/ast.cpp +++ b/src/ast.cpp @@ -628,7 +628,7 @@ string walkCondExpr (const COND_EXPR* expr, Function * pProc, int* numLoc) bool needBracket; /* Determine whether parenthesis is needed */ BWGLB_TYPE* bwGlb; /* Ptr to BWGLB_TYPE (global indexed var) */ STKSYM * psym; /* Pointer to argument in the stack */ - std::ostringstream outStr; + std::ostringstream outStr,codeOut; if (expr == NULL) return ""; @@ -709,7 +709,8 @@ string walkCondExpr (const COND_EXPR* expr, Function * pProc, int* numLoc) if (id->name[0] == '\0') /* no name */ { sprintf (id->name, "loc%ld", ++(*numLoc)); - cCode.appendDecl("%s %s; /* %s */\n",hlTypes[id->type], id->name,Machine_X86::regName(id->id.regi).c_str()); + codeOut <type)<< " "<name<<"; "; + codeOut <<"/* "<id.regi)<<" */\n"; } if (id->hasMacro) o << id->macro << "("<name<<")"; @@ -752,9 +753,9 @@ string walkCondExpr (const COND_EXPR* expr, Function * pProc, int* numLoc) else if (id->loc == REG_FRAME) { sprintf (id->name, "loc%ld", ++(*numLoc)); - cCode.appendDecl("%s %s; /* %s:%s */\n",hlTypes[id->type], id->name, - Machine_X86::regName(id->id.longId.h).c_str(), - Machine_X86::regName(id->id.longId.l).c_str()); + codeOut <type)<< " "<name<<"; "; + codeOut <<"/* "<id.longId.h) << ":" << + Machine_X86::regName(id->id.longId.l) << " */\n"; o << id->name; pProc->localId.propLongId (id->id.longId.l,id->id.longId.h, id->name); } @@ -784,6 +785,7 @@ string walkCondExpr (const COND_EXPR* expr, Function * pProc, int* numLoc) outStr << o.str(); break; } + cCode.appendDecl(codeOut.str()); return outStr.str(); } diff --git a/src/backend.cpp b/src/backend.cpp index 37dc9c9..3e5fe18 100644 --- a/src/backend.cpp +++ b/src/backend.cpp @@ -218,7 +218,7 @@ void Function::codeGen (std::ostream &fs) /* Write arguments */ for (size_t i = 0; i < args.sym.size(); i++) { - if (args.sym[i].invalid == FALSE) + if (args.sym[i].invalid == false) { buf<