Skip to content

Commit 1bf21ee

Browse files
eee4017hydai
authored andcommitted
[CodeGen] Solve int256/bool binary operation; Add Immutable Array as a global table
1 parent fe1791f commit 1bf21ee

File tree

6 files changed

+62
-11
lines changed

6 files changed

+62
-11
lines changed

lib/CodeGen/CGExpr.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <llvm/ADT/StringRef.h>
1010
#include <llvm/IR/Type.h>
1111
#include <llvm/IR/Value.h>
12+
#include <llvm/Support/raw_ostream.h>
1213
namespace soll::CodeGen {
1314

1415
ExprValuePtr CodeGenFunction::emitExpr(const Expr *E) {
@@ -1011,9 +1012,18 @@ void CodeGenFunction::emitAsmSetImmutable(const CallExpr *CE) {
10111012
auto &Ctx = CGM.getContext();
10121013
auto &Map = Ctx.getImmutableAddressMap();
10131014
if (Map.find(StringRefName) != Map.end()) {
1014-
llvm::Value *Offset = Builder.getInt(Map.lookup(StringRefName));
1015-
llvm::Value *Ptr = Builder.CreateIntToPtr(Offset, Int256PtrTy);
1016-
Builder.CreateStore(Value, Ptr);
1015+
llvm::Value *Offset = emitExpr(Arguments[0])->load(Builder, CGM);
1016+
llvm::Value *TableOffset = Builder.getInt(Map.lookup(StringRefName));
1017+
llvm::Value *ImmutableCPtr = Builder.CreateInBoundsGEP(
1018+
CGM.getImmutableBase(), {TableOffset}, "immutable.cptr");
1019+
Builder.CreateStore(
1020+
Offset,
1021+
Builder.CreateBitCast(ImmutableCPtr, Int256PtrTy, "immutable.ptr"));
1022+
1023+
llvm::Value *HeapCPtr =
1024+
Builder.CreateInBoundsGEP(CGM.getHeapBase(), {Offset}, "heap.cptr");
1025+
Builder.CreateStore(
1026+
Value, Builder.CreateBitCast(HeapCPtr, Int256PtrTy, "heap.ptr"));
10171027
CGM.emitUpdateMemorySize(Offset, Builder.getIntN(256, 0x20));
10181028
return;
10191029
}
@@ -1032,9 +1042,16 @@ llvm::Value *CodeGenFunction::emitAsmLoadImmutable(const CallExpr *CE) {
10321042
auto &Ctx = CGM.getContext();
10331043
auto &Map = Ctx.getImmutableAddressMap();
10341044
if (Map.find(StringRefName) != Map.end()) {
1035-
llvm::Value *Offset = Builder.getInt(Map.lookup(StringRefName));
1036-
llvm::Value *Ptr = Builder.CreateIntToPtr(Offset, Int256PtrTy);
1037-
return CGM.getEndianlessValue(Builder.CreateLoad(Ptr, Int256Ty));
1045+
llvm::Value *TableOffset = Builder.getInt(Map.lookup(StringRefName));
1046+
llvm::Value *ImmutableCPtr = Builder.CreateInBoundsGEP(
1047+
CGM.getImmutableBase(), {TableOffset}, "immutable.cptr");
1048+
llvm::Value *Offset = Builder.CreateLoad(
1049+
Builder.CreateBitCast(ImmutableCPtr, Int256PtrTy, "immutable.ptr"));
1050+
1051+
llvm::Value *HeapCPtr =
1052+
Builder.CreateInBoundsGEP(CGM.getHeapBase(), {Offset}, "heap.cptr");
1053+
return Builder.CreateLoad(
1054+
Builder.CreateBitCast(HeapCPtr, Int256PtrTy, "heap.ptr"));
10381055
}
10391056
}
10401057
}

lib/CodeGen/CodeGenFunction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <llvm/IR/BasicBlock.h>
1010
#include <llvm/IR/ConstantFolder.h>
1111
#include <llvm/IR/IRBuilder.h>
12+
#include <llvm/IR/Value.h>
1213

1314
namespace soll::CodeGen {
1415

@@ -26,7 +27,6 @@ class CodeGenFunction : public CodeGenTypeCache {
2627
llvm::Value *ReturnValue;
2728

2829
using DeclMapTy = llvm::DenseMap<const Decl *, llvm::Value *>;
29-
llvm::StringMap<llvm::Value *> ImmutableAllocationTable;
3030
DeclMapTy LocalDeclMap;
3131
void setAddrOfLocalVar(const Decl *VD, llvm::Value *Addr) {
3232
assert(!LocalDeclMap.count(VD) && "Decl already exists in LocalDeclMap!");

lib/CodeGen/CodeGenModule.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
#include "CodeGenModule.h"
33
#include "ABICodec.h"
44
#include "CodeGenFunction.h"
5+
#include <llvm/ADT/APInt.h>
56
#include <llvm/IR/Attributes.h>
67
#include <llvm/IR/Constants.h>
8+
#include <llvm/IR/DerivedTypes.h>
79

810
/*
911
// for testing purpose
@@ -125,6 +127,22 @@ void CodeGenModule::initMemorySection() {
125127
initUpdateMemorySize();
126128
}
127129

130+
void CodeGenModule::initImmutableTable() {
131+
size_t ImmutableSize = Context.getImmutableAddressMap().size();
132+
if (ImmutableSize && !ImmtableTable) {
133+
// Immutable array is a array of offsets of memory
134+
llvm::ArrayType *ImmtableArrayType =
135+
llvm::ArrayType::get(Int256Ty, ImmutableSize);
136+
ImmtableTable =
137+
new llvm::GlobalVariable(TheModule, ImmtableArrayType, false,
138+
llvm::GlobalVariable::PrivateLinkage, nullptr,
139+
"__immutable_table_base");
140+
ImmtableTable->setInitializer(
141+
llvm::Constant::getNullValue(ImmtableArrayType));
142+
ImmtableTable->setAlignment(llvm::MaybeAlign(256));
143+
}
144+
}
145+
128146
void CodeGenModule::initUpdateMemorySize() {
129147
llvm::Argument *Pos = Func_updateMemorySize->arg_begin();
130148
Pos->setName("memory.pos");
@@ -1622,6 +1640,14 @@ void CodeGenModule::emitYulCode(const YulCode *YC, llvm::StringRef Name) {
16221640
FT, llvm::Function::ExternalLinkage, FunctionName, TheModule);
16231641
llvm::BasicBlock *Entry = llvm::BasicBlock::Create(VMContext, "entry", Main);
16241642
Builder.SetInsertPoint(Entry);
1643+
1644+
// Global variable Immutable table should be generated here.
1645+
// Immutable table should not be generated with other global variables
1646+
// (e.g. heap_base), since the parsing stage have not finished, so the number
1647+
// of the immutable variable is not decided. The table should be generated
1648+
// as early as possible, so the immutable variable could be set at any time.
1649+
initImmutableTable();
1650+
16251651
CodeGenFunction(*this).generateYulCode(YC);
16261652
Builder.CreateRetVoid();
16271653
}

lib/CodeGen/CodeGenModule.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class CodeGenModule : public CodeGenTypeCache {
6363
llvm::DenseMap<const VarDecl *, llvm::GlobalVariable *> StateVarDeclMap;
6464
llvm::DenseMap<const YulData *, llvm::GlobalVariable *> YulDataMap;
6565
std::size_t StateVarAddrCursor;
66+
llvm::GlobalVariable *ImmtableTable = nullptr;
67+
llvm::ArrayType *ImmtableArrayType = nullptr;
6668

6769
llvm::Function *Func_create = nullptr;
6870
llvm::Function *Func_call = nullptr;
@@ -122,6 +124,7 @@ class CodeGenModule : public CodeGenTypeCache {
122124
void initTypes();
123125
void initMemorySection();
124126
void initUpdateMemorySize();
127+
void initImmutableTable();
125128

126129
void initEVMOpcodeDeclaration();
127130
void initEEIDeclaration();
@@ -279,6 +282,8 @@ class CodeGenModule : public CodeGenTypeCache {
279282
llvm::GlobalVariable *getYulDataAddr(const YulData *YD) const {
280283
return YulDataMap.lookup(YD);
281284
}
285+
llvm::GlobalVariable *getImmutableBase() { return ImmtableTable; }
286+
282287
std::variant<std::monostate, const YulData *, const YulObject *>
283288
lookupYulDataOrYulObject(llvm::StringRef Name) const;
284289
};

lib/Sema/SemaResolveImmutable.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ class ImmutableResolver : public StmtVisitor {
3838
auto *ICE0 = dynamic_cast<const ImplicitCastExpr *>(Arguments[0]);
3939
auto *ICE1 = dynamic_cast<const ImplicitCastExpr *>(Arguments[1]);
4040
if (ICE0 && ICE1) {
41-
auto *NL = dynamic_cast<const NumberLiteral *>(ICE0->getSubExpr());
4241
auto *SL = dynamic_cast<const StringLiteral *>(ICE1->getSubExpr());
43-
if (SL && NL) {
42+
if (SL) {
4443
std::string Name = SL->getValue();
4544
auto &ImmutableMap = Actions.getContext().getImmutableAddressMap();
46-
ImmutableMap.try_emplace(llvm::StringRef(Name), NL->getValue());
45+
const size_t ImmutableIndex = ImmutableMap.size();
46+
ImmutableMap.try_emplace(llvm::StringRef(Name),
47+
llvm::APInt(256, ImmutableIndex));
4748
}
4849
}
4950
}

lib/Sema/SemaResolveType.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include "soll/Sema/Sema.h"
55
#include <unordered_set>
66
#include <vector>
7-
87
namespace soll {
98
namespace {
109

@@ -29,6 +28,9 @@ bool isAllowedTypeForBinary(BinaryOperatorKind BOK, const Type::Category TyC) {
2928
case Type::Category::Bool:
3029
return BinaryOperator::isEqualityOp(BOK) || BOK == BO_LAnd ||
3130
BOK == BO_LOr || BOK == BO_Assign ||
31+
BinaryOperator::isShiftOp(BOK) ||
32+
BinaryOperator::isAdditiveOp(BOK) ||
33+
BinaryOperator::isMultiplicativeOp(BOK) ||
3234
BinaryOperator::isAsmBitwiseOp(BOK);
3335
case Type::Category::Integer:
3436
return BinaryOperator::isComparisonOp(BOK) ||

0 commit comments

Comments
 (0)