Skip to content

Commit

Permalink
Merge rust-lang#95
Browse files Browse the repository at this point in the history
95: Serialise function types and argument operands. r=ltratt a=vext01



Co-authored-by: Edd Barrett <vext01@gmail.com>
  • Loading branch information
bors[bot] and vext01 authored Oct 17, 2023
2 parents ca1842c + 058e020 commit c9ec9b1
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions llvm/lib/YkIR/YkIRWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ enum OperandKind {
Type,
Function,
Block,
Arg,
UnimplementedOperand = 255,
};

enum TypeKind {
Void = 0,
Integer,
Ptr,
FunctionTy,
UnimplementedType = 255, // YKFIXME: Will eventually be deleted.
};

Expand Down Expand Up @@ -133,8 +135,17 @@ class YkIRWriter {
if (Found != Types.end()) {
return std::distance(Types.begin(), Found);
}

// Not found. Assign it a type index.
size_t Idx = Types.size();
Types.push_back(Ty);

// If the newly-registered type is an aggregate type that contains other
// types, then assign them type indices now too.
for (llvm::Type *STy : Ty->subtypes()) {
typeIndex(STy);
}

return Idx;
}

Expand Down Expand Up @@ -206,12 +217,20 @@ class YkIRWriter {
serialiseString(toString(V));
}

void serialiseArgOperand(ValueLoweringMap &VLMap, Argument *A) {
// This assumes that the argument indices match in both IRs.
OutStreamer.emitInt8(OperandKind::Arg);
OutStreamer.emitSizeT(A->getArgNo());
}

void serialiseOperand(Instruction *Parent, ValueLoweringMap &VLMap,
Value *V) {
if (llvm::Function *F = dyn_cast<llvm::Function>(V)) {
serialiseFunctionOperand(F);
} else if (llvm::Constant *C = dyn_cast<llvm::Constant>(V)) {
serialiseConstantOperand(Parent, C);
} else if (llvm::Argument *A = dyn_cast<llvm::Argument>(V)) {
serialiseArgOperand(VLMap, A);
} else if (Instruction *I = dyn_cast<Instruction>(V)) {
// If an instruction defines the operand, it's a local variable.
serialiseLocalVariableOperand(I, VLMap);
Expand Down Expand Up @@ -372,9 +391,17 @@ class YkIRWriter {
BBIdx++;
}

void serialiseArg(Argument *A) {
// type_index:
OutStreamer.emitSizeT(typeIndex(A->getType()));
}

void serialiseFunc(llvm::Function &F) {
// name:
serialiseString(F.getName());
// type_idx:
OutStreamer.emitSizeT(typeIndex(F.getFunctionType()));
F.getType()->dump();
// num_blocks:
OutStreamer.emitSizeT(F.size());
// blocks:
Expand All @@ -385,6 +412,20 @@ class YkIRWriter {
}
}

void serialiseFunctionType(FunctionType *Ty) {
OutStreamer.emitInt8(TypeKind::FunctionTy);
// num_args:
OutStreamer.emitSizeT(Ty->getNumParams());
// arg_tys:
for (llvm::Type *SubTy : Ty->params()) {
OutStreamer.emitSizeT(typeIndex(SubTy));
}
// ret_ty:
OutStreamer.emitSizeT(typeIndex(Ty->getReturnType()));
// is_vararg:
OutStreamer.emitInt8(Ty->isVarArg());
}

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

0 comments on commit c9ec9b1

Please sign in to comment.