Move Function closer to LLVM interface ( FunctionType etc. )

A few more places are using Commands.
This commit is contained in:
nemerle
2016-04-28 16:25:58 +02:00
parent 0684062130
commit 3d5a907b30
25 changed files with 416 additions and 151 deletions

View File

@@ -4,30 +4,35 @@
class QTextStream;
struct CConv {
enum Type {
enum CC_Type {
UNKNOWN=0,
C,
PASCAL
};
virtual void processHLI(Function *func, Expr *_exp, iICODE picode)=0;
//! given return and argument types fill Function's STKFRAME and return locations
virtual void calculateStackLayout(Function *func)=0;
virtual void writeComments(QTextStream &)=0;
static CConv * create(Type v);
static CConv * create(CC_Type v);
protected:
};
struct C_CallingConvention : public CConv {
virtual void processHLI(Function *func, Expr *_exp, iICODE picode);
virtual void writeComments(QTextStream &);
virtual void processHLI(Function *func, Expr *_exp, iICODE picode) override;
virtual void writeComments(QTextStream &) override;
void calculateStackLayout(Function *func) override;
private:
int processCArg(Function *callee, Function *pProc, ICODE *picode, size_t numArgs);
};
struct Pascal_CallingConvention : public CConv {
virtual void processHLI(Function *func, Expr *_exp, iICODE picode);
virtual void writeComments(QTextStream &);
virtual void processHLI(Function *func, Expr *_exp, iICODE picode) override;
virtual void writeComments(QTextStream &) override;
void calculateStackLayout(Function *func) override;
};
struct Unknown_CallingConvention : public CConv {
void processHLI(Function *func, Expr *_exp, iICODE picode) {}
virtual void writeComments(QTextStream &);
void processHLI(Function *func, Expr *_exp, iICODE picode) override {}
void calculateStackLayout(Function *func) override;
virtual void writeComments(QTextStream &) override;
};

View File

@@ -263,7 +263,8 @@ enum hlType
TYPE_STR, /* string */
TYPE_CONST, /* constant (any type) */
TYPE_FLOAT, /* floating point */
TYPE_DOUBLE /* double precision float */
TYPE_DOUBLE, /* double precision float */
TYPE_FUNC
};
/* Operand is defined, used or both flag */

View File

@@ -63,7 +63,7 @@ enum PROC_FLAGS
GRAPH_IRRED =0x00100000, /* Proc generates an irreducible graph */
SI_REGVAR =0x00200000, /* SI is used as a stack variable */
DI_REGVAR =0x00400000, /* DI is used as a stack variable */
PROC_IS_FUNC=0x00800000, /* Proc is a function */
//PROC_IS_FUNC=0x00800000, /* Proc is a function - determined by return type */
REG_ARGS =0x01000000, /* Proc has registers as arguments */
// PROC_VARARG =0x02000000, /* Proc has variable arguments */
PROC_OUTPUT =0x04000000, /* C for this proc has been output */
@@ -74,10 +74,43 @@ enum PROC_FLAGS
//#define CALL_MASK 0xFFFF9FFF /* Masks off CALL_C and CALL_PASCAL */
};
struct FunctionType
struct Type {
hlType dcc_type;
};
struct FunctionType : public Type
{
CConv * m_call_conv;
std::vector<Type> ContainedTys;
ID retVal; /* Return value - identifier */
bool m_vararg=false;
unsigned getNumParams() const { return ContainedTys.size(); }
bool isVarArg() const {return m_vararg;}
void setReturnType(hlType t) {
retVal.type = t;
}
void setReturnLocation(const LONGID_TYPE &v) {
retVal.loc = REG_FRAME;
retVal.longId() = v;
}
void setReturnLocation(eReg reg) {
retVal.loc = REG_FRAME;
retVal.id.regi = reg;
}
hlType getReturnType() const { return retVal.type; }
void addArgument(hlType hl) {
ContainedTys.push_back(Type {hl});
}
void clearArguments() { ContainedTys.clear(); }
void setCallingConvention(CConv::CC_Type cc);
static FunctionType *get(Type result,std::vector<Type> params, bool vararg_func) {
FunctionType * res = new FunctionType;
res->setReturnType(result.dcc_type);
std::swap(res->ContainedTys,params);
res->m_vararg = vararg_func;
return res;
}
};
struct Assignment
{
@@ -121,16 +154,17 @@ struct Function : public llvm::ilist_node<Function>
typedef BasicBlockListType::const_iterator const_iterator;
protected:
BasicBlockListType BasicBlocks; ///< The basic blocks
Function(FunctionType */*ty*/) : procEntry(0),depth(0),flg(0),cbParam(0),m_dfsLast(0),numBBs(0),
Function(FunctionType *ty) : procEntry(0),depth(0),flg(0),cbParam(0),m_dfsLast(0),numBBs(0),
hasCase(false),liveAnal(0)
{
type = new FunctionType;
type = ty;
if(!ty) // No type was provided, create it
type = new FunctionType;
callingConv(CConv::UNKNOWN);
}
public:
FunctionType * type;
CConv * m_call_conv;
uint32_t procEntry; /* label number */
QString name; /* Meaningful name for this proc */
STATE state; /* Entry state */
@@ -139,7 +173,6 @@ public:
int16_t cbParam; /* Probable no. of bytes of parameters */
STKFRAME args; /* Array of arguments */
LOCAL_ID localId; /* Local identifiers */
ID retVal; /* Return value - identifier */
/* Icodes and control flow graph */
CIcodeRec Icode; /* Object with ICODE records */
@@ -165,11 +198,14 @@ public:
r->name = nm;
return r;
}
hlType getReturnType() const {
return getFunctionType()->getReturnType();
}
FunctionType *getFunctionType() const {
return type;
}
CConv *callingConv() const { return m_call_conv;}
void callingConv(CConv::Type v);
CConv *callingConv() const { return type->m_call_conv;}
void callingConv(CConv::CC_Type v);
// bool anyFlagsSet(uint32_t t) { return (flg&t)!=0;}
bool hasRegArgs() const { return (flg & REG_ARGS)!=0;}

View File

@@ -313,7 +313,7 @@ struct LLOperand
{
return not (*this == LLOperand());
}
void addProcInformation(int param_count, CConv::Type call_conv);
void addProcInformation(int param_count, CConv::CC_Type call_conv);
bool isImmediate() const { return immed;}
void setImmediate(bool x) { immed=x;}
bool compound() const {return is_compound;} // dx:ax pair

View File

@@ -104,10 +104,7 @@ public:
char macro[10]; /* Macro for this identifier */
QString name; /* Identifier's name */
union ID_UNION { /* Different types of identifiers */
friend struct ID;
protected:
LONG_STKID_TYPE longStkId; /* For TYPE_LONG_(UN)SIGN on the stack */
public:
eReg regi; /* For TYPE_BYTE(WORD)_(UN)SIGN registers */
struct { /* For TYPE_BYTE(WORD)_(UN)SIGN on the stack */
uint8_t regOff; /* register offset (if any) */

View File

@@ -21,7 +21,10 @@ class QString;
class SourceMachine;
struct CALL_GRAPH;
struct DosLoader;
struct SegOffAddr {
uint16_t seg;
uint32_t addr;
};
class Project : public QObject
{
Q_OBJECT
@@ -30,8 +33,8 @@ public:
typedef FunctionListType lFunction;
typedef FunctionListType::iterator ilFunction;
DosLoader * m_selected_loader;
public:
DosLoader * m_selected_loader;
uint32_t SynthLab; //!< Last snthetic lab idx
SYMTAB symtab; //!< Global symbol table
FunctionListType pProcList; //!< List of located functions
@@ -50,10 +53,11 @@ public:
void create(const QString &a);
bool addLoadCommands();
bool addLoadCommands(QString fname);
void processAllCommands();
void resetCommandsAndErrorState();
const QString & output_path() const {return m_output_path;}
const QString & project_name() const {return m_project_name;}
const QString & binary_path() const {return m_fname;}
@@ -76,13 +80,16 @@ public:
const FunctionListType &functions() const { return pProcList; }
FunctionListType &functions() { return pProcList; }
template<class COMMANDCLASS>
bool addCommand() {
return m_project_command_stream.add(new COMMANDCLASS);
}
void dumpAllErrors();
bool addCommand(Command *cmd) { return m_project_command_stream.add(cmd); }
void dumpAllErrors();
void setLoader(DosLoader *ins);
public slots:
void onCommandStreamFinished(bool state);
void onCommandStreamFinished(bool state);
void onNewFunctionDiscovered(SegOffAddr ip,QString name,FunctionType *ft);
signals:
void newFunctionCreated(Function &);
void loaderSelected();
protected:
void initialize();
void writeGlobSymTable();
@@ -95,4 +102,3 @@ protected:
CommandContext m_command_ctx;
};
//extern Project g_proj;

View File

@@ -105,6 +105,8 @@ struct TypeContainer
return 4;
case TYPE_FLOAT:
return 4;
case TYPE_PTR:
return 2;
default:
return ~0;
}