@@ -55,13 +55,15 @@ enum OperandKind {
5555 Type,
5656 Function,
5757 Block,
58+ Arg,
5859 UnimplementedOperand = 255 ,
5960};
6061
6162enum TypeKind {
6263 Void = 0 ,
6364 Integer,
6465 Ptr,
66+ FunctionTy,
6567 UnimplementedType = 255 , // YKFIXME: Will eventually be deleted.
6668};
6769
@@ -133,8 +135,17 @@ class YkIRWriter {
133135 if (Found != Types.end ()) {
134136 return std::distance (Types.begin (), Found);
135137 }
138+
139+ // Not found. Assign it a type index.
136140 size_t Idx = Types.size ();
137141 Types.push_back (Ty);
142+
143+ // If the newly-registered type is an aggregate type that contains other
144+ // types, then assign them type indices now too.
145+ for (llvm::Type *STy : Ty->subtypes ()) {
146+ typeIndex (STy);
147+ }
148+
138149 return Idx;
139150 }
140151
@@ -206,12 +217,20 @@ class YkIRWriter {
206217 serialiseString (toString (V));
207218 }
208219
220+ void serialiseArgOperand (ValueLoweringMap &VLMap, Argument *A) {
221+ // This assumes that the argument indices match in both IRs.
222+ OutStreamer.emitInt8 (OperandKind::Arg);
223+ OutStreamer.emitSizeT (A->getArgNo ());
224+ }
225+
209226 void serialiseOperand (Instruction *Parent, ValueLoweringMap &VLMap,
210227 Value *V) {
211228 if (llvm::Function *F = dyn_cast<llvm::Function>(V)) {
212229 serialiseFunctionOperand (F);
213230 } else if (llvm::Constant *C = dyn_cast<llvm::Constant>(V)) {
214231 serialiseConstantOperand (Parent, C);
232+ } else if (llvm::Argument *A = dyn_cast<llvm::Argument>(V)) {
233+ serialiseArgOperand (VLMap, A);
215234 } else if (Instruction *I = dyn_cast<Instruction>(V)) {
216235 // If an instruction defines the operand, it's a local variable.
217236 serialiseLocalVariableOperand (I, VLMap);
@@ -372,9 +391,17 @@ class YkIRWriter {
372391 BBIdx++;
373392 }
374393
394+ void serialiseArg (Argument *A) {
395+ // type_index:
396+ OutStreamer.emitSizeT (typeIndex (A->getType ()));
397+ }
398+
375399 void serialiseFunc (llvm::Function &F) {
376400 // name:
377401 serialiseString (F.getName ());
402+ // type_idx:
403+ OutStreamer.emitSizeT (typeIndex (F.getFunctionType ()));
404+ F.getType ()->dump ();
378405 // num_blocks:
379406 OutStreamer.emitSizeT (F.size ());
380407 // blocks:
@@ -385,6 +412,20 @@ class YkIRWriter {
385412 }
386413 }
387414
415+ void serialiseFunctionType (FunctionType *Ty) {
416+ OutStreamer.emitInt8 (TypeKind::FunctionTy);
417+ // num_args:
418+ OutStreamer.emitSizeT (Ty->getNumParams ());
419+ // arg_tys:
420+ for (llvm::Type *SubTy : Ty->params ()) {
421+ OutStreamer.emitSizeT (typeIndex (SubTy));
422+ }
423+ // ret_ty:
424+ OutStreamer.emitSizeT (typeIndex (Ty->getReturnType ()));
425+ // is_vararg:
426+ OutStreamer.emitInt8 (Ty->isVarArg ());
427+ }
428+
388429 void serialiseType (llvm::Type *Ty) {
389430 if (Ty->isVoidTy ()) {
390431 OutStreamer.emitInt8 (TypeKind::Void);
@@ -393,6 +434,8 @@ class YkIRWriter {
393434 } else if (IntegerType *ITy = dyn_cast<IntegerType>(Ty)) {
394435 OutStreamer.emitInt8 (TypeKind::Integer);
395436 OutStreamer.emitInt32 (ITy->getBitWidth ());
437+ } else if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
438+ serialiseFunctionType (FTy);
396439 } else {
397440 OutStreamer.emitInt8 (TypeKind::UnimplementedType);
398441 serialiseString (toString (Ty));
0 commit comments