mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 14:48:21 +00:00
binpac: Add pre-commit hooks and run clang-format on everything
This commit is contained in:
parent
090ac0a6e0
commit
090325df40
91 changed files with 3086 additions and 3665 deletions
|
@ -1,7 +1,8 @@
|
|||
#include "pac_expr.h"
|
||||
|
||||
#include "pac_case.h"
|
||||
#include "pac_cstr.h"
|
||||
#include "pac_exception.h"
|
||||
#include "pac_expr.h"
|
||||
#include "pac_exttype.h"
|
||||
#include "pac_id.h"
|
||||
#include "pac_number.h"
|
||||
|
@ -12,13 +13,13 @@
|
|||
#include "pac_typedecl.h"
|
||||
#include "pac_utils.h"
|
||||
|
||||
string OrigExprList(ExprList *list)
|
||||
string OrigExprList(ExprList* list)
|
||||
{
|
||||
bool first = true;
|
||||
string str;
|
||||
foreach(i, ExprList, list)
|
||||
foreach (i, ExprList, list)
|
||||
{
|
||||
Expr *expr = *i;
|
||||
Expr* expr = *i;
|
||||
if ( first )
|
||||
first = false;
|
||||
else
|
||||
|
@ -28,12 +29,12 @@ string OrigExprList(ExprList *list)
|
|||
return str;
|
||||
}
|
||||
|
||||
string EvalExprList(ExprList *exprlist, Output *out, Env *env)
|
||||
string EvalExprList(ExprList* exprlist, Output* out, Env* env)
|
||||
{
|
||||
string val_list("");
|
||||
bool first = true;
|
||||
|
||||
foreach(i, ExprList, exprlist)
|
||||
foreach (i, ExprList, exprlist)
|
||||
{
|
||||
if ( ! first )
|
||||
val_list += ", ";
|
||||
|
@ -44,11 +45,10 @@ string EvalExprList(ExprList *exprlist, Output *out, Env *env)
|
|||
return val_list;
|
||||
}
|
||||
|
||||
static const char* expr_fmt[] =
|
||||
{
|
||||
# define EXPR_DEF(type, num_op, fmt) fmt,
|
||||
# include "pac_expr.def"
|
||||
# undef EXPR_DEF
|
||||
static const char* expr_fmt[] = {
|
||||
#define EXPR_DEF(type, num_op, fmt) fmt,
|
||||
#include "pac_expr.def"
|
||||
#undef EXPR_DEF
|
||||
};
|
||||
|
||||
void Expr::init()
|
||||
|
@ -65,8 +65,7 @@ void Expr::init()
|
|||
cases_ = 0;
|
||||
}
|
||||
|
||||
Expr::Expr(ID* arg_id)
|
||||
: DataDepElement(EXPR)
|
||||
Expr::Expr(ID* arg_id) : DataDepElement(EXPR)
|
||||
{
|
||||
init();
|
||||
expr_type_ = EXPR_ID;
|
||||
|
@ -75,8 +74,7 @@ Expr::Expr(ID* arg_id)
|
|||
orig_ = strfmt("%s", id_->Name());
|
||||
}
|
||||
|
||||
Expr::Expr(Number* arg_num)
|
||||
: DataDepElement(EXPR)
|
||||
Expr::Expr(Number* arg_num) : DataDepElement(EXPR)
|
||||
{
|
||||
init();
|
||||
expr_type_ = EXPR_NUM;
|
||||
|
@ -85,8 +83,7 @@ Expr::Expr(Number* arg_num)
|
|||
orig_ = strfmt("((int) %s)", num_->Str());
|
||||
}
|
||||
|
||||
Expr::Expr(ConstString *cstr)
|
||||
: DataDepElement(EXPR)
|
||||
Expr::Expr(ConstString* cstr) : DataDepElement(EXPR)
|
||||
{
|
||||
init();
|
||||
expr_type_ = EXPR_CSTR;
|
||||
|
@ -95,8 +92,7 @@ Expr::Expr(ConstString *cstr)
|
|||
orig_ = cstr_->str();
|
||||
}
|
||||
|
||||
Expr::Expr(RegEx *regex)
|
||||
: DataDepElement(EXPR)
|
||||
Expr::Expr(RegEx* regex) : DataDepElement(EXPR)
|
||||
{
|
||||
init();
|
||||
expr_type_ = EXPR_REGEX;
|
||||
|
@ -105,8 +101,7 @@ Expr::Expr(RegEx *regex)
|
|||
orig_ = strfmt("/%s/", regex_->str().c_str());
|
||||
}
|
||||
|
||||
Expr::Expr(ExprType arg_type, Expr* op1)
|
||||
: DataDepElement(EXPR)
|
||||
Expr::Expr(ExprType arg_type, Expr* op1) : DataDepElement(EXPR)
|
||||
{
|
||||
init();
|
||||
expr_type_ = arg_type;
|
||||
|
@ -115,8 +110,7 @@ Expr::Expr(ExprType arg_type, Expr* op1)
|
|||
orig_ = strfmt(expr_fmt[expr_type_], op1->orig());
|
||||
}
|
||||
|
||||
Expr::Expr(ExprType arg_type, Expr* op1, Expr* op2)
|
||||
: DataDepElement(EXPR)
|
||||
Expr::Expr(ExprType arg_type, Expr* op1, Expr* op2) : DataDepElement(EXPR)
|
||||
{
|
||||
init();
|
||||
expr_type_ = arg_type;
|
||||
|
@ -127,8 +121,7 @@ Expr::Expr(ExprType arg_type, Expr* op1, Expr* op2)
|
|||
orig_ = strfmt(expr_fmt[expr_type_], op1->orig(), op2->orig());
|
||||
}
|
||||
|
||||
Expr::Expr(ExprType arg_type, Expr* op1, Expr* op2, Expr* op3)
|
||||
: DataDepElement(EXPR)
|
||||
Expr::Expr(ExprType arg_type, Expr* op1, Expr* op2, Expr* op3) : DataDepElement(EXPR)
|
||||
{
|
||||
init();
|
||||
expr_type_ = arg_type;
|
||||
|
@ -139,8 +132,7 @@ Expr::Expr(ExprType arg_type, Expr* op1, Expr* op2, Expr* op3)
|
|||
orig_ = strfmt(expr_fmt[expr_type_], op1->orig(), op2->orig(), op3->orig());
|
||||
}
|
||||
|
||||
Expr::Expr(ExprList *args)
|
||||
: DataDepElement(EXPR)
|
||||
Expr::Expr(ExprList* args) : DataDepElement(EXPR)
|
||||
{
|
||||
init();
|
||||
expr_type_ = EXPR_CALLARGS;
|
||||
|
@ -150,8 +142,7 @@ Expr::Expr(ExprList *args)
|
|||
orig_ = OrigExprList(args_);
|
||||
}
|
||||
|
||||
Expr::Expr(Expr *index, CaseExprList *cases)
|
||||
: DataDepElement(EXPR)
|
||||
Expr::Expr(Expr* index, CaseExprList* cases) : DataDepElement(EXPR)
|
||||
{
|
||||
init();
|
||||
expr_type_ = EXPR_CASE;
|
||||
|
@ -160,12 +151,10 @@ Expr::Expr(Expr *index, CaseExprList *cases)
|
|||
cases_ = cases;
|
||||
|
||||
orig_ = strfmt("case %s of { ", index->orig());
|
||||
foreach(i, CaseExprList, cases_)
|
||||
foreach (i, CaseExprList, cases_)
|
||||
{
|
||||
CaseExpr *c = *i;
|
||||
orig_ += strfmt("%s => %s; ",
|
||||
OrigExprList(c->index()).c_str(),
|
||||
c->value()->orig());
|
||||
CaseExpr* c = *i;
|
||||
orig_ += strfmt("%s => %s; ", OrigExprList(c->index()).c_str(), c->value()->orig());
|
||||
}
|
||||
orig_ += "}";
|
||||
}
|
||||
|
@ -180,7 +169,7 @@ Expr::~Expr()
|
|||
delete_list(CaseExprList, cases_);
|
||||
}
|
||||
|
||||
void Expr::AddCaseExpr(CaseExpr *case_expr)
|
||||
void Expr::AddCaseExpr(CaseExpr* case_expr)
|
||||
{
|
||||
ASSERT(str_.empty());
|
||||
ASSERT(expr_type_ == EXPR_CASE);
|
||||
|
@ -188,7 +177,7 @@ void Expr::AddCaseExpr(CaseExpr *case_expr)
|
|||
cases_->push_back(case_expr);
|
||||
}
|
||||
|
||||
void Expr::GenStrFromFormat(Env *env)
|
||||
void Expr::GenStrFromFormat(Env* env)
|
||||
{
|
||||
// The format != "@custom@"
|
||||
ASSERT(*expr_fmt[expr_type_] != '@');
|
||||
|
@ -196,20 +185,15 @@ void Expr::GenStrFromFormat(Env *env)
|
|||
switch ( num_operands_ )
|
||||
{
|
||||
case 1:
|
||||
str_ = strfmt(expr_fmt[expr_type_],
|
||||
operand_[0]->str());
|
||||
break;
|
||||
str_ = strfmt(expr_fmt[expr_type_], operand_[0]->str());
|
||||
break;
|
||||
case 2:
|
||||
str_ = strfmt(expr_fmt[expr_type_],
|
||||
operand_[0]->str(),
|
||||
operand_[1]->str());
|
||||
break;
|
||||
str_ = strfmt(expr_fmt[expr_type_], operand_[0]->str(), operand_[1]->str());
|
||||
break;
|
||||
case 3:
|
||||
str_ = strfmt(expr_fmt[expr_type_],
|
||||
operand_[0]->str(),
|
||||
operand_[1]->str(),
|
||||
operand_[2]->str());
|
||||
break;
|
||||
str_ = strfmt(expr_fmt[expr_type_], operand_[0]->str(), operand_[1]->str(),
|
||||
operand_[2]->str());
|
||||
break;
|
||||
default:
|
||||
DEBUG_MSG("num_operands_ = %d, orig = %s\n", num_operands_, orig());
|
||||
ASSERT(0);
|
||||
|
@ -217,43 +201,41 @@ void Expr::GenStrFromFormat(Env *env)
|
|||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
namespace
|
||||
{
|
||||
|
||||
RecordField *GetRecordField(const ID *id, Env *env)
|
||||
{
|
||||
Field* field = env->GetField(id);
|
||||
ASSERT(field);
|
||||
if ( field->tof() != RECORD_FIELD &&
|
||||
field->tof() != PADDING_FIELD )
|
||||
throw Exception(id, "not a record field");
|
||||
RecordField *r = static_cast<RecordField *>(field);
|
||||
ASSERT(r);
|
||||
return r;
|
||||
}
|
||||
RecordField* GetRecordField(const ID* id, Env* env)
|
||||
{
|
||||
Field* field = env->GetField(id);
|
||||
ASSERT(field);
|
||||
if ( field->tof() != RECORD_FIELD && field->tof() != PADDING_FIELD )
|
||||
throw Exception(id, "not a record field");
|
||||
RecordField* r = static_cast<RecordField*>(field);
|
||||
ASSERT(r);
|
||||
return r;
|
||||
}
|
||||
|
||||
} // private namespace
|
||||
} // private namespace
|
||||
|
||||
void Expr::GenCaseEval(Output *out_cc, Env *env)
|
||||
void Expr::GenCaseEval(Output* out_cc, Env* env)
|
||||
{
|
||||
ASSERT(expr_type_ == EXPR_CASE);
|
||||
ASSERT(operand_[0]);
|
||||
ASSERT(cases_);
|
||||
|
||||
Type *val_type = DataType(env);
|
||||
ID *val_var = env->AddTempID(val_type);
|
||||
Type* val_type = DataType(env);
|
||||
ID* val_var = env->AddTempID(val_type);
|
||||
|
||||
// DataType(env) can return a null pointer if an enum value is not
|
||||
// defined.
|
||||
if ( ! val_type )
|
||||
throw Exception(this, "undefined case value");
|
||||
|
||||
out_cc->println("%s %s;",
|
||||
val_type->DataTypeStr().c_str(),
|
||||
env->LValue(val_var));
|
||||
out_cc->println("%s %s;", val_type->DataTypeStr().c_str(), env->LValue(val_var));
|
||||
|
||||
// force evaluation of IDs appearing in case stmt
|
||||
operand_[0]->ForceIDEval(out_cc, env);
|
||||
foreach(i, CaseExprList, cases_)
|
||||
operand_[0]->ForceIDEval(out_cc, env);
|
||||
foreach (i, CaseExprList, cases_)
|
||||
(*i)->value()->ForceIDEval(out_cc, env);
|
||||
|
||||
out_cc->println("switch ( %s )", operand_[0]->EvalExpr(out_cc, env));
|
||||
|
@ -262,11 +244,11 @@ void Expr::GenCaseEval(Output *out_cc, Env *env)
|
|||
out_cc->inc_indent();
|
||||
out_cc->println("{");
|
||||
|
||||
CaseExpr *default_case = 0;
|
||||
foreach(i, CaseExprList, cases_)
|
||||
CaseExpr* default_case = 0;
|
||||
foreach (i, CaseExprList, cases_)
|
||||
{
|
||||
CaseExpr *c = *i;
|
||||
ExprList *index = c->index();
|
||||
CaseExpr* c = *i;
|
||||
ExprList* index = c->index();
|
||||
if ( ! index )
|
||||
{
|
||||
if ( default_case )
|
||||
|
@ -277,9 +259,7 @@ void Expr::GenCaseEval(Output *out_cc, Env *env)
|
|||
{
|
||||
GenCaseStr(index, out_cc, env, switch_type);
|
||||
out_cc->inc_indent();
|
||||
out_cc->println("%s = %s;",
|
||||
env->LValue(val_var),
|
||||
c->value()->EvalExpr(out_cc, env));
|
||||
out_cc->println("%s = %s;", env->LValue(val_var), c->value()->EvalExpr(out_cc, env));
|
||||
out_cc->println("break;");
|
||||
out_cc->dec_indent();
|
||||
}
|
||||
|
@ -290,14 +270,13 @@ void Expr::GenCaseEval(Output *out_cc, Env *env)
|
|||
out_cc->inc_indent();
|
||||
if ( default_case )
|
||||
{
|
||||
out_cc->println("%s = %s;",
|
||||
env->LValue(val_var),
|
||||
out_cc->println("%s = %s;", env->LValue(val_var),
|
||||
default_case->value()->EvalExpr(out_cc, env));
|
||||
}
|
||||
else
|
||||
{
|
||||
out_cc->println("throw binpac::ExceptionInvalidCaseIndex(\"%s\", (int64)%s);",
|
||||
Location(), operand_[0]->EvalExpr(out_cc, env));
|
||||
out_cc->println("throw binpac::ExceptionInvalidCaseIndex(\"%s\", (int64)%s);", Location(),
|
||||
operand_[0]->EvalExpr(out_cc, env));
|
||||
}
|
||||
out_cc->println("break;");
|
||||
out_cc->dec_indent();
|
||||
|
@ -333,20 +312,17 @@ void Expr::GenEval(Output* out_cc, Env* env)
|
|||
*/
|
||||
operand_[0]->GenEval(out_cc, env);
|
||||
|
||||
Type *ty0 = operand_[0]->DataType(env);
|
||||
Type* ty0 = operand_[0]->DataType(env);
|
||||
|
||||
if ( ty0 )
|
||||
{
|
||||
str_ = strfmt("%s%s",
|
||||
operand_[0]->EvalExpr(out_cc, env),
|
||||
str_ = strfmt("%s%s", operand_[0]->EvalExpr(out_cc, env),
|
||||
ty0->EvalMember(operand_[1]->id()).c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
string tmp = strfmt("->%s()", operand_[1]->id()->Name());
|
||||
str_ = strfmt("%s%s",
|
||||
operand_[0]->EvalExpr(out_cc, env),
|
||||
tmp.c_str());
|
||||
str_ = strfmt("%s%s", operand_[0]->EvalExpr(out_cc, env), tmp.c_str());
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
@ -358,8 +334,8 @@ void Expr::GenEval(Output* out_cc, Env* env)
|
|||
|
||||
string v0 = operand_[0]->EvalExpr(out_cc, env);
|
||||
string v1 = operand_[1]->EvalExpr(out_cc, env);
|
||||
|
||||
Type *ty0 = operand_[0]->DataType(env);
|
||||
|
||||
Type* ty0 = operand_[0]->DataType(env);
|
||||
if ( ty0 )
|
||||
str_ = ty0->EvalElement(v0, v1);
|
||||
else
|
||||
|
@ -369,18 +345,18 @@ void Expr::GenEval(Output* out_cc, Env* env)
|
|||
|
||||
case EXPR_SIZEOF:
|
||||
{
|
||||
const ID *id = operand_[0]->id();
|
||||
RecordField *rf;
|
||||
Type *ty;
|
||||
const ID* id = operand_[0]->id();
|
||||
RecordField* rf;
|
||||
Type* ty;
|
||||
|
||||
try
|
||||
try
|
||||
{
|
||||
if ( (rf = GetRecordField(id, env)) != 0 )
|
||||
{
|
||||
str_ = strfmt("%s", rf->FieldSize(out_cc, env));
|
||||
}
|
||||
}
|
||||
catch ( ExceptionIDNotFound &e )
|
||||
catch ( ExceptionIDNotFound& e )
|
||||
{
|
||||
if ( (ty = TypeDecl::LookUpType(id)) != 0 )
|
||||
{
|
||||
|
@ -398,8 +374,8 @@ void Expr::GenEval(Output* out_cc, Env* env)
|
|||
|
||||
case EXPR_OFFSETOF:
|
||||
{
|
||||
const ID *id = operand_[0]->id();
|
||||
RecordField *rf = GetRecordField(id, env);
|
||||
const ID* id = operand_[0]->id();
|
||||
RecordField* rf = GetRecordField(id, env);
|
||||
str_ = strfmt("%s", rf->FieldOffset(out_cc, env));
|
||||
}
|
||||
break;
|
||||
|
@ -423,7 +399,7 @@ void Expr::GenEval(Output* out_cc, Env* env)
|
|||
}
|
||||
|
||||
void Expr::ForceIDEval(Output* out_cc, Env* env)
|
||||
{
|
||||
{
|
||||
switch ( expr_type_ )
|
||||
{
|
||||
case EXPR_NUM:
|
||||
|
@ -441,18 +417,18 @@ void Expr::ForceIDEval(Output* out_cc, Env* env)
|
|||
break;
|
||||
|
||||
case EXPR_CALLARGS:
|
||||
{
|
||||
foreach(i, ExprList, args_)
|
||||
(*i)->ForceIDEval(out_cc, env);
|
||||
{
|
||||
foreach (i, ExprList, args_)
|
||||
(*i)->ForceIDEval(out_cc, env);
|
||||
}
|
||||
break;
|
||||
|
||||
case EXPR_CASE:
|
||||
{
|
||||
operand_[0]->ForceIDEval(out_cc, env);
|
||||
foreach(i, CaseExprList, cases_)
|
||||
{
|
||||
operand_[0]->ForceIDEval(out_cc, env);
|
||||
foreach (i, CaseExprList, cases_)
|
||||
(*i)->value()->ForceIDEval(out_cc, env);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
|
@ -464,16 +440,15 @@ void Expr::ForceIDEval(Output* out_cc, Env* env)
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
const char* Expr::EvalExpr(Output* out_cc, Env* env)
|
||||
{
|
||||
GenEval(out_cc, env);
|
||||
return str();
|
||||
}
|
||||
|
||||
Type *Expr::DataType(Env *env) const
|
||||
Type* Expr::DataType(Env* env) const
|
||||
{
|
||||
Type *data_type;
|
||||
Type* data_type;
|
||||
|
||||
switch ( expr_type_ )
|
||||
{
|
||||
|
@ -484,7 +459,7 @@ Type *Expr::DataType(Env *env) const
|
|||
case EXPR_MEMBER:
|
||||
{
|
||||
// Get type of the parent
|
||||
Type *parent_type = operand_[0]->DataType(env);
|
||||
Type* parent_type = operand_[0]->DataType(env);
|
||||
if ( ! parent_type )
|
||||
return 0;
|
||||
data_type = parent_type->MemberDataType(operand_[1]->id());
|
||||
|
@ -494,7 +469,7 @@ Type *Expr::DataType(Env *env) const
|
|||
case EXPR_SUBSCRIPT:
|
||||
{
|
||||
// Get type of the parent
|
||||
Type *parent_type = operand_[0]->DataType(env);
|
||||
Type* parent_type = operand_[0]->DataType(env);
|
||||
data_type = parent_type->ElementDataType();
|
||||
}
|
||||
break;
|
||||
|
@ -505,14 +480,13 @@ Type *Expr::DataType(Env *env) const
|
|||
|
||||
case EXPR_COND:
|
||||
{
|
||||
Type *type1 = operand_[1]->DataType(env);
|
||||
Type *type2 = operand_[2]->DataType(env);
|
||||
Type* type1 = operand_[1]->DataType(env);
|
||||
Type* type2 = operand_[2]->DataType(env);
|
||||
if ( ! Type::CompatibleTypes(type1, type2) )
|
||||
{
|
||||
throw Exception(this,
|
||||
strfmt("type mismatch: %s vs %s",
|
||||
type1->DataTypeStr().c_str(),
|
||||
type2->DataTypeStr().c_str()));
|
||||
throw Exception(this,
|
||||
strfmt("type mismatch: %s vs %s", type1->DataTypeStr().c_str(),
|
||||
type2->DataTypeStr().c_str()));
|
||||
}
|
||||
data_type = type1;
|
||||
}
|
||||
|
@ -526,20 +500,17 @@ Type *Expr::DataType(Env *env) const
|
|||
{
|
||||
if ( cases_ && ! cases_->empty() )
|
||||
{
|
||||
Type *type1 =
|
||||
cases_->front()->value()->DataType(env);
|
||||
Type* type1 = cases_->front()->value()->DataType(env);
|
||||
Type* numeric_with_largest_width = 0;
|
||||
|
||||
foreach(i, CaseExprList, cases_)
|
||||
foreach (i, CaseExprList, cases_)
|
||||
{
|
||||
Type *type2 =
|
||||
(*i)->value()->DataType(env);
|
||||
Type* type2 = (*i)->value()->DataType(env);
|
||||
if ( ! Type::CompatibleTypes(type1, type2) )
|
||||
{
|
||||
throw Exception(this,
|
||||
strfmt("type mismatch: %s vs %s",
|
||||
type1->DataTypeStr().c_str(),
|
||||
type2->DataTypeStr().c_str()));
|
||||
throw Exception(this, strfmt("type mismatch: %s vs %s",
|
||||
type1->DataTypeStr().c_str(),
|
||||
type2->DataTypeStr().c_str()));
|
||||
}
|
||||
if ( type1 == extern_type_nullptr )
|
||||
type1 = type2;
|
||||
|
@ -611,21 +582,19 @@ Type *Expr::DataType(Env *env) const
|
|||
return data_type;
|
||||
}
|
||||
|
||||
string Expr::DataTypeStr(Env *env) const
|
||||
string Expr::DataTypeStr(Env* env) const
|
||||
{
|
||||
Type *type = DataType(env);
|
||||
Type* type = DataType(env);
|
||||
|
||||
if ( ! type )
|
||||
{
|
||||
throw Exception(this,
|
||||
strfmt("cannot find data type for expression `%s'",
|
||||
orig()));
|
||||
throw Exception(this, strfmt("cannot find data type for expression `%s'", orig()));
|
||||
}
|
||||
|
||||
return type->DataTypeStr();
|
||||
}
|
||||
|
||||
string Expr::SetFunc(Output *out, Env *env)
|
||||
string Expr::SetFunc(Output* out, Env* env)
|
||||
{
|
||||
switch ( expr_type_ )
|
||||
{
|
||||
|
@ -635,172 +604,168 @@ string Expr::SetFunc(Output *out, Env *env)
|
|||
{
|
||||
// Evaluate the parent
|
||||
string parent_val(operand_[0]->EvalExpr(out, env));
|
||||
return parent_val
|
||||
+ "->"
|
||||
+ set_function(operand_[1]->id());
|
||||
return parent_val + "->" + set_function(operand_[1]->id());
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw Exception(this,
|
||||
strfmt("cannot generate set function "
|
||||
"for expression `%s'", orig()));
|
||||
throw Exception(this, strfmt("cannot generate set function "
|
||||
"for expression `%s'",
|
||||
orig()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bool Expr::ConstFold(Env* env, int* pn) const
|
||||
{
|
||||
switch ( expr_type_ )
|
||||
switch ( expr_type_ )
|
||||
{
|
||||
case EXPR_NUM:
|
||||
case EXPR_NUM:
|
||||
*pn = num_->Num();
|
||||
return true;
|
||||
case EXPR_ID:
|
||||
return env->GetConstant(id_, pn);
|
||||
default:
|
||||
default:
|
||||
// ### FIXME: folding consts
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: build a generic data dependency extraction process
|
||||
namespace {
|
||||
namespace
|
||||
{
|
||||
|
||||
// Maximum of two minimal header sizes
|
||||
int mhs_max(int h1, int h2)
|
||||
// Maximum of two minimal header sizes
|
||||
int mhs_max(int h1, int h2)
|
||||
{
|
||||
if ( h1 < 0 || h2 < 0 )
|
||||
return -1;
|
||||
else
|
||||
{
|
||||
if ( h1 < 0 || h2 < 0 )
|
||||
return -1;
|
||||
else
|
||||
// return max(h1, h2);
|
||||
return h1 > h2 ? h1 : h2;
|
||||
}
|
||||
}
|
||||
|
||||
// MHS required to evaluate the field
|
||||
int mhs_letfield(Env* env, LetField* field)
|
||||
{
|
||||
return field->expr()->MinimalHeaderSize(env);
|
||||
}
|
||||
|
||||
int mhs_recordfield(Env* env, RecordField* field)
|
||||
{
|
||||
int offset = field->static_offset();
|
||||
if ( offset < 0 ) // offset cannot be statically determined
|
||||
return -1;
|
||||
int size = field->StaticSize(env, offset);
|
||||
if ( size < 0 ) // size cannot be statically determined
|
||||
return -1;
|
||||
return offset + size;
|
||||
}
|
||||
|
||||
int mhs_casefield(Env* env, CaseField* field)
|
||||
{
|
||||
// TODO: deal with the index
|
||||
int size = field->StaticSize(env);
|
||||
if ( size < 0 ) // size cannot be statically determined
|
||||
return -1;
|
||||
return size;
|
||||
}
|
||||
|
||||
int mhs_field(Env* env, Field* field)
|
||||
{
|
||||
int mhs = -1;
|
||||
switch ( field->tof() )
|
||||
{
|
||||
case LET_FIELD:
|
||||
{
|
||||
// return max(h1, h2);
|
||||
return h1 > h2 ? h1 : h2;
|
||||
LetField* f = static_cast<LetField*>(field);
|
||||
ASSERT(f);
|
||||
mhs = mhs_letfield(env, f);
|
||||
}
|
||||
}
|
||||
|
||||
// MHS required to evaluate the field
|
||||
int mhs_letfield(Env* env, LetField* field)
|
||||
{
|
||||
return field->expr()->MinimalHeaderSize(env);
|
||||
}
|
||||
break;
|
||||
|
||||
int mhs_recordfield(Env* env, RecordField* field)
|
||||
{
|
||||
int offset = field->static_offset();
|
||||
if ( offset < 0 ) // offset cannot be statically determined
|
||||
return -1;
|
||||
int size = field->StaticSize(env, offset);
|
||||
if ( size < 0 ) // size cannot be statically determined
|
||||
return -1;
|
||||
return offset + size;
|
||||
}
|
||||
case CONTEXT_FIELD:
|
||||
case FLOW_FIELD:
|
||||
ASSERT(0);
|
||||
break;
|
||||
|
||||
int mhs_casefield(Env* env, CaseField* field)
|
||||
{
|
||||
// TODO: deal with the index
|
||||
int size = field->StaticSize(env);
|
||||
if ( size < 0 ) // size cannot be statically determined
|
||||
return -1;
|
||||
return size;
|
||||
}
|
||||
case PARAM_FIELD:
|
||||
mhs = 0;
|
||||
break;
|
||||
|
||||
int mhs_field(Env* env, Field* field)
|
||||
{
|
||||
int mhs = -1;
|
||||
switch ( field->tof() )
|
||||
case RECORD_FIELD:
|
||||
case PADDING_FIELD:
|
||||
{
|
||||
case LET_FIELD:
|
||||
{
|
||||
LetField *f =
|
||||
static_cast<LetField *>(field);
|
||||
ASSERT(f);
|
||||
mhs = mhs_letfield(env, f);
|
||||
}
|
||||
break;
|
||||
|
||||
case CONTEXT_FIELD:
|
||||
case FLOW_FIELD:
|
||||
ASSERT(0);
|
||||
break;
|
||||
|
||||
case PARAM_FIELD:
|
||||
mhs = 0;
|
||||
break;
|
||||
|
||||
case RECORD_FIELD:
|
||||
case PADDING_FIELD:
|
||||
{
|
||||
RecordField *f =
|
||||
static_cast<RecordField *>(field);
|
||||
ASSERT(f);
|
||||
mhs = mhs_recordfield(env, f);
|
||||
}
|
||||
break;
|
||||
|
||||
case CASE_FIELD:
|
||||
{
|
||||
CaseField *f =
|
||||
static_cast<CaseField *>(field);
|
||||
ASSERT(f);
|
||||
mhs = mhs_casefield(env, f);
|
||||
}
|
||||
break;
|
||||
|
||||
case PARSE_VAR_FIELD:
|
||||
case PRIV_VAR_FIELD:
|
||||
case PUB_VAR_FIELD:
|
||||
case TEMP_VAR_FIELD:
|
||||
mhs = 0;
|
||||
break;
|
||||
|
||||
case WITHINPUT_FIELD:
|
||||
{
|
||||
// ### TODO: fix this
|
||||
mhs = -1;
|
||||
}
|
||||
break;
|
||||
RecordField* f = static_cast<RecordField*>(field);
|
||||
ASSERT(f);
|
||||
mhs = mhs_recordfield(env, f);
|
||||
}
|
||||
return mhs;
|
||||
}
|
||||
break;
|
||||
|
||||
int mhs_id(Env *env, const ID *id)
|
||||
{
|
||||
int mhs = -1;
|
||||
switch ( env->GetIDType(id) )
|
||||
case CASE_FIELD:
|
||||
{
|
||||
case CONST:
|
||||
case GLOBAL_VAR:
|
||||
case TEMP_VAR:
|
||||
case STATE_VAR:
|
||||
case FUNC_ID:
|
||||
case FUNC_PARAM:
|
||||
mhs = 0;
|
||||
break;
|
||||
case MEMBER_VAR:
|
||||
case PRIV_MEMBER_VAR:
|
||||
{
|
||||
Field* field = env->GetField(id);
|
||||
if ( ! field )
|
||||
throw ExceptionIDNotField(id);
|
||||
mhs = mhs_field(env, field);
|
||||
}
|
||||
break;
|
||||
case UNION_VAR:
|
||||
// TODO: deal with UNION_VAR
|
||||
mhs = -1;
|
||||
break;
|
||||
case MACRO:
|
||||
{
|
||||
Expr *e = env->GetMacro(id);
|
||||
mhs = e->MinimalHeaderSize(env);
|
||||
}
|
||||
break;
|
||||
CaseField* f = static_cast<CaseField*>(field);
|
||||
ASSERT(f);
|
||||
mhs = mhs_casefield(env, f);
|
||||
}
|
||||
return mhs;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
int Expr::MinimalHeaderSize(Env *env)
|
||||
case PARSE_VAR_FIELD:
|
||||
case PRIV_VAR_FIELD:
|
||||
case PUB_VAR_FIELD:
|
||||
case TEMP_VAR_FIELD:
|
||||
mhs = 0;
|
||||
break;
|
||||
|
||||
case WITHINPUT_FIELD:
|
||||
{
|
||||
// ### TODO: fix this
|
||||
mhs = -1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return mhs;
|
||||
}
|
||||
|
||||
int mhs_id(Env* env, const ID* id)
|
||||
{
|
||||
int mhs = -1;
|
||||
switch ( env->GetIDType(id) )
|
||||
{
|
||||
case CONST:
|
||||
case GLOBAL_VAR:
|
||||
case TEMP_VAR:
|
||||
case STATE_VAR:
|
||||
case FUNC_ID:
|
||||
case FUNC_PARAM:
|
||||
mhs = 0;
|
||||
break;
|
||||
case MEMBER_VAR:
|
||||
case PRIV_MEMBER_VAR:
|
||||
{
|
||||
Field* field = env->GetField(id);
|
||||
if ( ! field )
|
||||
throw ExceptionIDNotField(id);
|
||||
mhs = mhs_field(env, field);
|
||||
}
|
||||
break;
|
||||
case UNION_VAR:
|
||||
// TODO: deal with UNION_VAR
|
||||
mhs = -1;
|
||||
break;
|
||||
case MACRO:
|
||||
{
|
||||
Expr* e = env->GetMacro(id);
|
||||
mhs = e->MinimalHeaderSize(env);
|
||||
}
|
||||
break;
|
||||
}
|
||||
return mhs;
|
||||
}
|
||||
}
|
||||
|
||||
int Expr::MinimalHeaderSize(Env* env)
|
||||
{
|
||||
int mhs;
|
||||
|
||||
|
@ -826,11 +791,10 @@ int Expr::MinimalHeaderSize(Env *env)
|
|||
case EXPR_SUBSCRIPT:
|
||||
{
|
||||
int index;
|
||||
Type *array_type = operand_[0]->DataType(env);
|
||||
Type *elem_type = array_type->ElementDataType();
|
||||
Type* array_type = operand_[0]->DataType(env);
|
||||
Type* elem_type = array_type->ElementDataType();
|
||||
int elem_size = elem_type->StaticSize(env);
|
||||
if ( elem_size >= 0 &&
|
||||
operand_[1]->ConstFold(env, &index) )
|
||||
if ( elem_size >= 0 && operand_[1]->ConstFold(env, &index) )
|
||||
{
|
||||
mhs = elem_size * index;
|
||||
}
|
||||
|
@ -845,8 +809,8 @@ int Expr::MinimalHeaderSize(Env *env)
|
|||
{
|
||||
const ID* id = operand_[0]->id();
|
||||
ASSERT(id);
|
||||
RecordField *rf;
|
||||
Type *ty;
|
||||
RecordField* rf;
|
||||
Type* ty;
|
||||
|
||||
if ( (rf = GetRecordField(id, env)) != 0 )
|
||||
{
|
||||
|
@ -870,8 +834,8 @@ int Expr::MinimalHeaderSize(Env *env)
|
|||
{
|
||||
const ID* id = operand_[0]->id();
|
||||
ASSERT(id);
|
||||
RecordField *field = GetRecordField(id, env);
|
||||
|
||||
RecordField* field = GetRecordField(id, env);
|
||||
|
||||
mhs = field->static_offset();
|
||||
if ( mhs < 0 )
|
||||
{
|
||||
|
@ -885,21 +849,21 @@ int Expr::MinimalHeaderSize(Env *env)
|
|||
break;
|
||||
|
||||
case EXPR_CALLARGS:
|
||||
{
|
||||
mhs = 0;
|
||||
{
|
||||
mhs = 0;
|
||||
if ( args_ )
|
||||
for ( unsigned int i = 0; i < args_->size(); ++i )
|
||||
for ( unsigned int i = 0; i < args_->size(); ++i )
|
||||
mhs = mhs_max(mhs, (*args_)[i]->MinimalHeaderSize(env));
|
||||
}
|
||||
break;
|
||||
break;
|
||||
case EXPR_CASE:
|
||||
{
|
||||
mhs = operand_[0]->MinimalHeaderSize(env);
|
||||
{
|
||||
mhs = operand_[0]->MinimalHeaderSize(env);
|
||||
for ( unsigned int i = 0; i < cases_->size(); ++i )
|
||||
{
|
||||
CaseExpr * ce = (*cases_)[i];
|
||||
{
|
||||
CaseExpr* ce = (*cases_)[i];
|
||||
if ( ce->index() )
|
||||
for ( unsigned int j = 0; j < ce->index()->size(); ++j )
|
||||
for ( unsigned int j = 0; j < ce->index()->size(); ++j )
|
||||
mhs = mhs_max(mhs, (*ce->index())[j]->MinimalHeaderSize(env));
|
||||
mhs = mhs_max(mhs, ce->value()->MinimalHeaderSize(env));
|
||||
}
|
||||
|
@ -917,7 +881,7 @@ int Expr::MinimalHeaderSize(Env *env)
|
|||
return mhs;
|
||||
}
|
||||
|
||||
bool Expr::HasReference(const ID *id) const
|
||||
bool Expr::HasReference(const ID* id) const
|
||||
{
|
||||
switch ( expr_type_ )
|
||||
{
|
||||
|
@ -929,7 +893,7 @@ bool Expr::HasReference(const ID *id) const
|
|||
|
||||
case EXPR_CALLARGS:
|
||||
{
|
||||
foreach(i, ExprList, args_)
|
||||
foreach (i, ExprList, args_)
|
||||
if ( (*i)->HasReference(id) )
|
||||
return true;
|
||||
}
|
||||
|
@ -937,7 +901,7 @@ bool Expr::HasReference(const ID *id) const
|
|||
|
||||
case EXPR_CASE:
|
||||
{
|
||||
foreach(i, CaseExprList, cases_)
|
||||
foreach (i, CaseExprList, cases_)
|
||||
if ( (*i)->HasReference(id) )
|
||||
return true;
|
||||
}
|
||||
|
@ -947,8 +911,7 @@ bool Expr::HasReference(const ID *id) const
|
|||
// Evaluate every operand by default
|
||||
for ( int i = 0; i < 3; ++i )
|
||||
{
|
||||
if ( operand_[i] &&
|
||||
operand_[i]->HasReference(id) )
|
||||
if ( operand_[i] && operand_[i]->HasReference(id) )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
@ -957,7 +920,7 @@ bool Expr::HasReference(const ID *id) const
|
|||
}
|
||||
}
|
||||
|
||||
bool Expr::DoTraverse(DataDepVisitor *visitor)
|
||||
bool Expr::DoTraverse(DataDepVisitor* visitor)
|
||||
{
|
||||
switch ( expr_type_ )
|
||||
{
|
||||
|
@ -977,7 +940,7 @@ bool Expr::DoTraverse(DataDepVisitor *visitor)
|
|||
|
||||
case EXPR_CALLARGS:
|
||||
{
|
||||
foreach(i, ExprList, args_)
|
||||
foreach (i, ExprList, args_)
|
||||
if ( ! (*i)->Traverse(visitor) )
|
||||
return false;
|
||||
}
|
||||
|
@ -985,7 +948,7 @@ bool Expr::DoTraverse(DataDepVisitor *visitor)
|
|||
|
||||
case EXPR_CASE:
|
||||
{
|
||||
foreach(i, CaseExprList, cases_)
|
||||
foreach (i, CaseExprList, cases_)
|
||||
if ( ! (*i)->Traverse(visitor) )
|
||||
return false;
|
||||
}
|
||||
|
@ -995,8 +958,7 @@ bool Expr::DoTraverse(DataDepVisitor *visitor)
|
|||
// Evaluate every operand by default
|
||||
for ( int i = 0; i < 3; ++i )
|
||||
{
|
||||
if ( operand_[i] &&
|
||||
! operand_[i]->Traverse(visitor) )
|
||||
if ( operand_[i] && ! operand_[i]->Traverse(visitor) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
@ -1025,7 +987,7 @@ bool Expr::RequiresAnalyzerContext() const
|
|||
|
||||
case EXPR_CALLARGS:
|
||||
{
|
||||
foreach(i, ExprList, args_)
|
||||
foreach (i, ExprList, args_)
|
||||
if ( (*i)->RequiresAnalyzerContext() )
|
||||
return true;
|
||||
}
|
||||
|
@ -1033,7 +995,7 @@ bool Expr::RequiresAnalyzerContext() const
|
|||
|
||||
case EXPR_CASE:
|
||||
{
|
||||
foreach(i, CaseExprList, cases_)
|
||||
foreach (i, CaseExprList, cases_)
|
||||
if ( (*i)->RequiresAnalyzerContext() )
|
||||
return true;
|
||||
}
|
||||
|
@ -1042,8 +1004,7 @@ bool Expr::RequiresAnalyzerContext() const
|
|||
default:
|
||||
// Evaluate every operand by default
|
||||
for ( int i = 0; i < 3; ++i )
|
||||
if ( operand_[i] &&
|
||||
operand_[i]->RequiresAnalyzerContext() )
|
||||
if ( operand_[i] && operand_[i]->RequiresAnalyzerContext() )
|
||||
{
|
||||
DEBUG_MSG("'%s' requires analyzer context\n", operand_[i]->orig());
|
||||
return true;
|
||||
|
@ -1052,9 +1013,8 @@ bool Expr::RequiresAnalyzerContext() const
|
|||
}
|
||||
}
|
||||
|
||||
CaseExpr::CaseExpr(ExprList *index, Expr *value)
|
||||
: DataDepElement(DataDepElement::CASEEXPR),
|
||||
index_(index), value_(value)
|
||||
CaseExpr::CaseExpr(ExprList* index, Expr* value)
|
||||
: DataDepElement(DataDepElement::CASEEXPR), index_(index), value_(value)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -1064,15 +1024,15 @@ CaseExpr::~CaseExpr()
|
|||
delete value_;
|
||||
}
|
||||
|
||||
bool CaseExpr::DoTraverse(DataDepVisitor *visitor)
|
||||
bool CaseExpr::DoTraverse(DataDepVisitor* visitor)
|
||||
{
|
||||
foreach(i, ExprList, index_)
|
||||
foreach (i, ExprList, index_)
|
||||
if ( ! (*i)->Traverse(visitor) )
|
||||
return false;
|
||||
return value_->Traverse(visitor);
|
||||
}
|
||||
|
||||
bool CaseExpr::HasReference(const ID *id) const
|
||||
bool CaseExpr::HasReference(const ID* id) const
|
||||
{
|
||||
return value_->HasReference(id);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue