Skip to content

Commit

Permalink
Add structure types to the Yk IR.
Browse files Browse the repository at this point in the history
(and also the `insertvalue` opcode to allow for testing in `yk`)
  • Loading branch information
vext01 committed Nov 15, 2023
1 parent 82b2e13 commit c21b1be
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions llvm/lib/YkIR/YkIRWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ enum OpCode {
ICmp,
BinaryOperator,
Ret,
InsertValue,
UnimplementedInstruction = 255, // YKFIXME: Will eventually be deleted.
};

Expand All @@ -63,6 +64,7 @@ enum TypeKind {
Integer,
Ptr,
FunctionTy,
Struct,
UnimplementedType = 255, // YKFIXME: Will eventually be deleted.
};

Expand Down Expand Up @@ -350,6 +352,7 @@ class YkIRWriter {
GENERIC_INST_SERIALISE(I, ICmpInst, ICmp)
GENERIC_INST_SERIALISE(I, llvm::BinaryOperator, BinaryOperator)
GENERIC_INST_SERIALISE(I, ReturnInst, Ret)
GENERIC_INST_SERIALISE(I, llvm::InsertValueInst, InsertValue)

CUSTOM_INST_SERIALISE(I, AllocaInst, serialiseAllocaInst)
CUSTOM_INST_SERIALISE(I, CallInst, serialiseCallInst)
Expand Down Expand Up @@ -426,6 +429,23 @@ class YkIRWriter {
OutStreamer.emitInt8(Ty->isVarArg());
}

void serialiseStructType(StructType *STy) {
OutStreamer.emitInt8(TypeKind::Struct);
unsigned NumFields = STy->getNumElements();
DataLayout DL(&M);
const StructLayout *SL = DL.getStructLayout(STy);
// num_fields:
OutStreamer.emitSizeT(NumFields);
// field_tys:
for (unsigned I = 0; I < NumFields; I++) {
OutStreamer.emitSizeT(typeIndex(STy->getElementType(I)));
}
// field_bit_offs:
for (unsigned I = 0; I < NumFields; I++) {
OutStreamer.emitSizeT(SL->getElementOffsetInBits(I));
}
}

void serialiseType(llvm::Type *Ty) {
if (Ty->isVoidTy()) {
OutStreamer.emitInt8(TypeKind::Void);
Expand All @@ -436,6 +456,8 @@ class YkIRWriter {
OutStreamer.emitInt32(ITy->getBitWidth());
} else if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
serialiseFunctionType(FTy);
} else if (StructType *STy = dyn_cast<StructType>(Ty)) {
serialiseStructType(STy);
} else {
OutStreamer.emitInt8(TypeKind::UnimplementedType);
serialiseString(toString(Ty));
Expand Down

0 comments on commit c21b1be

Please sign in to comment.