Skip to content

Commit

Permalink
Fix access to Yul built-ins from inside Yul function definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
ggiraldez committed Feb 4, 2025
1 parent 7fff141 commit e8f42aa
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 77 deletions.
42 changes: 14 additions & 28 deletions crates/solidity/inputs/language/bindings/rules.msgb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ inherit .star_extension
; special symbol (which is automatically imported below)
attr (export) pop_symbol = "@@built-ins@@"

; NOTE: since we're no longer ingesting built-ins from an external Solidity
; file and instead synthetically building the corresponding stack graph
; directly from the language definition, this branch should never execute.
; Regardless, it can help understand how to link to a fixed, built-in,
; scope.

} else {
; This is a user file, so we want to export under the file's path symbol
attr (export) pop_symbol = FILE_PATH
Expand Down Expand Up @@ -103,34 +109,6 @@ inherit .star_extension
edge @unit_member.lexical_scope -> @source_unit.lexical_scope
}

;; Special case for built-ins: we want to export all symbols in the contract:
;; functions, types and state variables. All built-in symbols are defined in an
;; internal contract named '%SolidityBuiltIns%' (renamed from
;; '$SolidityBuiltIns$') so we need to export all its members and type members
;; directly as a source unit definition.
;; __SLANG_SOLIDITY_BUILT_INS_CONTRACT_NAME__ keep in sync with language definition.
@source_unit [SourceUnit [SourceUnitMembers
[SourceUnitMember @contract [ContractDefinition name: ["%SolidityBuiltIns%"]]]
]] {
if (is-system-file FILE_PATH) {
edge @source_unit.defs -> @contract.instance
}
}

;; Similarly for Yul built-ins, we export all the symbols in the `%YulBuiltIns%`
;; contract guarded by a `@yul` symbol.
;; __SLANG_YUL_BUILT_INS_CONTRACT_NAME__ keep in sync with language definition file.
@source_unit [SourceUnit [SourceUnitMembers
[SourceUnitMember @contract [ContractDefinition name: ["%YulBuiltIns%"]]]
]] {
if (is-system-file FILE_PATH) {
node yul
attr (yul) pop_symbol = "@yul"
edge @source_unit.defs -> yul
edge yul -> @contract.instance
}
}

