rdaly525 / coreir

BSD 3-Clause "New" or "Revised" License
100 stars 24 forks source link

Create mini expression language for symbolic type generation #494

Open rdaly525 opened 6 years ago

rdaly525 commented 6 years ago
Value = Const(<>)
      | Var(string name)
      | Arg(string name)
      | Op(Value*)
      attribute(ValueType)

Stmt = Assign(string varname, Value)
     | Return(Value)

TypeGen(Params,Stmt*)

Op = Array<Type>(number n, Value<Type>)
   | Add<int>(Value<int> l, Value<int> r)
   | Select(Value<bool>, Value truecond, Value falsecond)
   | Record<Type>(string f, Value<Type>, Value<bool>, ...) 
   | ...

//Last statement needs to be a return.
//Vars have to be decalred before used
rdaly525 commented 6 years ago

@leonardt, want to do a mini design review of this with me some time this week? The goal is to create a very simple Type language which can be stored/executed symbolically (and therefore serializable). This could alleviate the need to run python code for typegens.

rdaly525 commented 6 years ago

http://www.drdobbs.com/cpp/the-spirit-parser-library-inline-parsing/184401692

If I wanted to actually have a parseable language, this seems like a great C++ tool to do it. This way I could simply pass around the typegen as a string representing the code.

rdaly525 commented 6 years ago

Here is a small example of how I would imagine creating this in coreir.

Context* c = newContext();
  Namespace* g = c->getGlobal();

  Params params({{"width",c->Int()}});

  SymTypeGen* tg = SymTypeGen::make(g,"unary",params);
  tg->addStmt(Assign::make(tg,"w",Arg::make(tg,"width")));
  Value* inArr = Ops::Array::make(tg,Var::make(tg,"w"),Const::make(c,c->BitIn()));
  Value* outArr = Ops::Flip::make(tg,Var::make(tg,"inArr"));
  tg->addStmt(Assign::make(tg,"inArr",inArr);
  tg->addStmt(Assign::make(tg,"outArr",outArr);
  tg->addStmt(Assign::make(tg,"finalType",Record::make(tg,
    "in",Var::make(tg,"inArr"), Const::make(c,true),
    "out",Var::make(tg,"outArr"), Const::make(c,true)
  )));
  tg->addStmt(Return::make(tg,Var(tg,"finalType")));
rdaly525 commented 6 years ago

Also here is an example of some potential future syntax for the language itself.

Type function unary(int width) {
  int w = width;
  Type inArr = Array(w,BitIn);
  Type outArr = Flip(inArr);
  Type finalType = Record(
    in : (inArr,true)
    out : (outArr,true)
  );
  return finalType;
}

Type function unary_with_en(int width, bool has_en) {
  int w = width;
  Type inArr = Array(w,BitIn);
  Type outArr = Flip(inArr);
  Type final = Record(
    in : (inArr,true),
    out : (outArr,true),
    en : (BitIn,has_en),
  );
  return final;
}