more progress
This commit is contained in:
@@ -84,6 +84,7 @@ public:
|
||||
value *create_add(value *lhs, value *rhs, const std::string &name = "", bool has_nuw = false, bool has_nsw = false);
|
||||
value *create_sub(value *lhs, value *rhs, const std::string &name = "", bool has_nuw = false, bool has_nsw = false);
|
||||
value *create_shl(value *lhs, value *rhs, const std::string &name = "", bool has_nuw = false, bool has_nsw = false);
|
||||
value *create_lshr(value *lhs, value *rhs, const std::string &name = "", bool has_nuw = false, bool has_nsw = false);
|
||||
value *create_ashr(value *lhs, value *rhs, const std::string &name = "", bool has_nuw = false, bool has_nsw = false);
|
||||
// GEP
|
||||
value *create_gep(value *ptr, const std::vector<value*>& idx_list, const std::string &name = "");
|
||||
|
@@ -102,13 +102,14 @@ private:
|
||||
|
||||
/* constant fp */
|
||||
class constant_fp: public constant{
|
||||
constant_fp(context &ctx, double value);
|
||||
constant_fp(type *ty, double value);
|
||||
|
||||
public:
|
||||
double get_value() { return value_; }
|
||||
static constant* get_negative_zero(type *ty);
|
||||
static constant* get_zero_value_for_negation(type *ty);
|
||||
static constant *get(context &ctx, double v);
|
||||
static constant* get(context &ctx, double v);
|
||||
static constant* get(type *ty, double v);
|
||||
|
||||
private:
|
||||
double value_;
|
||||
|
@@ -32,7 +32,7 @@ public:
|
||||
// Int constants
|
||||
std::map<std::pair<type*, uint64_t>, constant_int*> int_constants_;
|
||||
// Float constants
|
||||
std::map<double, constant_fp*> fp_constants_;
|
||||
std::map<std::pair<type*, double>, constant_fp*> fp_constants_;
|
||||
// undef values
|
||||
std::map<type*, undef_value*> uv_constants_;
|
||||
// Metaparameters
|
||||
|
@@ -34,6 +34,7 @@ class alloc_const;
|
||||
/* Module */
|
||||
struct scope {
|
||||
std::map<std::string, ir::type*> types;
|
||||
std::map<std::string, ir::value*> values;
|
||||
};
|
||||
|
||||
class module {
|
||||
|
@@ -38,7 +38,8 @@ public:
|
||||
IntegerTyID, ///< 10: Arbitrary bit width integers
|
||||
FunctionTyID, ///< 11: Functions
|
||||
PointerTyID, ///< 12: Pointers
|
||||
TileTyID, ///< 13: Tile
|
||||
StructTyID, ///< 13: Struct
|
||||
TileTyID, ///< 14: Tile
|
||||
};
|
||||
|
||||
public:
|
||||
|
@@ -40,6 +40,7 @@ class Enumerator;
|
||||
// Statements
|
||||
class Stmt;
|
||||
class IfStmt;
|
||||
class ForStmt;
|
||||
class JumpStmt;
|
||||
class LabelStmt;
|
||||
class EmptyStmt;
|
||||
@@ -263,7 +264,7 @@ class Expr : public Stmt {
|
||||
template<typename T> friend class Evaluator;
|
||||
friend class AddrEvaluator;
|
||||
friend class Generator;
|
||||
friend class LValGenerator;
|
||||
friend class LValAssigner;
|
||||
|
||||
public:
|
||||
virtual ~Expr() {}
|
||||
@@ -308,7 +309,7 @@ class BinaryOp : public Expr {
|
||||
template<typename T> friend class Evaluator;
|
||||
friend class AddrEvaluator;
|
||||
friend class Generator;
|
||||
friend class LValGenerator;
|
||||
friend class LValAssigner;
|
||||
friend class Declaration;
|
||||
|
||||
public:
|
||||
@@ -375,7 +376,7 @@ class UnaryOp : public Expr {
|
||||
template<typename T> friend class Evaluator;
|
||||
friend class AddrEvaluator;
|
||||
friend class Generator;
|
||||
friend class LValGenerator;
|
||||
friend class LValAssigner;
|
||||
|
||||
public:
|
||||
static UnaryOp* New(int op, Expr* operand, QualType type=nullptr);
|
||||
@@ -538,7 +539,7 @@ class Identifier: public Expr {
|
||||
template<typename T> friend class Evaluator;
|
||||
friend class AddrEvaluator;
|
||||
friend class Generator;
|
||||
friend class LValGenerator;
|
||||
friend class LValAssigner;
|
||||
|
||||
public:
|
||||
static Identifier* New(const Token* tok, QualType type, Linkage linkage);
|
||||
@@ -596,7 +597,7 @@ class Object : public Identifier {
|
||||
template<typename T> friend class Evaluator;
|
||||
friend class AddrEvaluator;
|
||||
friend class Generator;
|
||||
friend class LValGenerator;
|
||||
friend class LValAssigner;
|
||||
|
||||
public:
|
||||
static Object* New(const Token* tok,
|
||||
|
@@ -3,90 +3,50 @@
|
||||
|
||||
#include "ast.h"
|
||||
#include "visitor.h"
|
||||
#include <stack>
|
||||
|
||||
namespace triton{
|
||||
namespace ir{
|
||||
|
||||
class value;
|
||||
class module;
|
||||
class type;
|
||||
class context;
|
||||
class builder;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
using namespace triton;
|
||||
|
||||
class Parser;
|
||||
struct Addr;
|
||||
struct ROData;
|
||||
template<> class Evaluator<Addr>;
|
||||
struct StaticInitializer;
|
||||
class LValAssigner;
|
||||
|
||||
using TypeList = std::vector<Type*>;
|
||||
using LocationList = std::vector<std::string>;
|
||||
using RODataList = std::vector<ROData>;
|
||||
using StaticInitList = std::vector<StaticInitializer>;
|
||||
|
||||
|
||||
enum class ParamClass {
|
||||
INTEGER,
|
||||
SSE,
|
||||
SSEUP,
|
||||
X87,
|
||||
X87_UP,
|
||||
COMPLEX_X87,
|
||||
NO_CLASS,
|
||||
MEMORY
|
||||
};
|
||||
|
||||
struct ParamLocations {
|
||||
LocationList locs_;
|
||||
size_t regCnt_;
|
||||
size_t xregCnt_;
|
||||
};
|
||||
|
||||
struct ROData {
|
||||
ROData(long ival, int align): ival_(ival), align_(align) {
|
||||
label_ = ".LC" + std::to_string(GenTag());
|
||||
}
|
||||
|
||||
explicit ROData(const std::string& sval): sval_(sval), align_(1) {
|
||||
label_ = ".LC" + std::to_string(GenTag());
|
||||
}
|
||||
|
||||
~ROData() {}
|
||||
|
||||
std::string sval_;
|
||||
long ival_;
|
||||
int align_;
|
||||
std::string label_;
|
||||
|
||||
private:
|
||||
static long GenTag() {
|
||||
static long tag = 0;
|
||||
return tag++;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
struct ObjectAddr {
|
||||
explicit ObjectAddr(int offset)
|
||||
: ObjectAddr("", "%rbp", offset) {}
|
||||
|
||||
ObjectAddr(const std::string& label, const std::string& base, int offset)
|
||||
: label_(label), base_(base), offset_(offset) {}
|
||||
|
||||
std::string Repr() const;
|
||||
|
||||
std::string label_;
|
||||
std::string base_;
|
||||
int offset_;
|
||||
unsigned char bitFieldBegin_ {0};
|
||||
unsigned char bitFieldWidth_ {0};
|
||||
};
|
||||
|
||||
|
||||
struct StaticInitializer {
|
||||
int offset_;
|
||||
int width_;
|
||||
long val_;
|
||||
std::string label_;
|
||||
};
|
||||
|
||||
// Error
|
||||
inline void should_not_happen() { assert(false); }
|
||||
inline void error_not_implemented() { assert(false); }
|
||||
|
||||
class Generator: public Visitor {
|
||||
friend class Evaluator<Addr>;
|
||||
friend class LValAssigner;
|
||||
|
||||
protected:
|
||||
struct scope {
|
||||
std::map<std::string, ir::type*> types;
|
||||
std::map<std::string, ir::value*> values;
|
||||
};
|
||||
|
||||
void set_ret(ir::value* value);
|
||||
|
||||
public:
|
||||
Generator() {}
|
||||
Generator(Parser* parser) : parser_(parser) {}
|
||||
|
||||
virtual void Visit(ASTNode* node) { node->Accept(this); }
|
||||
void VisitExpr(Expr* expr) { expr->Accept(this); }
|
||||
@@ -115,160 +75,75 @@ public:
|
||||
virtual void VisitFuncDef(FuncDef* funcDef);
|
||||
virtual void VisitTranslationUnit(TranslationUnit* unit);
|
||||
|
||||
|
||||
static void SetInOut(Parser* parser, FILE* outFile) {
|
||||
parser_ = parser;
|
||||
outFile_ = outFile;
|
||||
}
|
||||
|
||||
void Gen();
|
||||
void Gen(ir::module *mod);
|
||||
|
||||
protected:
|
||||
// Binary
|
||||
void GenCommaOp(BinaryOp* comma);
|
||||
void GenMemberRefOp(BinaryOp* binaryOp);
|
||||
void GenAndOp(BinaryOp* binaryOp);
|
||||
void GenOrOp(BinaryOp* binaryOp);
|
||||
void GenAddOp(BinaryOp* binaryOp);
|
||||
void GenSubOp(BinaryOp* binaryOp);
|
||||
void GenAssignOp(BinaryOp* assign);
|
||||
void GenCastOp(UnaryOp* cast);
|
||||
void GenDerefOp(UnaryOp* deref);
|
||||
void GenMinusOp(UnaryOp* minus);
|
||||
void GenPointerArithm(BinaryOp* binary);
|
||||
void GenDivOp(bool flt, bool sign, int width, int op);
|
||||
void GenMulOp(int width, bool flt, bool sign);
|
||||
void GenCompOp(int width, bool flt, const char* set);
|
||||
void GenCompZero(Type* type);
|
||||
// Triton-IR values
|
||||
ir::value* GenAssignOp(Expr* lvalue, ir::value* rhs);
|
||||
ir::value* GenCastOp(ir::value* op, ir::type* type);
|
||||
|
||||
// Unary
|
||||
void GenIncDec(Expr* operand, bool postfix, const std::string& inst);
|
||||
// Triton-IR types
|
||||
static ir::type* GenIRType(::Type* type, ir::context &ctx);
|
||||
static ir::type* GenIRArithmType(ArithmType* type, ir::context& ctx);
|
||||
static ir::type* GenIRArrayType(ArrayType* type, ir::context& ctx);
|
||||
static ir::type* GenIRTileType(TileType* type, ir::context& ctx);
|
||||
static ir::type* GenIRFuncType(FuncType* type, ir::context& ctx);
|
||||
static ir::type* GenIRPointerType(PointerType* type, ir::context& ctx);
|
||||
static ir::type* GenIRStructType(StructType* type, ir::context& ctx);
|
||||
void AllocObjects(Scope* scope, const FuncDef::ParamList& params=FuncDef::ParamList());
|
||||
|
||||
StaticInitializer GetStaticInit(InitList::iterator& iter,
|
||||
InitList::iterator end, int offset);
|
||||
// SSA
|
||||
void pushScope();
|
||||
void popScope();
|
||||
|
||||
void GenStaticDecl(Declaration* decl);
|
||||
private:
|
||||
Parser* parser_;
|
||||
ir::value* ret_;
|
||||
ir::builder* bld_;
|
||||
ir::context* ctx_;
|
||||
ir::module* mod_;
|
||||
|
||||
void GenSaveArea();
|
||||
void GenBuiltin(FuncCall* funcCall);
|
||||
|
||||
void AllocObjects(Scope* scope,
|
||||
const FuncDef::ParamList& params=FuncDef::ParamList());
|
||||
|
||||
void CopyStruct(ObjectAddr desAddr, int width);
|
||||
|
||||
std::string ConsLabel(Constant* cons);
|
||||
|
||||
ParamLocations GetParamLocations(const TypeList& types, bool retStruct);
|
||||
void GetParamRegOffsets(int& gpOffset, int& fpOffset,
|
||||
int& overflow, FuncType* funcType);
|
||||
|
||||
void Emit(const std::string& str) {
|
||||
fprintf(outFile_, "\t%s\n", str.c_str());
|
||||
}
|
||||
|
||||
void Emit(const std::string& inst,
|
||||
const std::string& src,
|
||||
const std::string& des) {
|
||||
Emit(inst + "\t" + src + ", " + des);
|
||||
}
|
||||
|
||||
void Emit(const std::string& inst,
|
||||
int imm,
|
||||
const std::string& reg) {
|
||||
Emit(inst + "\t$" + std::to_string(imm) + ", " + reg);
|
||||
}
|
||||
|
||||
void Emit(const std::string& inst,
|
||||
const std::string& des) {
|
||||
Emit(inst + "\t" + des);
|
||||
}
|
||||
|
||||
void Emit(const std::string& inst,
|
||||
const LabelStmt* label) {
|
||||
Emit(inst + "\t" + label->Repr());
|
||||
}
|
||||
|
||||
void Emit(const std::string& inst,
|
||||
const ObjectAddr& src,
|
||||
const ObjectAddr& des) {
|
||||
Emit(inst, src.Repr(), des.Repr());
|
||||
}
|
||||
|
||||
void Emit(const std::string& inst,
|
||||
const std::string& src,
|
||||
const ObjectAddr& des) {
|
||||
Emit(inst, src, des.Repr());
|
||||
}
|
||||
|
||||
void Emit(const std::string& inst,
|
||||
const ObjectAddr& src,
|
||||
const std::string& des) {
|
||||
Emit(inst, src.Repr(), des);
|
||||
}
|
||||
|
||||
void EmitLabel(const std::string& label);
|
||||
void EmitZero(ObjectAddr addr, int width);
|
||||
void EmitLoad(const std::string& addr, Type* type);
|
||||
void EmitLoad(const std::string& addr, int width, bool flt);
|
||||
void EmitStore(const ObjectAddr& addr, Type* type);
|
||||
void EmitStore(const std::string& addr, Type* type);
|
||||
void EmitStore(const std::string& addr, int width, bool flt);
|
||||
void EmitLoadBitField(const std::string& addr, Object* bitField);
|
||||
void EmitStoreBitField(const ObjectAddr& addr, Type* type);
|
||||
void EmitLoc(Expr* expr);
|
||||
|
||||
int Push(Type* type);
|
||||
int Push(const std::string& reg);
|
||||
int Pop(const std::string& reg);
|
||||
|
||||
void Spill(bool flt);
|
||||
|
||||
void Restore(bool flt);
|
||||
|
||||
void Save(bool flt);
|
||||
|
||||
void Exchange(bool flt);
|
||||
|
||||
protected:
|
||||
static const std::string* last_file;
|
||||
static Parser* parser_;
|
||||
static FILE* outFile_;
|
||||
static RODataList rodatas_;
|
||||
static int offset_;
|
||||
|
||||
// The address that store the register %rdi,
|
||||
// when the return value is a struct/union
|
||||
static int retAddrOffset_;
|
||||
static FuncDef* curFunc_;
|
||||
|
||||
static std::vector<Declaration*> staticDecls_;
|
||||
private:
|
||||
// std::stack<scope> scopes_;
|
||||
LValAssigner* assign_;
|
||||
};
|
||||
|
||||
|
||||
class LValGenerator: public Generator {
|
||||
class LValAssigner: public Visitor {
|
||||
public:
|
||||
LValGenerator() {}
|
||||
LValAssigner(Generator* gen): gen_(gen) {}
|
||||
|
||||
// Expression
|
||||
virtual void VisitBinaryOp(BinaryOp* binaryOp);
|
||||
virtual void VisitUnaryOp(UnaryOp* unaryOp);
|
||||
virtual void VisitObject(Object* obj);
|
||||
virtual void VisitIdentifier(Identifier* ident);
|
||||
void VisitBinaryOp(BinaryOp* binaryOp);
|
||||
void VisitUnaryOp(UnaryOp* unaryOp);
|
||||
void VisitObject(Object* obj);
|
||||
void VisitIdentifier(Identifier* ident);
|
||||
|
||||
virtual void VisitConditionalOp(ConditionalOp* condOp) { assert(false); }
|
||||
virtual void VisitFuncCall(FuncCall* funcCall) { assert(false); }
|
||||
virtual void VisitEnumerator(Enumerator* enumer) { assert(false); }
|
||||
virtual void VisitConstant(Constant* cons) { assert(false); }
|
||||
virtual void VisitTempVar(TempVar* tempVar);
|
||||
void VisitConditionalOp(ConditionalOp*) { should_not_happen(); }
|
||||
void VisitFuncCall(FuncCall*) { should_not_happen(); }
|
||||
void VisitEnumerator(Enumerator*) { should_not_happen(); }
|
||||
void VisitConstant(Constant*) { should_not_happen(); }
|
||||
void VisitTempVar(TempVar*) { should_not_happen(); }
|
||||
void VisitDeclaration(Declaration*) { should_not_happen(); }
|
||||
void VisitEmptyStmt(EmptyStmt*) { should_not_happen(); }
|
||||
void VisitIfStmt(IfStmt*) { should_not_happen(); }
|
||||
void VisitJumpStmt(JumpStmt*) { should_not_happen(); }
|
||||
void VisitReturnStmt(ReturnStmt*) { should_not_happen(); }
|
||||
void VisitLabelStmt(LabelStmt*) { should_not_happen(); }
|
||||
void VisitCompoundStmt(CompoundStmt*) { should_not_happen(); }
|
||||
void VisitFuncDef(FuncDef*) { should_not_happen(); }
|
||||
void VisitTranslationUnit(TranslationUnit*) { should_not_happen(); }
|
||||
|
||||
ObjectAddr GenExpr(Expr* expr) {
|
||||
ir::value* GenExpr(Expr* expr, ir::value* rhs) {
|
||||
rhs_ = rhs;
|
||||
expr->Accept(this);
|
||||
return addr_;
|
||||
return ret_;
|
||||
}
|
||||
|
||||
private:
|
||||
ObjectAddr addr_ {"", "", 0};
|
||||
ir::value* ret_;
|
||||
ir::value* rhs_;
|
||||
Generator* gen_;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@@ -1,122 +0,0 @@
|
||||
#ifndef _WGTCC_CODE_GEN_H_
|
||||
#define _WGTCC_CODE_GEN_H_
|
||||
|
||||
#include "ast.h"
|
||||
#include "visitor.h"
|
||||
|
||||
namespace triton{
|
||||
namespace ir{
|
||||
|
||||
class value;
|
||||
class module;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
using namespace triton;
|
||||
|
||||
class Parser;
|
||||
struct Addr;
|
||||
template<> class Evaluator<Addr>;
|
||||
struct StaticInitializer;
|
||||
|
||||
using TypeList = std::vector<Type*>;
|
||||
using LocationList = std::vector<std::string>;
|
||||
using StaticInitList = std::vector<StaticInitializer>;
|
||||
|
||||
|
||||
class Generator: public Visitor {
|
||||
friend class Evaluator<Addr>;
|
||||
public:
|
||||
Generator(Parser* parser, ir::module& mod) : parser_(parser), mod_(mod){}
|
||||
|
||||
virtual void Visit(ASTNode* node) { node->Accept(this); }
|
||||
void VisitExpr(Expr* expr) { expr->Accept(this); }
|
||||
void VisitStmt(Stmt* stmt) { stmt->Accept(this); }
|
||||
|
||||
// Expression
|
||||
virtual void VisitBinaryOp(BinaryOp* binaryOp);
|
||||
virtual void VisitUnaryOp(UnaryOp* unaryOp);
|
||||
virtual void VisitConditionalOp(ConditionalOp* condOp);
|
||||
virtual void VisitFuncCall(FuncCall* funcCall);
|
||||
virtual void VisitObject(Object* obj);
|
||||
virtual void VisitEnumerator(Enumerator* enumer);
|
||||
virtual void VisitIdentifier(Identifier* ident);
|
||||
virtual void VisitConstant(Constant* cons);
|
||||
virtual void VisitTempVar(TempVar* tempVar);
|
||||
|
||||
// Statement
|
||||
virtual void VisitDeclaration(Declaration* init);
|
||||
virtual void VisitEmptyStmt(EmptyStmt* emptyStmt);
|
||||
virtual void VisitIfStmt(IfStmt* ifStmt);
|
||||
virtual void VisitJumpStmt(JumpStmt* jumpStmt);
|
||||
virtual void VisitReturnStmt(ReturnStmt* returnStmt);
|
||||
virtual void VisitLabelStmt(LabelStmt* labelStmt);
|
||||
virtual void VisitCompoundStmt(CompoundStmt* compoundStmt);
|
||||
|
||||
virtual void VisitFuncDef(FuncDef* funcDef);
|
||||
virtual void VisitTranslationUnit(TranslationUnit* unit);
|
||||
|
||||
void Gen();
|
||||
|
||||
protected:
|
||||
// Binary
|
||||
void GenCommaOp(BinaryOp* comma);
|
||||
void GenMemberRefOp(BinaryOp* binaryOp);
|
||||
void GenAndOp(BinaryOp* binaryOp);
|
||||
void GenOrOp(BinaryOp* binaryOp);
|
||||
void GenAddOp(BinaryOp* binaryOp);
|
||||
void GenSubOp(BinaryOp* binaryOp);
|
||||
void GenAssignOp(BinaryOp* assign);
|
||||
void GenCastOp(UnaryOp* cast);
|
||||
void GenDerefOp(UnaryOp* deref);
|
||||
void GenMinusOp(UnaryOp* minus);
|
||||
void GenPointerArithm(BinaryOp* binary);
|
||||
void GenDivOp(bool flt, bool sign, int width, int op);
|
||||
void GenMulOp(int width, bool flt, bool sign);
|
||||
void GenCompOp(int width, bool flt, const char* set);
|
||||
void GenCompZero(Type* type);
|
||||
|
||||
// Unary
|
||||
void GenIncDec(Expr* operand, bool postfix, const std::string& inst);
|
||||
StaticInitializer GetStaticInit(InitList::iterator& iter,
|
||||
InitList::iterator end, int offset);
|
||||
void GenStaticDecl(Declaration* decl);
|
||||
void GenSaveArea();
|
||||
void GenBuiltin(FuncCall* funcCall);
|
||||
|
||||
void AllocObjects(Scope* scope,
|
||||
const FuncDef::ParamList& params=FuncDef::ParamList());
|
||||
|
||||
protected:
|
||||
Parser* parser_;
|
||||
ir::module& mod_;
|
||||
};
|
||||
|
||||
|
||||
class LValGenerator: public Generator {
|
||||
public:
|
||||
LValGenerator(Parser* parser, ir::module& mod): Generator(parser, mod) {}
|
||||
|
||||
// Expression
|
||||
virtual void VisitBinaryOp(BinaryOp* binaryOp);
|
||||
virtual void VisitUnaryOp(UnaryOp* unaryOp);
|
||||
virtual void VisitObject(Object* obj);
|
||||
virtual void VisitIdentifier(Identifier* ident);
|
||||
|
||||
virtual void VisitConditionalOp(ConditionalOp* condOp) { assert(false); }
|
||||
virtual void VisitFuncCall(FuncCall* funcCall) { assert(false); }
|
||||
virtual void VisitEnumerator(Enumerator* enumer) { assert(false); }
|
||||
virtual void VisitConstant(Constant* cons) { assert(false); }
|
||||
virtual void VisitTempVar(TempVar* tempVar);
|
||||
|
||||
ir::value* GenExpr(Expr* expr) {
|
||||
expr->Accept(this);
|
||||
return addr_;
|
||||
}
|
||||
|
||||
private:
|
||||
ir::value* addr_;
|
||||
};
|
||||
|
||||
#endif
|
@@ -185,6 +185,7 @@ DEFINE_NOWRAP_BINARY(add, binary_op_t::Add)
|
||||
DEFINE_NOWRAP_BINARY(sub, binary_op_t::Sub)
|
||||
DEFINE_NOWRAP_BINARY(shl, binary_op_t::Shl)
|
||||
DEFINE_NOWRAP_BINARY(ashr, binary_op_t::AShr)
|
||||
DEFINE_NOWRAP_BINARY(lshr, binary_op_t::LShr)
|
||||
DEFINE_BINARY_INT(sdiv, binary_op_t::SDiv)
|
||||
DEFINE_BINARY_INT(udiv, binary_op_t::UDiv)
|
||||
DEFINE_BINARY_INT(srem, binary_op_t::SRem)
|
||||
|
@@ -16,17 +16,11 @@ constant *constant::get_null_value(type *ty) {
|
||||
case type::IntegerTyID:
|
||||
return constant_int::get(ty, 0);
|
||||
case type::HalfTyID:
|
||||
return constant_fp::get(ctx, 0);
|
||||
return constant_fp::get(type::get_half_ty(ctx), 0);
|
||||
case type::FloatTyID:
|
||||
return constant_fp::get(ctx, 0);
|
||||
return constant_fp::get(type::get_float_ty(ctx), 0);
|
||||
case type::DoubleTyID:
|
||||
return constant_fp::get(ctx, 0);
|
||||
case type::X86_FP80TyID:
|
||||
return constant_fp::get(ctx, 0);
|
||||
case type::FP128TyID:
|
||||
return constant_fp::get(ctx, 0);
|
||||
case type::PPC_FP128TyID:
|
||||
return constant_fp::get(ctx, 0);
|
||||
return constant_fp::get(type::get_double_ty(ctx), 0);
|
||||
default:
|
||||
throw std::runtime_error("Cannot create a null constant of that type!");
|
||||
}
|
||||
@@ -38,7 +32,7 @@ constant *constant::get_all_ones_value(type *ty) {
|
||||
if(ty->is_integer_ty())
|
||||
return constant_int::get(ty, 0xFFFFFFFF);
|
||||
if(ty->is_floating_point_ty())
|
||||
return constant_fp::get(ty->get_context(), 0xFFFFFFFF);
|
||||
return constant_fp::get(ty, 0xFFFFFFFF);
|
||||
throw std::runtime_error("Cannot create all ones value for that type!");
|
||||
}
|
||||
|
||||
@@ -83,12 +77,12 @@ const constant_int* constant_range::get_last() const {
|
||||
// constant_fp
|
||||
// FIXME use something like APFloat
|
||||
|
||||
constant_fp::constant_fp(context &ctx, double value)
|
||||
: constant(type::get_float_ty(ctx), 0), value_(value){ }
|
||||
constant_fp::constant_fp(type *ty, double value)
|
||||
: constant(ty, 0), value_(value){ }
|
||||
|
||||
constant *constant_fp::get_negative_zero(type *ty){
|
||||
double neg_zero = 0;
|
||||
return get(ty->get_context(), neg_zero);
|
||||
return get(ty, neg_zero);
|
||||
}
|
||||
|
||||
constant *constant_fp::get_zero_value_for_negation(type *ty) {
|
||||
@@ -97,11 +91,11 @@ constant *constant_fp::get_zero_value_for_negation(type *ty) {
|
||||
return constant::get_null_value(ty);
|
||||
}
|
||||
|
||||
constant *constant_fp::get(context &ctx, double v){
|
||||
context_impl *impl = ctx.p_impl.get();
|
||||
constant_fp *&result = impl->fp_constants_[v];
|
||||
constant *constant_fp::get(type *ty, double v){
|
||||
context_impl *impl = ty->get_context().p_impl.get();
|
||||
constant_fp *&result = impl->fp_constants_[std::make_pair(ty, v)];
|
||||
if(!result)
|
||||
result = new constant_fp(ctx, v);
|
||||
result = new constant_fp(ty, v);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -28,226 +28,3 @@ static std::list<std::string> gcc_filenames_in;
|
||||
static std::list<std::string> gcc_args;
|
||||
static std::list<std::string> defines;
|
||||
static std::list<std::string> include_paths;
|
||||
|
||||
|
||||
static void Usage() {
|
||||
printf("Usage: wgtcc [options] file...\n"
|
||||
"Options: \n"
|
||||
" -h Display this information\n"
|
||||
" -D Define object like macro\n"
|
||||
" -I Add search path\n"
|
||||
" -E Preprocess only; do not compile, assemble or link\n"
|
||||
" -S Compile only; do not assemble or link\n"
|
||||
" -o specify output file\n");
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
||||
static std::string GetExtension(const std::string& filename) {
|
||||
return filename.substr(filename.size() >= 2 ? filename.size() - 2 : 0);
|
||||
}
|
||||
|
||||
|
||||
static void ValidateFileName(const std::string& filename) {
|
||||
auto ext = GetExtension(filename);
|
||||
if (ext != ".c" && ext != ".s" && ext != ".o" && ext != ".a")
|
||||
Error("bad file name format:'%s'", filename.c_str());
|
||||
}
|
||||
|
||||
|
||||
static void DefineMacro(Preprocessor& cpp, const std::string& def) {
|
||||
auto pos = def.find('=');
|
||||
std::string macro;
|
||||
std::string* replace;
|
||||
if (pos == std::string::npos) {
|
||||
macro = def;
|
||||
replace = new std::string();
|
||||
} else {
|
||||
macro = def.substr(0, pos);
|
||||
replace = new std::string(def.substr(pos + 1));
|
||||
}
|
||||
cpp.AddMacro(macro, replace);
|
||||
}
|
||||
|
||||
|
||||
static std::string GetName(const std::string& path) {
|
||||
auto pos = path.rfind('/');
|
||||
if (pos == std::string::npos)
|
||||
return path;
|
||||
return path.substr(pos + 1);
|
||||
}
|
||||
|
||||
static int RunWgtcc() {
|
||||
if (GetExtension(filename_in) != ".c")
|
||||
return -3;
|
||||
|
||||
Preprocessor cpp(&filename_in);
|
||||
for (auto& def: defines)
|
||||
DefineMacro(cpp, def);
|
||||
for (auto& path: include_paths)
|
||||
cpp.AddSearchPath(path);
|
||||
|
||||
FILE* fp = stdout;
|
||||
if (specified_out_name) {
|
||||
fp = fopen(filename_out.c_str(), "w");
|
||||
}
|
||||
TokenSequence ts;
|
||||
cpp.Process(ts);
|
||||
if (only_preprocess) {
|
||||
ts.Print(fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!only_compile || !specified_out_name) {
|
||||
filename_out = GetName(filename_in);
|
||||
filename_out.back() = 's';
|
||||
}
|
||||
fp = fopen(filename_out.c_str(), "w");
|
||||
|
||||
Parser parser(ts);
|
||||
parser.Parse();
|
||||
Generator::SetInOut(&parser, fp);
|
||||
Generator().Gen();
|
||||
fclose(fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int RunGcc() {
|
||||
// Froce C11
|
||||
bool spec_std = false;
|
||||
for (auto& arg: gcc_args) {
|
||||
if (arg.substr(0, 4) == "-std") {
|
||||
arg = "-std=c11";
|
||||
spec_std = true;
|
||||
}
|
||||
}
|
||||
if (!spec_std) {
|
||||
gcc_args.push_front("-std=c11");
|
||||
}
|
||||
|
||||
std::string systemArg = "gcc";
|
||||
for (const auto& arg: gcc_args) {
|
||||
systemArg += " " + arg;
|
||||
}
|
||||
auto ret = system(systemArg.c_str());
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static void ParseInclude(int argc, char* argv[], int& i) {
|
||||
if (argv[i][2]) {
|
||||
include_paths.push_front(&argv[i][2]);
|
||||
return;
|
||||
}
|
||||
|
||||
if (i == argc - 1) {
|
||||
Error("missing argument to '%s'", argv[i]);
|
||||
}
|
||||
include_paths.push_front(argv[++i]);
|
||||
gcc_args.push_back(argv[i]);
|
||||
}
|
||||
|
||||
|
||||
static void ParseDefine(int argc, char* argv[], int& i) {
|
||||
if (argv[i][2]) {
|
||||
defines.push_back(&argv[i][2]);
|
||||
return;
|
||||
}
|
||||
|
||||
if (i == argc - 1)
|
||||
Error("missing argument to '%s'", argv[i]);
|
||||
defines.push_back(argv[++i]);
|
||||
gcc_args.push_back(argv[i]);
|
||||
}
|
||||
|
||||
|
||||
static void ParseOut(int argc, char* argv[], int& i) {
|
||||
if (i == argc - 1)
|
||||
Error("missing argument to '%s'", argv[i]);
|
||||
filename_out = argv[++i];
|
||||
gcc_args.push_back(argv[i]);
|
||||
}
|
||||
|
||||
|
||||
/* Use:
|
||||
* wgtcc: compile
|
||||
* gcc: assemble and link
|
||||
* Allowing multi file may not be a good idea...
|
||||
*/
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc < 2)
|
||||
Usage();
|
||||
|
||||
program = std::string(argv[0]);
|
||||
for (auto i = 1; i < argc; ++i) {
|
||||
if (argv[i][0] != '-') {
|
||||
filename_in = std::string(argv[i]);
|
||||
ValidateFileName(filename_in);
|
||||
filenames_in.push_back(filename_in);
|
||||
continue;
|
||||
}
|
||||
|
||||
gcc_args.push_back(argv[i]);
|
||||
switch (argv[i][1]) {
|
||||
case 'h': Usage(); break;
|
||||
case 'E': only_preprocess = true; break;
|
||||
case 'S': only_compile = true; break;
|
||||
case 'I': ParseInclude(argc, argv, i); break;
|
||||
case 'D': ParseDefine(argc, argv, i); break;
|
||||
case 'o':
|
||||
specified_out_name = true;
|
||||
ParseOut(argc, argv, i); break;
|
||||
case 'g': gcc_args.pop_back(); debug = true; break;
|
||||
default:;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
RunWgtcc();
|
||||
#else
|
||||
for (const auto& filename: filenames_in) {
|
||||
filename_in = filename;
|
||||
pid_t pid = fork();
|
||||
if (pid < 0) {
|
||||
Error("fork error");
|
||||
} else if (pid == 0) {
|
||||
// Do work in child process
|
||||
return RunWgtcc();
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < filenames_in.size(); ++i) {
|
||||
int stat;
|
||||
wait(&stat);
|
||||
// Child process terminate normaly if :
|
||||
// 1. terminate with `exit()`, that is, WIFEXITED(stat) if true.
|
||||
// 2. the status code is 0, that is, WEXITSTATUS(stat) == 0
|
||||
if (!WIFEXITED(stat) || WEXITSTATUS(stat))
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (only_preprocess || only_compile) {
|
||||
if (specified_out_name && filenames_in.size() > 1)
|
||||
Error("cannot specifier output filename with multiple input file");
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::list<std::string> filenames_out;
|
||||
for (auto& filename: filenames_in) {
|
||||
if (GetExtension(filename) == ".c") {
|
||||
gcc_args.push_back(GetName(filename));
|
||||
gcc_args.back().back() = 's';
|
||||
} else {
|
||||
gcc_args.clear();
|
||||
for (int i = 1; i < argc; ++i)
|
||||
gcc_args.push_back(argv[i]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
auto ret = RunGcc();
|
||||
remove(filename_out.c_str());
|
||||
return ret;
|
||||
}
|
||||
|
@@ -1,8 +0,0 @@
|
||||
#include "triton/lang/wgtcc/t_code_gen.h"
|
||||
#include "triton/lang/wgtcc/evaluator.h"
|
||||
#include "triton/lang/wgtcc/parser.h"
|
||||
#include "triton/lang/wgtcc/token.h"
|
||||
|
||||
void Generator::Gen() {
|
||||
VisitTranslationUnit(parser_->Unit());
|
||||
}
|
Reference in New Issue
Block a user