@source_unit [SourceUnit [SourceUnitMembers [SourceUnitMember @using [UsingDirective]]]] {
; TODO: this is the hook for top-level extensions, but this should connect to
; an extensions scope that gets pushed to the scope stack, as in the case of
Expand Down Expand Up @@ -2014,10 +1992,18 @@ inherit .star_extension
@stmt [Statement [AssemblyStatement @body body: [YulBlock]]] {
edge @body.lexical_scope -> @stmt.lexical_scope

;; This provides a path to Yul built-ins
;; __SLANG_SOLIDITY_YUL_BUILT_INS_GUARD__ keep in sync with built-ins code generation
node yul
attr (yul) push_symbol = "@yul"
edge @body.lexical_scope -> yul
edge yul -> @stmt.lexical_scope

;; Provide alternative path to Yul built-ins from the context of a Yul function
node in_yul_function_pop
attr (in_yul_function_pop) pop_symbol = "@in_yul_function"
edge @body.lexical_scope -> in_yul_function_pop
edge in_yul_function_pop -> yul
}


Expand Down
2 changes: 2 additions & 0 deletions crates/solidity/inputs/language/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ fn render_contexts_for_version(
for context in built_in_contexts {
writeln!(buffer, " // {context}", context = context.name)?;
writeln!(buffer, " {{")?;
// __SLANG_YUL_BUILT_INS_CONTEXT_NAME__ keep in sync with language definition
if context.name == "$YulBuiltIns$" {
// __SLANG_SOLIDITY_YUL_BUILT_INS_GUARD__ keep in sync with binding rules
writeln!(
buffer,
" let mut scope = scope.new_context(builder, \"@yul\");"
Expand Down
3 changes: 1 addition & 2 deletions crates/solidity/inputs/language/src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6006,7 +6006,6 @@ codegen_language_macros::compile!(Language(
],
built_ins = [
BuiltInContext(
// __SLANG_SOLIDITY_BUILT_INS_CONTRACT_NAME__ keep in sync with rules file.
name = "$SolidityBuiltIns$",
definitions = [
BuiltInFunction(
Expand Down Expand Up @@ -6432,7 +6431,7 @@ codegen_language_macros::compile!(Language(
]
),
BuiltInContext(
// __SLANG_YUL_BUILT_INS_CONTRACT_NAME__ keep in sync with binding rules file.
// __SLANG_YUL_BUILT_INS_CONTEXT_NAME__ keep in sync with built-ins code generation
name = "$YulBuiltIns$",
definitions = [
BuiltInType(
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ References and definitions:
│ ╰───── name: 5
7 │ mstore(emptyPtr, CONTRACT_CONST)
│ ───┬── ────┬─── ───────┬──────
│ ╰───────────────────────────── unresolved
│ ╰───────────────────────────── ref: built-in
│ │ │
│ ╰───────────────────── ref: 5
│ │
│ ╰──────── ref: 2
8 │ mstore(emptyPtr, TOP_LEVEL_CONST)
│ ───┬── ────┬─── ───────┬───────
│ ╰────────────────────────────── unresolved
│ ╰────────────────────────────── ref: built-in
│ │ │
│ ╰────────────────────── ref: 5
│ │
Expand All @@ -60,14 +60,14 @@ References and definitions:
│ ╰───── name: 10
20 │ mstore(emptyPtr, LIB_CONST)
│ ───┬── ────┬─── ────┬────
│ ╰──────────────────────── unresolved
│ ╰──────────────────────── ref: built-in
│ │ │
│ ╰──────────────── ref: 10
│ │
│ ╰────── ref: 7
21 │ mstore(emptyPtr, TOP_LEVEL_CONST)
│ ───┬── ────┬─── ───────┬───────
│ ╰────────────────────────────── unresolved
│ ╰────────────────────────────── ref: built-in
│ │ │
│ ╰────────────────────── ref: 10
│ │
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ References and definitions:
│ ╰───── name: 5
7 │ mstore(emptyPtr, CONTRACT_CONST)
│ ───┬── ────┬─── ───────┬──────
│ ╰───────────────────────────── unresolved
│ ╰───────────────────────────── ref: built-in
│ │ │
│ ╰───────────────────── ref: 5
│ │
│ ╰──────── ref: 2
8 │ mstore(emptyPtr, TOP_LEVEL_CONST)
│ ───┬── ────┬─── ───────┬───────
│ ╰────────────────────────────── unresolved
│ ╰────────────────────────────── ref: built-in
│ │ │
│ ╰────────────────────── ref: 5
│ │
Expand All @@ -60,14 +60,14 @@ References and definitions:
│ ╰───── name: 10
20 │ mstore(emptyPtr, LIB_CONST)
│ ───┬── ────┬─── ────┬────
│ ╰──────────────────────── unresolved
│ ╰──────────────────────── ref: built-in
│ │ │
│ ╰──────────────── ref: 10
│ │
│ ╰────── ref: 7
21 │ mstore(emptyPtr, TOP_LEVEL_CONST)
│ ───┬── ────┬─── ───────┬───────
│ ╰────────────────────────────── unresolved
│ ╰────────────────────────────── ref: built-in
│ │ │
│ ╰────────────────────── ref: 10
│ │
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ References and definitions:
│ ╰───── name: 5
7 │ mstore(emptyPtr, CONTRACT_CONST)
│ ───┬── ────┬─── ───────┬──────
│ ╰───────────────────────────── unresolved
│ ╰───────────────────────────── ref: built-in
│ │ │
│ ╰───────────────────── ref: 5
│ │
│ ╰──────── ref: 2
8 │ mstore(emptyPtr, TOP_LEVEL_CONST)
│ ───┬── ────┬─── ───────┬───────
│ ╰────────────────────────────── unresolved
│ ╰────────────────────────────── ref: built-in
│ │ │
│ ╰────────────────────── ref: 5
│ │
Expand All @@ -60,14 +60,14 @@ References and definitions:
│ ╰───── name: 10
20 │ mstore(emptyPtr, LIB_CONST)
│ ───┬── ────┬─── ────┬────
│ ╰──────────────────────── unresolved
│ ╰──────────────────────── ref: built-in
│ │ │
│ ╰──────────────── ref: 10
│ │
│ ╰────── ref: 7
21 │ mstore(emptyPtr, TOP_LEVEL_CONST)
│ ───┬── ────┬─── ───────┬───────
│ ╰────────────────────────────── unresolved
│ ╰────────────────────────────── ref: built-in
│ │ │
│ ╰────────────────────── ref: 10
│ │
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ References and definitions:
│ ╰───── name: 5
7 │ mstore(emptyPtr, CONTRACT_CONST)
│ ───┬── ────┬─── ───────┬──────
│ ╰───────────────────────────── unresolved
│ ╰───────────────────────────── ref: built-in
│ │ │
│ ╰───────────────────── ref: 5
│ │
│ ╰──────── ref: 2
8 │ mstore(emptyPtr, TOP_LEVEL_CONST)
│ ───┬── ────┬─── ───────┬───────
│ ╰────────────────────────────── unresolved
│ ╰────────────────────────────── ref: built-in
│ │ │
│ ╰────────────────────── ref: 5
│ │
Expand All @@ -52,14 +52,14 @@ References and definitions:
│ ╰───── name: 10
20 │ mstore(emptyPtr, LIB_CONST)
│ ───┬── ────┬─── ────┬────
│ ╰──────────────────────── unresolved
│ ╰──────────────────────── ref: built-in
│ │ │
│ ╰──────────────── ref: 10
│ │
│ ╰────── ref: 7
21 │ mstore(emptyPtr, TOP_LEVEL_CONST)
│ ───┬── ────┬─── ───────┬───────
│ ╰────────────────────────────── unresolved
│ ╰────────────────────────────── ref: built-in
│ │ │
│ ╰────────────────────── ref: 10
│ │
Expand Down
Loading

0 comments on commit e8f42aa

Please sign in to comment.