Skip to content

Commit

Permalink
first cut at calling the constructor for main per #426 #427
Browse files Browse the repository at this point in the history
  • Loading branch information
ivoysey committed Feb 25, 2022
1 parent 260e466 commit f97fd39
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Obsidian_Runtime/src/main/yul_templates/object.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ object "{{contractName}}" {

{{! todo: is 4 a magic number or should it be generated based on the object in question? check the ABI }}
if iszero(lt(calldatasize(), 4)) {
{{buildMain}}
{{! TODO 224 is a magic number offset to shift to follow the spec above; check that it's right }}
let this := allocate_memory({{mainSize}})
let selector := shr(224, calldataload(0))
{{dispatchTable}}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ object CodeGenYul extends CodeGenerator {
data = Seq(),
mainContractTransactions = translateContract(main_contract),
mainContractSize = sizeOfContractST(main_contract.name, checkedTable),
mainConstructorTypeNames = defaultConstructorSignature(main_contract,checkedTable,"").map(tn => tn.typ.toString),
otherTransactions = other_contracts.flatMap(translateContract),
tracers = (main_contract +: other_contracts).flatMap(c => writeTracers(checkedTable, c.name)).distinctBy(fd => fd.name)
)
Expand Down
12 changes: 10 additions & 2 deletions src/main/scala/edu/cmu/cs/obsidian/codegen/yulAST.scala
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ case class YulObject(contractName: String,
data: Seq[Data],
mainContractTransactions: Seq[YulStatement],
mainContractSize: Int,
mainConstructorTypeNames : Seq[String],
otherTransactions: Seq[YulStatement],
tracers: Seq[FunctionDefinition]) extends YulAST {
def yulString(): String = {
Expand Down Expand Up @@ -293,8 +294,6 @@ case class YulObject(contractName: String,

def defaultReturn(): Expression = apply("return", intlit(0), datasize)

val mainSize: Int = mainContractSize

// together, these keep track of and create temporary variables for returns in the dispatch table
var deRetCnt = 0

Expand Down Expand Up @@ -365,6 +364,15 @@ case class YulObject(contractName: String,
// the decoders and encoders we actually need (i.e. if f only has `this` as a param, the decoder we
// emit never gets called and gets optimized away. that's fine enough but why not make it better?)

def buildMain(): YulStatement = Block(
Seq(
//let this := allocate_memory({{mainSize}})
decl_1exp(Identifier("this"), apply("allocate_memory", intlit(mainContractSize))),
// todo call the constructor for the main contract on this and ... what?
ExpressionStatement(apply(flattenedName(contractName,contractName,Some(mainConstructorTypeNames)), Identifier("this")))
)
)

// the dispatch table gets one entry for each transaction in the main contract. the transactions
// elaborations are added below, and those have a `this` argument added, which is supplied in the
// dispatch table from an allocated instance of the main contract at the top of memory. but the
Expand Down

0 comments on commit f97fd39

Please sign in to comment.