Skip to content

Commit 4f9bb72

Browse files
committed
Fix native source locations in optimized IR AST
1 parent 99ae8d1 commit 4f9bb72

File tree

4 files changed

+147
-135
lines changed

4 files changed

+147
-135
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Bugfixes:
1717
* SMTChecker: Fix formatting of unary minus expressions in invariants.
1818
* SMTChecker: Fix internal compiler error when reporting proved targets for BMC engine.
1919
* TypeChecker: Fix segfault when assigning nested tuple to tuple.
20+
* Yul AST: Fix ``nativeSrc`` attributes in optimized IR AST referring to locations in unoptimized IR.
2021
* Yul Optimizer: Name simplification could lead to forbidden identifiers with a leading and/or trailing dot, e.g., ``x._`` would get simplified into ``x.``.
2122

2223

libsolidity/interface/CompilerStack.cpp

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,25 +1464,36 @@ void CompilerStack::generateIR(ContractDefinition const& _contract)
14641464
);
14651465
}
14661466

1467-
yul::YulStack stack(
1468-
m_evmVersion,
1469-
m_eofVersion,
1470-
yul::YulStack::Language::StrictAssembly,
1471-
m_optimiserSettings,
1472-
m_debugInfoSelection
1473-
);
1474-
bool yulAnalysisSuccessful = stack.parseAndAnalyze("", compiledContract.yulIR);
1475-
solAssert(
1476-
yulAnalysisSuccessful,
1477-
compiledContract.yulIR + "\n\n"
1478-
"Invalid IR generated:\n" +
1479-
langutil::SourceReferenceFormatter::formatErrorInformation(stack.errors(), stack) + "\n"
1480-
);
1467+
auto const reparseYul = [&](std::string const& _irSource) {
1468+
YulStack stack(
1469+
m_evmVersion,
1470+
m_eofVersion,
1471+
YulStack::Language::StrictAssembly,
1472+
m_optimiserSettings,
1473+
m_debugInfoSelection
1474+
);
1475+
bool yulAnalysisSuccessful = stack.parseAndAnalyze("", _irSource);
1476+
solAssert(
1477+
yulAnalysisSuccessful,
1478+
_irSource + "\n\n"
1479+
"Invalid IR generated:\n" +
1480+
langutil::SourceReferenceFormatter::formatErrorInformation(stack.errors(), stack) + "\n"
1481+
);
1482+
return stack;
1483+
};
14811484

1482-
compiledContract.yulIRAst = stack.astJson();
1483-
stack.optimize();
1484-
compiledContract.yulIROptimized = stack.print(this);
1485-
compiledContract.yulIROptimizedAst = stack.astJson();
1485+
{
1486+
YulStack stack = reparseYul(compiledContract.yulIR);
1487+
compiledContract.yulIRAst = stack.astJson();
1488+
stack.optimize();
1489+
compiledContract.yulIROptimized = stack.print(this);
1490+
}
1491+
{
1492+
// Optimizer does not maintain correct native source locations in the AST.
1493+
// We can work around it by regenerating the AST from scratch from optimized IR.
1494+
YulStack stack = reparseYul(compiledContract.yulIROptimized);
1495+
compiledContract.yulIROptimizedAst = stack.astJson();
1496+
}
14861497
}
14871498

14881499
void CompilerStack::generateEVMFromIR(ContractDefinition const& _contract)

