@@ -232,7 +232,7 @@ void CompilerStack::setModelCheckerSettings(ModelCheckerSettings _settings)
232
232
m_modelCheckerSettings = _settings;
233
233
}
234
234
235
- void CompilerStack::setLibraries (std:: map<std:: string, util::h160> const & _libraries)
235
+ void CompilerStack::setLibraries (map<string, util::h160> const & _libraries)
236
236
{
237
237
if (m_stackState >= ParsedAndImported)
238
238
solThrow (CompilerError, " Must set libraries before parsing." );
@@ -242,6 +242,7 @@ void CompilerStack::setLibraries(std::map<std::string, util::h160> const& _libra
242
242
void CompilerStack::setOptimiserSettings (bool _optimize, size_t _runs)
243
243
{
244
244
OptimiserSettings settings = _optimize ? OptimiserSettings::standard () : OptimiserSettings::minimal ();
245
+ settings.enabled = _optimize;
245
246
settings.expectedExecutionsPerDeployment = _runs;
246
247
setOptimiserSettings (std::move (settings));
247
248
}
@@ -416,6 +417,29 @@ void CompilerStack::importASTs(map<string, Json::Value> const& _sources)
416
417
storeContractDefinitions ();
417
418
}
418
419
420
+ void CompilerStack::importEvmAssemblyJson (map<string, Json::Value> const & _sources)
421
+ {
422
+ solAssert (_sources.size () == 1 , " " );
423
+ solAssert (m_sourceJsons.empty (), " " );
424
+ solAssert (m_sourceOrder.empty (), " " );
425
+ if (m_stackState != Empty)
426
+ solThrow (CompilerError, " Must call importEvmAssemblyJson only before the SourcesSet state." );
427
+
428
+ m_sourceJsons = _sources;
429
+ Json::Value jsonValue = _sources.begin ()->second ;
430
+ if (jsonValue.isMember (" sourceList" ))
431
+ for (auto const & item: jsonValue[" sourceList" ])
432
+ {
433
+ Source source;
434
+ source.charStream = make_shared<CharStream>(item.asString (), " " );
435
+ m_sources.emplace (make_pair (item.asString (), source));
436
+ m_sourceOrder.push_back (&m_sources[item.asString ()]);
437
+ }
438
+ m_sourceJsons[_sources.begin ()->first ] = std::move (jsonValue);
439
+ m_compilationSourceType = CompilationSourceType::EvmAssemblyJson;
440
+ m_stackState = SourcesSet;
441
+ }
442
+
419
443
bool CompilerStack::analyze ()
420
444
{
421
445
if (m_stackState != ParsedAndImported || m_stackState >= AnalysisPerformed)
@@ -605,6 +629,9 @@ bool CompilerStack::parseAndAnalyze(State _stopAfter)
605
629
{
606
630
m_stopAfter = _stopAfter;
607
631
632
+ if (m_compilationSourceType == CompilationSourceType::EvmAssemblyJson)
633
+ return true ;
634
+
608
635
bool success = parse ();
609
636
if (m_stackState >= m_stopAfter)
610
637
return success;
@@ -654,53 +681,73 @@ bool CompilerStack::compile(State _stopAfter)
654
681
// Only compile contracts individually which have been requested.
655
682
map<ContractDefinition const *, shared_ptr<Compiler const >> otherCompilers;
656
683
657
- for (Source const * source: m_sourceOrder)
658
- for (ASTPointer<ASTNode> const & node: source->ast ->nodes ())
659
- if (auto contract = dynamic_cast <ContractDefinition const *>(node.get ()))
660
- if (isRequestedContract (*contract))
661
- {
662
- try
684
+ if (m_compilationSourceType == CompilationSourceType::EvmAssemblyJson)
685
+ {
686
+ solAssert (m_sourceJsons.size () == 1 );
687
+
688
+ string const evmSourceName = m_sourceJsons.begin ()->first ;
689
+ Json::Value const evmJson = m_sourceJsons.begin ()->second ;
690
+
691
+ evmasm::Assembly::OptimiserSettings optimiserSettings =
692
+ evmasm::Assembly::OptimiserSettings::translateSettings (m_optimiserSettings, m_evmVersion);
693
+
694
+ m_contracts[evmSourceName].evmAssembly = evmasm::Assembly::loadFromAssemblyJSON (m_sourceJsons[evmSourceName]);
695
+ if (m_optimiserSettings.enabled )
696
+ m_contracts[evmSourceName].evmAssembly ->optimise (optimiserSettings);
697
+ m_contracts[evmSourceName].object = m_contracts[evmSourceName].evmAssembly ->assemble ();
698
+
699
+ m_contracts[evmSourceName].evmRuntimeAssembly = make_shared<evmasm::Assembly>(m_contracts[evmSourceName].evmAssembly ->sub (0 ));
700
+ solAssert (m_contracts[evmSourceName].evmRuntimeAssembly ->isCreation () == false );
701
+ if (m_optimiserSettings.enabled )
702
+ m_contracts[evmSourceName].evmRuntimeAssembly ->optimise (optimiserSettings);
703
+ m_contracts[evmSourceName].runtimeObject = m_contracts[evmSourceName].evmRuntimeAssembly ->assemble ();
704
+ }
705
+ else
706
+ {
707
+ for (Source const * source: m_sourceOrder)
708
+ for (ASTPointer<ASTNode> const & node: source->ast ->nodes ())
709
+ if (auto contract = dynamic_cast <ContractDefinition const *>(node.get ()))
710
+ if (isRequestedContract (*contract))
663
711
{
664
- if (m_viaIR || m_generateIR || m_generateEwasm)
665
- generateIR (*contract);
666
- if (m_generateEvmBytecode)
712
+ try
667
713
{
668
- if (m_viaIR)
669
- generateEVMFromIR (*contract);
670
- else
671
- compileContract (*contract, otherCompilers);
714
+ if (m_viaIR || m_generateIR || m_generateEwasm)
715
+ generateIR (*contract);
716
+ if (m_generateEvmBytecode)
717
+ {
718
+ if (m_viaIR)
719
+ generateEVMFromIR (*contract);
720
+ else
721
+ compileContract (*contract, otherCompilers);
722
+ }
723
+ if (m_generateEwasm)
724
+ generateEwasm (*contract);
672
725
}
673
- if (m_generateEwasm)
674
- generateEwasm (*contract);
675
- }
676
- catch (Error const & _error)
677
- {
678
- if (_error.type () != Error::Type::CodeGenerationError)
679
- throw ;
680
- m_errorReporter.error (_error.errorId (), _error.type (), SourceLocation (), _error.what ());
681
- return false ;
682
- }
683
- catch (UnimplementedFeatureError const & _unimplementedError)
684
- {
685
- if (
686
- SourceLocation const * sourceLocation =
687
- boost::get_error_info<langutil::errinfo_sourceLocation>(_unimplementedError)
688
- )
726
+ catch (Error const & _error)
689
727
{
690
- string const * comment = _unimplementedError.comment ();
691
- m_errorReporter.error (
692
- 1834_error,
693
- Error::Type::CodeGenerationError,
694
- *sourceLocation,
695
- " Unimplemented feature error" +
696
- ((comment && !comment->empty ()) ? " : " + *comment : string{}) +
697
- " in " +
698
- _unimplementedError.lineInfo ()
699
- );
728
+ if (_error.type () != Error::Type::CodeGenerationError)
729
+ throw ;
730
+ m_errorReporter.error (_error.errorId (), _error.type (), SourceLocation (), _error.what ());
700
731
return false ;
701
732
}
702
- else
703
- throw ;
733
+ catch (UnimplementedFeatureError const & _unimplementedError)
734
+ {
735
+ if (SourceLocation const * sourceLocation
736
+ = boost::get_error_info<langutil::errinfo_sourceLocation>(_unimplementedError))
737
+ {
738
+ string const * comment = _unimplementedError.comment ();
739
+ m_errorReporter.error (
740
+ 1834_error,
741
+ Error::Type::CodeGenerationError,
742
+ *sourceLocation,
743
+ " Unimplemented feature error"
744
+ + ((comment && !comment->empty ()) ? " : " + *comment : string{}) + " in "
745
+ + _unimplementedError.lineInfo ());
746
+ return false ;
747
+ }
748
+ else
749
+ throw ;
750
+ }
704
751
}
705
752
}
706
753
m_stackState = CompilationSuccessful;
@@ -795,7 +842,6 @@ Json::Value CompilerStack::generatedSources(string const& _contractName, bool _r
795
842
sources[0 ][" id" ] = sourceIndex;
796
843
sources[0 ][" language" ] = " Yul" ;
797
844
sources[0 ][" contents" ] = std::move (source);
798
-
799
845
}
800
846
}
801
847
return sources;
@@ -832,7 +878,7 @@ string const* CompilerStack::runtimeSourceMapping(string const& _contractName) c
832
878
return c.runtimeSourceMapping ? &*c.runtimeSourceMapping : nullptr ;
833
879
}
834
880
835
- std:: string const CompilerStack::filesystemFriendlyName (string const & _contractName) const
881
+ string const CompilerStack::filesystemFriendlyName (string const & _contractName) const
836
882
{
837
883
if (m_stackState < AnalysisPerformed)
838
884
solThrow (CompilerError, " No compiled contracts found." );
@@ -846,7 +892,7 @@ std::string const CompilerStack::filesystemFriendlyName(string const& _contractN
846
892
contract.second .contract != matchContract.contract )
847
893
{
848
894
// If it does, then return its fully-qualified name, made fs-friendly
849
- std:: string friendlyName = boost::algorithm::replace_all_copy (_contractName, " /" , " _" );
895
+ string friendlyName = boost::algorithm::replace_all_copy (_contractName, " /" , " _" );
850
896
boost::algorithm::replace_all (friendlyName, " :" , " _" );
851
897
boost::algorithm::replace_all (friendlyName, " ." , " _" );
852
898
return friendlyName;
@@ -943,9 +989,10 @@ map<string, unsigned> CompilerStack::sourceIndices() const
943
989
map<string, unsigned > indices;
944
990
unsigned index = 0 ;
945
991
for (auto const & s: m_sources)
946
- indices[s.first ] = index++;
947
- solAssert (!indices.count (CompilerContext::yulUtilityFileName ()), " " );
948
- indices[CompilerContext::yulUtilityFileName ()] = index++;
992
+ if (s.first != CompilerContext::yulUtilityFileName ())
993
+ indices[s.first ] = index++;
994
+ if (indices.find (CompilerContext::yulUtilityFileName ()) == indices.end ())
995
+ indices[CompilerContext::yulUtilityFileName ()] = index++;
949
996
return indices;
950
997
}
951
998
@@ -1098,7 +1145,7 @@ ContractDefinition const& CompilerStack::contractDefinition(string const& _contr
1098
1145
}
1099
1146
1100
1147
size_t CompilerStack::functionEntryPoint (
1101
- std:: string const & _contractName,
1148
+ string const & _contractName,
1102
1149
FunctionDefinition const & _function
1103
1150
) const
1104
1151
{
@@ -1240,8 +1287,8 @@ bool onlySafeExperimentalFeaturesActivated(set<ExperimentalFeature> const& featu
1240
1287
1241
1288
void CompilerStack::assemble (
1242
1289
ContractDefinition const & _contract,
1243
- std:: shared_ptr<evmasm::Assembly> _assembly,
1244
- std:: shared_ptr<evmasm::Assembly> _runtimeAssembly
1290
+ shared_ptr<evmasm::Assembly> _assembly,
1291
+ shared_ptr<evmasm::Assembly> _runtimeAssembly
1245
1292
)
1246
1293
{
1247
1294
solAssert (m_stackState >= AnalysisPerformed, " " );
@@ -1492,6 +1539,11 @@ string CompilerStack::createMetadata(Contract const& _contract, bool _forIR) con
1492
1539
case CompilationSourceType::SolidityAST:
1493
1540
sourceType = " SolidityAST" ;
1494
1541
break ;
1542
+ case CompilationSourceType::EvmAssemblyJson:
1543
+ sourceType = " EvmAssemblyJson" ;
1544
+ break ;
1545
+ default :
1546
+ solAssert (false );
1495
1547
}
1496
1548
meta[" language" ] = sourceType;
1497
1549
meta[" compiler" ][" version" ] = VersionStringStrict;
@@ -1523,7 +1575,7 @@ string CompilerStack::createMetadata(Contract const& _contract, bool _forIR) con
1523
1575
}
1524
1576
1525
1577
static_assert (sizeof (m_optimiserSettings.expectedExecutionsPerDeployment ) <= sizeof (Json::LargestUInt), " Invalid word size." );
1526
- solAssert (static_cast <Json::LargestUInt>(m_optimiserSettings.expectedExecutionsPerDeployment ) < std:: numeric_limits<Json::LargestUInt>::max (), " " );
1578
+ solAssert (static_cast <Json::LargestUInt>(m_optimiserSettings.expectedExecutionsPerDeployment ) < numeric_limits<Json::LargestUInt>::max (), " " );
1527
1579
meta[" settings" ][" optimizer" ][" runs" ] = Json::Value (Json::LargestUInt (m_optimiserSettings.expectedExecutionsPerDeployment ));
1528
1580
1529
1581
// / Backwards compatibility: If set to one of the default settings, do not provide details.
0 commit comments