test/cmdlineTests/ast_ir/output

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -637,24 +637,24 @@ Optimized IR AST:
637637
{
638638
"code": {
639639
"block": {
640-
"nativeSrc": "59:790:0",
640+
"nativeSrc": "58:315:0",
641641
"nodeType": "YulBlock",
642642
"src": "-1:-1:0",
643643
"statements": [
644644
{
645-
"nativeSrc": "59:790:0",
645+
"nativeSrc": "68:299:0",
646646
"nodeType": "YulBlock",
647647
"src": "-1:-1:0",
648648
"statements": [
649649
{
650-
"nativeSrc": "122:16:0",
650+
"nativeSrc": "128:27:0",
651651
"nodeType": "YulVariableDeclaration",
652652
"src": "60:13:0",
653653
"value": {
654654
"arguments": [
655655
{
656656
"kind": "number",
657-
"nativeSrc": "134:3:0",
657+
"nativeSrc": "150:4:0",
658658
"nodeType": "YulLiteral",
659659
"src": "60:13:0",
660660
"type": "",
@@ -663,18 +663,18 @@ Optimized IR AST:
663663
],
664664
"functionName": {
665665
"name": "memoryguard",
666-
"nativeSrc": "122:11:0",
666+
"nativeSrc": "138:11:0",
667667
"nodeType": "YulIdentifier",
668668
"src": "60:13:0"
669669
},
670-
"nativeSrc": "122:16:0",
670+
"nativeSrc": "138:17:0",
671671
"nodeType": "YulFunctionCall",
672672
"src": "60:13:0"
673673
},
674674
"variables": [
675675
{
676676
"name": "_1",
677-
"nativeSrc": "122:16:0",
677+
"nativeSrc": "132:2:0",
678678
"nodeType": "YulTypedName",
679679
"src": "60:13:0",
680680
"type": ""
@@ -686,36 +686,36 @@ Optimized IR AST:
686686
"arguments": [
687687
{
688688
"kind": "number",
689-
"nativeSrc": "118:2:0",
689+
"nativeSrc": "175:2:0",
690690
"nodeType": "YulLiteral",
691691
"src": "60:13:0",
692692
"type": "",
693693
"value": "64"
694694
},
695695
{
696696
"name": "_1",
697-
"nativeSrc": "122:16:0",
697+
"nativeSrc": "179:2:0",
698698
"nodeType": "YulIdentifier",
699699
"src": "60:13:0"
700700
}
701701
],
702702
"functionName": {
703703
"name": "mstore",
704-
"nativeSrc": "111:6:0",
704+
"nativeSrc": "168:6:0",
705705
"nodeType": "YulIdentifier",
706706
"src": "60:13:0"
707707
},
708-
"nativeSrc": "111:28:0",
708+
"nativeSrc": "168:14:0",
709709
"nodeType": "YulFunctionCall",
710710
"src": "60:13:0"
711711
},
712-
"nativeSrc": "111:28:0",
712+
"nativeSrc": "168:14:0",
713713
"nodeType": "YulExpressionStatement",
714714
"src": "60:13:0"
715715
},
716716
{
717717
"body": {
718-
"nativeSrc": "163:83:0",
718+
"nativeSrc": "210:16:0",
719719
"nodeType": "YulBlock",
720720
"src": "60:13:0",
721721
"statements": [
@@ -724,15 +724,15 @@ Optimized IR AST:
724724
"arguments": [
725725
{
726726
"kind": "number",
727-
"nativeSrc": "650:1:0",
727+
"nativeSrc": "219:1:0",
728728
"nodeType": "YulLiteral",
729729
"src": "60:13:0",
730730
"type": "",
731731
"value": "0"
732732
},
733733
{
734734
"kind": "number",
735-
"nativeSrc": "650:1:0",
735+
"nativeSrc": "222:1:0",
736736
"nodeType": "YulLiteral",
737737
"src": "60:13:0",
738738
"type": "",
@@ -741,15 +741,15 @@ Optimized IR AST:
741741
],
742742
"functionName": {
743743
"name": "revert",
744-
"nativeSrc": "640:6:0",
744+
"nativeSrc": "212:6:0",
745745
"nodeType": "YulIdentifier",
746746
"src": "60:13:0"
747747
},
748-
"nativeSrc": "640:12:0",
748+
"nativeSrc": "212:12:0",
749749
"nodeType": "YulFunctionCall",
750750
"src": "60:13:0"
751751
},
752-
"nativeSrc": "640:12:0",
752+
"nativeSrc": "212:12:0",
753753
"nodeType": "YulExpressionStatement",
754754
"src": "60:13:0"
755755
}
@@ -759,28 +759,28 @@ Optimized IR AST:
759759
"arguments": [],
760760
"functionName": {
761761
"name": "callvalue",
762-
"nativeSrc": "151:9:0",
762+
"nativeSrc": "198:9:0",
763763
"nodeType": "YulIdentifier",
764764
"src": "60:13:0"
765765
},
766-
"nativeSrc": "151:11:0",
766+
"nativeSrc": "198:11:0",
767767
"nodeType": "YulFunctionCall",
768768
"src": "60:13:0"
769769
},
770-
"nativeSrc": "148:98:0",
770+
"nativeSrc": "195:31:0",
771771
"nodeType": "YulIf",
772772
"src": "60:13:0"
773773
},
774774
{
775-
"nativeSrc": "363:24:0",
775+
"nativeSrc": "239:34:0",
776776
"nodeType": "YulVariableDeclaration",
777777
"src": "60:13:0",
778778
"value": {
779779
"arguments": [
780780
{
781781
"hexValue": "435f325f6465706c6f796564",
782782
"kind": "string",
783-
"nativeSrc": "372:14:0",
783+
"nativeSrc": "258:14:0",
784784
"nodeType": "YulLiteral",
785785
"src": "60:13:0",
786786
"type": "",
@@ -789,18 +789,18 @@ Optimized IR AST:
789789
],
790790
"functionName": {
791791
"name": "datasize",
792-
"nativeSrc": "363:8:0",
792+
"nativeSrc": "249:8:0",
793793
"nodeType": "YulIdentifier",
794794
"src": "60:13:0"
795795
},
796-
"nativeSrc": "363:24:0",
796+
"nativeSrc": "249:24:0",
797797
"nodeType": "YulFunctionCall",
798798
"src": "60:13:0"
799799
},
800800
"variables": [
801801
{
802802
"name": "_2",
803-
"nativeSrc": "363:24:0",
803+
"nativeSrc": "243:2:0",
804804
"nodeType": "YulTypedName",
805805
"src": "60:13:0",
806806
"type": ""
@@ -812,7 +812,7 @@ Optimized IR AST:
812812
"arguments": [
813813
{
814814
"name": "_1",
815-
"nativeSrc": "331:2:0",
815+
"nativeSrc": "295:2:0",
816816
"nodeType": "YulIdentifier",
817817
"src": "60:13:0"
818818
},
@@ -821,7 +821,7 @@ Optimized IR AST:
821821
{
822822
"hexValue": "435f325f6465706c6f796564",
823823
"kind": "string",
824-
"nativeSrc": "346:14:0",
824+
"nativeSrc": "310:14:0",
825825
"nodeType": "YulLiteral",
826826
"src": "60:13:0",
827827
"type": "",
@@ -830,32 +830,32 @@ Optimized IR AST:
830830
],
831831
"functionName": {
832832
"name": "dataoffset",
833-
"nativeSrc": "335:10:0",
833+
"nativeSrc": "299:10:0",
834834
"nodeType": "YulIdentifier",
835835
"src": "60:13:0"
836836
},
837-
"nativeSrc": "335:26:0",
837+
"nativeSrc": "299:26:0",
838838
"nodeType": "YulFunctionCall",
839839
"src": "60:13:0"
840840
},
841841
{
842842
"name": "_2",
843-
"nativeSrc": "363:24:0",
843+
"nativeSrc": "327:2:0",
844844
"nodeType": "YulIdentifier",
845845
"src": "60:13:0"
846846
}
847847
],
848848
"functionName": {
849849
"name": "codecopy",
850-
"nativeSrc": "322:8:0",
850+
"nativeSrc": "286:8:0",
851851
"nodeType": "YulIdentifier",
852852
"src": "60:13:0"
853853
},
854-
"nativeSrc": "322:66:0",
854+
"nativeSrc": "286:44:0",
855855
"nodeType": "YulFunctionCall",
856856
"src": "60:13:0"
857857
},
858-
"nativeSrc": "322:66:0",
858+
"nativeSrc": "286:44:0",
859859
"nodeType": "YulExpressionStatement",
860860
"src": "60:13:0"
861861
},
@@ -864,28 +864,28 @@ Optimized IR AST:
864864
"arguments": [
865865
{
866866
"name": "_1",
867-
"nativeSrc": "405:2:0",
867+
"nativeSrc": "350:2:0",
868868
"nodeType": "YulIdentifier",
869869
"src": "60:13:0"
870870
},
871871
{
872872
"name": "_2",
873-
"nativeSrc": "409:24:0",
873+
"nativeSrc": "354:2:0",
874874
"nodeType": "YulIdentifier",
875875
"src": "60:13:0"
876876
}
877877
],
878878
"functionName": {
879879
"name": "return",
880-
"nativeSrc": "398:6:0",
880+
"nativeSrc": "343:6:0",
881881
"nodeType": "YulIdentifier",
882882
"src": "60:13:0"
883883
},
884-
"nativeSrc": "398:36:0",
884+
"nativeSrc": "343:14:0",
885885
"nodeType": "YulFunctionCall",
886886
"src": "60:13:0"
887887
},
888-
"nativeSrc": "398:36:0",
888+
"nativeSrc": "343:14:0",
889889
"nodeType": "YulExpressionStatement",
890890
"src": "60:13:0"
891891
}
@@ -901,12 +901,12 @@ Optimized IR AST:
901901
{
902902
"code": {
903903
"block": {
904-
"nativeSrc": "929:588:0",
904+
"nativeSrc": "453:118:0",
905905
"nodeType": "YulBlock",
906906
"src": "-1:-1:0",
907907
"statements": [
908908
{
909-
"nativeSrc": "929:588:0",
909+
"nativeSrc": "467:94:0",
910910
"nodeType": "YulBlock",
911911
"src": "-1:-1:0",
912912
"statements": [
@@ -915,15 +915,15 @@ Optimized IR AST:
915915
"arguments": [
916916
{
917917
"kind": "number",
918-
"nativeSrc": "1490:1:0",
918+
"nativeSrc": "542:1:0",
919919
"nodeType": "YulLiteral",
920920
"src": "60:13:0",
921921
"type": "",
922922
"value": "0"
923923
},
924924
{
925925
"kind": "number",
926-
"nativeSrc": "1490:1:0",
926+
"nativeSrc": "545:1:0",
927927
"nodeType": "YulLiteral",
928928
"src": "60:13:0",
929929
"type": "",
@@ -932,15 +932,15 @@ Optimized IR AST:
932932
],
933933
"functionName": {
934934
"name": "revert",
935-
"nativeSrc": "1480:6:0",
935+
"nativeSrc": "535:6:0",
936936
"nodeType": "YulIdentifier",
937937
"src": "60:13:0"
938938
},
939-
"nativeSrc": "1480:12:0",
939+
"nativeSrc": "535:12:0",
940940
"nodeType": "YulFunctionCall",
941941
"src": "60:13:0"
942942
},
943-
"nativeSrc": "1480:12:0",
943+
"nativeSrc": "535:12:0",
944944
"nodeType": "YulExpressionStatement",
945945
"src": "60:13:0"
946946
}

0 commit comments

Comments
 (0)