diff --git a/generic/llvmtcl.cpp b/generic/llvmtcl.cpp index d111c49..80539df 100644 --- a/generic/llvmtcl.cpp +++ b/generic/llvmtcl.cpp @@ -54,23 +54,6 @@ #include #endif // API_7 -/* - * ------------------------------------------------------------------------ - * - * Missing piece of LLVM C API. - * - * ------------------------------------------------------------------------ - */ - -static void -LLVMAddCoroutinePassesToExtensionPoints( - LLVMPassManagerBuilderRef ref) -{ - auto Builder = reinterpret_cast(ref); - - addCoroutinePassesToExtensionPoints(*Builder); -} - /* * ------------------------------------------------------------------------ * @@ -307,6 +290,273 @@ LLVMDeleteBasicBlockTcl( LLVMBasicBlockRef_refmap.erase(basicBlockRef); } +/* + * ------------------------------------------------------------------------ + * + * Adding passes, including those not exposed via LLVM C API. + * + * ------------------------------------------------------------------------ + */ + +static void +LLVMAddCoroutinePassesToExtensionPoints( + LLVMPassManagerBuilderRef ref) +{ + auto Builder = reinterpret_cast(ref); + + addCoroutinePassesToExtensionPoints(*Builder); +} + +static int +LLVMTclAddPass( + ClientData clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *const objv[]) +{ + const char *passes[] = { + "aggressivedce", "alwaysinliner", "argumentpromotion", + "basicaliasanalysis", "cfgsimplification", "constantmerge", + "constantpropagation", "correlatedvaluepropagation", + "deadargelimination", "deadstoreelimination", "demotememorytoregister", + "earlycse", "functionattrs", "functioninlining", "globaldce", + "globaloptimizer", "gvn", "indvarsimplify", "instructioncombining", + "internalize", "ipconstantpropagation", "ipsccp", "jumpthreading", + "licm", "loopdeletion", "loopidiom", "looprotate", "loopunroll", + "loopunswitch", "loopvectorize", "lowerexpectintrinsic", "memcpyopt", + "promotememorytoregister", "pruneeh", "reassociate", + "scalarreplaggregates", "scalarreplaggregatesssa", + "scalarreplaggregateswiththreshold" + "sccp", "simplifylibcalls", "stripdeadprototypes", "stripsymbols", + "tailcallelimination", "typebasedaliasanalysis", "verifier", + nullptr + }; + enum Passes { + AGGRESSIVE_DCE, ALWAYS_INLINER, ARG_PROMOTION, BASIC_ALIAS_ANALYSIS, + CFG_SIMPLIFY, CONST_MERGE, CONST_PROP, CORRELATED_VALUE_PROP, + DEAD_ARG_ELIM, DEAD_STORE_ELIM, DEMOTE_MEMORY, EARLY_CSE, FUN_ATTRS, + FUN_INLINE, GLOBAL_DCE, GLOBAL_OPT, GVN, IND_VAR_SIMLIFY, + INST_COMBINE, INTERNALIZE, IP_CONST_PROP, IP_SCCP, JUMP_THREAD, LICM, + LOOP_DEL, LOOP_IDIOM, LOOP_ROT, LOOP_UNROLL, LOOP_UNSWITCH, LOOP_VECT, + LOWER_EXPECT, MEMCPY_OPT, PROMOTE_MEMORY, PRUNE_EH, REASSOCIATE, + SCALAR_REPL_AGGR, SCALAR_REPL_AGGR_SSA, SCALAR_REPL_AGGR_THRESHOLD, + SCCP, SIMPLIFY_LIB_CALLS, STRIP_DEAD_PROTO, STRIP_SYM, TAILCALL_ELIM, + TBAA, VERIFIER + }; + + if (objc != 3 && objc != 4) { + Tcl_WrongNumArgs(interp, 1, objv, "passName PassManager ?arg?"); + return TCL_ERROR; + } + + int idx = -1; + if (Tcl_GetIndexFromObj(interp, objv[1], passes, "pass", 0, &idx)) + return TCL_ERROR; + LLVMPassManagerRef PM; + if (GetLLVMPassManagerRefFromObj(interp, objv[2], PM)) + return TCL_ERROR; + + switch ((enum Passes) idx) { + case AGGRESSIVE_DCE: + if (objc != 3) goto bad3arg; + LLVMAddAggressiveDCEPass(PM); + break; + case ALWAYS_INLINER: + if (objc != 3) goto bad3arg; + LLVMAddAlwaysInlinerPass(PM); + break; + case ARG_PROMOTION: + if (objc != 3) goto bad3arg; + LLVMAddArgumentPromotionPass(PM); + break; + case BASIC_ALIAS_ANALYSIS: + if (objc != 3) goto bad3arg; + LLVMAddBasicAliasAnalysisPass(PM); + break; + case CFG_SIMPLIFY: + if (objc != 3) goto bad3arg; + LLVMAddCFGSimplificationPass(PM); + break; + case CONST_MERGE: + if (objc != 3) goto bad3arg; + LLVMAddConstantMergePass(PM); + break; + case CONST_PROP: + if (objc != 3) goto bad3arg; + LLVMAddConstantPropagationPass(PM); + break; + case CORRELATED_VALUE_PROP: + if (objc != 3) goto bad3arg; + LLVMAddCorrelatedValuePropagationPass(PM); + break; + case DEAD_ARG_ELIM: + if (objc != 3) goto bad3arg; + LLVMAddDeadArgEliminationPass(PM); + break; + case DEAD_STORE_ELIM: + if (objc != 3) goto bad3arg; + LLVMAddDeadStoreEliminationPass(PM); + break; + case DEMOTE_MEMORY: + if (objc != 3) goto bad3arg; + LLVMAddDemoteMemoryToRegisterPass(PM); + break; + case EARLY_CSE: + if (objc != 3) goto bad3arg; + LLVMAddEarlyCSEPass(PM); + break; + case FUN_ATTRS: + if (objc != 3) goto bad3arg; + LLVMAddFunctionAttrsPass(PM); + break; + case FUN_INLINE: + if (objc != 3) goto bad3arg; + LLVMAddFunctionInliningPass(PM); + break; + case GLOBAL_DCE: + if (objc != 3) goto bad3arg; + LLVMAddGlobalDCEPass(PM); + break; + case GLOBAL_OPT: + if (objc != 3) goto bad3arg; + LLVMAddGlobalOptimizerPass(PM); + break; + case GVN: + if (objc != 3) goto bad3arg; + LLVMAddGVNPass(PM); + break; + case IND_VAR_SIMLIFY: + if (objc != 3) goto bad3arg; + LLVMAddIndVarSimplifyPass(PM); + break; + case INST_COMBINE: + if (objc != 3) goto bad3arg; + LLVMAddInstructionCombiningPass(PM); + break; + case INTERNALIZE: { + if (objc != 4) { + Tcl_WrongNumArgs(interp, 3, objv, "allButMain"); + return TCL_ERROR; + } + int flag; + if (Tcl_GetBooleanFromObj(interp, objv[3], &flag)) return TCL_ERROR; + LLVMAddInternalizePass(PM, (unsigned) flag); + break; + } + case IP_CONST_PROP: + if (objc != 3) goto bad3arg; + LLVMAddIPConstantPropagationPass(PM); + break; + case IP_SCCP: + if (objc != 3) goto bad3arg; + LLVMAddIPSCCPPass(PM); + break; + case JUMP_THREAD: + if (objc != 3) goto bad3arg; + LLVMAddJumpThreadingPass(PM); + break; + case LICM: + if (objc != 3) goto bad3arg; + LLVMAddLICMPass(PM); + break; + case LOOP_DEL: + if (objc != 3) goto bad3arg; + LLVMAddLoopDeletionPass(PM); + break; + case LOOP_IDIOM: + if (objc != 3) goto bad3arg; + LLVMAddLoopIdiomPass(PM); + break; + case LOOP_ROT: + if (objc != 3) goto bad3arg; + LLVMAddLoopRotatePass(PM); + break; + case LOOP_UNROLL: + if (objc != 3) goto bad3arg; + LLVMAddLoopUnrollPass(PM); + break; + case LOOP_UNSWITCH: + if (objc != 3) goto bad3arg; + LLVMAddLoopUnswitchPass(PM); + break; + case LOOP_VECT: + if (objc != 3) goto bad3arg; + LLVMAddLoopVectorizePass(PM); + break; + case LOWER_EXPECT: + if (objc != 3) goto bad3arg; + LLVMAddLowerExpectIntrinsicPass(PM); + break; + case MEMCPY_OPT: + if (objc != 3) goto bad3arg; + LLVMAddMemCpyOptPass(PM); + break; + case PROMOTE_MEMORY: + if (objc != 3) goto bad3arg; + LLVMAddPromoteMemoryToRegisterPass(PM); + break; + case PRUNE_EH: + if (objc != 3) goto bad3arg; + LLVMAddPruneEHPass(PM); + break; + case REASSOCIATE: + if (objc != 3) goto bad3arg; + LLVMAddReassociatePass(PM); + break; + case SCALAR_REPL_AGGR: + if (objc != 3) goto bad3arg; + LLVMAddScalarReplAggregatesPass(PM); + break; + case SCALAR_REPL_AGGR_SSA: + if (objc != 3) goto bad3arg; + LLVMAddScalarReplAggregatesPassSSA(PM); + break; + case SCALAR_REPL_AGGR_THRESHOLD: { + if (objc != 4) { + Tcl_WrongNumArgs(interp, 3, objv, "threshold"); + return TCL_ERROR; + } + int threshold; + if (Tcl_GetIntFromObj(interp, objv[3], &threshold)) return TCL_ERROR; + LLVMAddScalarReplAggregatesPassWithThreshold(PM, threshold); + break; + } + case SCCP: + if (objc != 3) goto bad3arg; + LLVMAddSCCPPass(PM); + break; + case SIMPLIFY_LIB_CALLS: + if (objc != 3) goto bad3arg; + LLVMAddSimplifyLibCallsPass(PM); + break; + case STRIP_DEAD_PROTO: + if (objc != 3) goto bad3arg; + LLVMAddStripDeadPrototypesPass(PM); + break; + case STRIP_SYM: + if (objc != 3) goto bad3arg; + LLVMAddStripSymbolsPass(PM); + break; + case TAILCALL_ELIM: + if (objc != 3) goto bad3arg; + LLVMAddTailCallEliminationPass(PM); + break; + case TBAA: + if (objc != 3) goto bad3arg; + LLVMAddTypeBasedAliasAnalysisPass(PM); + break; + case VERIFIER: + if (objc != 3) goto bad3arg; + LLVMAddVerifierPass(PM); + break; + } + + return TCL_OK; + bad3arg: + Tcl_WrongNumArgs(interp, 3, objv, nullptr); + return TCL_ERROR; +} + /* * ------------------------------------------------------------------------ * @@ -656,6 +906,72 @@ GetBasicBlocks( return TCL_OK; } +/* + * ------------------------------------------------------------------------ + * + * GetGlobals -- + * Implements a Tcl command for getting the globals of a module. + * + * ------------------------------------------------------------------------ + */ + +static int +GetGlobals( + ClientData clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *const objv[]) +{ + if (objc != 2) { + Tcl_WrongNumArgs(interp, 1, objv, "Module"); + return TCL_ERROR; + } + + llvm::Module *mod; + if (GetModuleFromObj(interp, objv[1], mod) != TCL_OK) + return TCL_ERROR; + + Tcl_Obj *rtl = Tcl_NewListObj(0, NULL); + for (auto &g : mod->globals()) + Tcl_ListObjAppendElement(interp, rtl, NewObj(&g)); + + Tcl_SetObjResult(interp, rtl); + return TCL_OK; +} + +/* + * ------------------------------------------------------------------------ + * + * GetFunctions -- + * Implements a Tcl command for getting the functions of a module. + * + * ------------------------------------------------------------------------ + */ + +static int +GetFunctions( + ClientData clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *const objv[]) +{ + if (objc != 2) { + Tcl_WrongNumArgs(interp, 1, objv, "Module"); + return TCL_ERROR; + } + + llvm::Module *mod; + if (GetModuleFromObj(interp, objv[1], mod) != TCL_OK) + return TCL_ERROR; + + Tcl_Obj *rtl = Tcl_NewListObj(0, NULL); + for (auto &f : mod->functions()) + Tcl_ListObjAppendElement(interp, rtl, NewObj(&f)); + + Tcl_SetObjResult(interp, rtl); + return TCL_OK; +} + /* * ------------------------------------------------------------------------ * @@ -672,19 +988,19 @@ NamedStructType( int objc, Tcl_Obj *const objv[]) { - if (objc != 4) { - Tcl_WrongNumArgs(interp, 1, objv, "Name ElementTypes Packed"); + if (objc < 3 || objc > 4) { + Tcl_WrongNumArgs(interp, 1, objv, "Name ElementTypes ?Packed?"); return TCL_ERROR; } std::string name = Tcl_GetString(objv[1]); - int numTypes = 0; + unsigned numTypes = 0; LLVMTypeRef *types = 0; if (GetListOfLLVMTypeRefFromObj(interp, objv[2], types, numTypes) != TCL_OK) return TCL_ERROR; int packed = 0; - if (Tcl_GetIntFromObj(interp, objv[3], &packed) != TCL_OK) + if (objc > 3 && Tcl_GetBooleanFromObj(interp, objv[3], &packed) != TCL_OK) return TCL_ERROR; llvm::Type *rt; @@ -692,9 +1008,8 @@ NamedStructType( rt = llvm::StructType::create(*llvm::unwrap(LLVMGetGlobalContext()), name); } else { - llvm::ArrayRef elements( - llvm::unwrap(types), unsigned(numTypes)); - rt = llvm::StructType::create(elements, name, packed); + llvm::ArrayRef elements(llvm::unwrap(types), numTypes); + rt = llvm::StructType::create(elements, name, bool(packed)); } Tcl_SetObjResult(interp, NewObj(rt)); @@ -805,8 +1120,11 @@ DefineCommand( const char *tclName, Tcl_ObjCmdProc *cName) { - // Theoretically, this can blow up. It only actually does this if - // the interpreter is being deleted. + /* + * Theoretically, this can blow up. It only actually does this if + * the interpreter is being deleted. + */ + Tcl_CreateObjCommand(interp, tclName, cName, nullptr, nullptr); } @@ -863,6 +1181,8 @@ DLLEXPORT int Llvmtcl_Init(Tcl_Interp *interp) LLVMObjCmd("llvmtcl::GetParams", GetParams); LLVMObjCmd("llvmtcl::GetStructElementTypes", GetStructElementTypes); LLVMObjCmd("llvmtcl::GetBasicBlocks", GetBasicBlocks); + LLVMObjCmd("llvmtcl::GetGlobals", GetGlobals); + LLVMObjCmd("llvmtcl::GetFunctions", GetFunctions); LLVMObjCmd("llvmtcl::CallInitialisePackageFunction", CallInitialisePackageFunction); LLVMObjCmd("llvmtcl::GetIntrinsicDefinition", GetIntrinsicDefinition); @@ -878,6 +1198,7 @@ DLLEXPORT int Llvmtcl_Init(Tcl_Interp *interp) LLVMObjCmd("llvmtcl::MakeTargetMachine", MakeTargetMachine); LLVMObjCmd("llvmtcl::WriteModuleMachineCodeToFile", WriteModuleMachineCodeToFile); + LLVMObjCmd("llvmtcl::AddPass", LLVMTclAddPass); // Debugging info support LLVMObjCmd("llvmtcl::DebugInfo::BuildDbgValue", BuildDbgValue); LLVMObjCmd("llvmtcl::DebugInfo::CreateBuilder", CreateDebugBuilder); diff --git a/llvmtcl-gen.inp b/llvmtcl-gen.inp index 695f962..2254e05 100644 --- a/llvmtcl-gen.inp +++ b/llvmtcl-gen.inp @@ -1,11 +1,12 @@ +// -*- c++ -*- typedef LLVMBuilderRef typedef LLVMTypeRef typedef LLVMValueRef typedef LLVMBasicBlockRef -typedef LLVMContextRef +//typedef LLVMContextRef //typedef LLVMTypeHandleRef typedef LLVMModuleRef -typedef LLVMUseRef +//typedef LLVMUseRef typedef LLVMExecutionEngineRef typedef LLVMGenericValueRef typedef LLVMPassManagerRef @@ -14,14 +15,12 @@ typedef LLVMTargetDataRef typedef LLVMTargetMachineRef enum {LLVMVoidTypeKind, LLVMHalfTypeKind, LLVMFloatTypeKind, LLVMDoubleTypeKind, LLVMX86_FP80TypeKind, LLVMFP128TypeKind, LLVMPPC_FP128TypeKind, LLVMLabelTypeKind, LLVMIntegerTypeKind, LLVMFunctionTypeKind, LLVMStructTypeKind, LLVMArrayTypeKind, LLVMPointerTypeKind, LLVMVectorTypeKind, LLVMMetadataTypeKind, LLVMX86_MMXTypeKind, LLVMTokenTypeKind} LLVMTypeKind; -//enum {LLVMZExtAttribute, LLVMSExtAttribute, LLVMNoReturnAttribute, LLVMInRegAttribute, LLVMStructRetAttribute, LLVMNoUnwindAttribute, LLVMNoAliasAttribute, LLVMByValAttribute, LLVMNestAttribute, LLVMReadNoneAttribute, LLVMReadOnlyAttribute, LLVMNoInlineAttribute, LLVMAlwaysInlineAttribute, LLVMOptimizeForSizeAttribute, LLVMStackProtectAttribute, LLVMStackProtectReqAttribute, LLVMAlignment, LLVMNoCaptureAttribute, LLVMNoRedZoneAttribute, LLVMNoImplicitFloatAttribute, LLVMNakedAttribute, LLVMInlineHintAttribute, LLVMStackAlignment, LLVMReturnsTwice, LLVMUWTable, LLVMNonLazyBind} LLVMAttribute; enum { LLVMRet, LLVMBr, LLVMSwitch, LLVMIndirectBr, LLVMInvoke, LLVMUnreachable, LLVMAdd, LLVMFAdd, LLVMSub, LLVMFSub, LLVMMul, LLVMFMul, LLVMUDiv, LLVMSDiv, LLVMFDiv, LLVMURem, LLVMSRem, LLVMFRem, LLVMShl, LLVMLShr, LLVMAShr, LLVMAnd, LLVMOr, LLVMXor, LLVMAlloca, LLVMLoad, LLVMStore, LLVMGetElementPtr, LLVMTrunc, LLVMZExt, LLVMSExt, LLVMFPToUI, LLVMFPToSI, LLVMUIToFP, LLVMSIToFP, LLVMFPTrunc, LLVMFPExt, LLVMPtrToInt, LLVMIntToPtr, LLVMBitCast, LLVMAddrSpaceCast, LLVMICmp, LLVMFCmp, LLVMPHI, LLVMCall, LLVMSelect, LLVMVAArg, LLVMExtractElement, LLVMInsertElement, LLVMShuffleVector, LLVMExtractValue, LLVMInsertValue} LLVMOpcode; enum { LLVMIntEQ, LLVMIntNE, LLVMIntUGT, LLVMIntUGE, LLVMIntULT, LLVMIntULE, LLVMIntSGT, LLVMIntSGE, LLVMIntSLT, LLVMIntSLE} LLVMIntPredicate; enum {LLVMRealPredicateFalse,LLVMRealOEQ,LLVMRealOGT,LLVMRealOGE,LLVMRealOLT,LLVMRealOLE,LLVMRealONE,LLVMRealORD,LLVMRealUNO,LLVMRealUEQ,LLVMRealUGT,LLVMRealUGE,LLVMRealULT,LLVMRealULE,LLVMRealUNE,LLVMRealPredicateTrue} LLVMRealPredicate; enum {LLVMExternalLinkage, LLVMAvailableExternallyLinkage, LLVMLinkOnceAnyLinkage, LLVMLinkOnceODRLinkage, LLVMLinkOnceODRAutoHideLinkage, LLVMWeakAnyLinkage, LLVMWeakODRLinkage, LLVMAppendingLinkage, LLVMInternalLinkage, LLVMPrivateLinkage, LLVMDLLImportLinkage, LLVMDLLExportLinkage, LLVMExternalWeakLinkage, LLVMGhostLinkage, LLVMCommonLinkage, LLVMLinkerPrivateLinkage, LLVMLinkerPrivateWeakLinkage} LLVMLinkage; enum { LLVMDefaultVisibility, LLVMHiddenVisibility, LLVMProtectedVisibility} LLVMVisibility; enum { LLVMCCallConv, LLVMFastCallConv, LLVMColdCallConv, LLVMWebKitJSCallConv, LLVMAnyRegCallConv, LLVMX86StdcallCallConv, LLVMX86FastcallCallConv} LLVMCallConv; -enum { LLVMAbortProcessAction, LLVMPrintMessageAction, LLVMReturnStatusAction } LLVMVerifierFailureAction; enum { LLVMAtomicRMWBinOpXchg, LLVMAtomicRMWBinOpAdd, LLVMAtomicRMWBinOpSub, LLVMAtomicRMWBinOpAnd, LLVMAtomicRMWBinOpOr, LLVMAtomicRMWBinOpXor, LLVMAtomicRMWBinOpMax, LLVMAtomicRMWBinOpMin, LLVMAtomicRMWBinOpUMax, LLVMAtomicRMWBinOpUMin} LLVMAtomicRMWBinOp; enum { LLVMAtomicOrderingNotAtomic, LLVMAtomicOrderingUnordered, LLVMAtomicOrderingMonotonic, LLVMAtomicOrderingAcquire, LLVMAtomicOrderingRelease, LLVMAtomicOrderingAcquireRelease, LLVMAtomicOrderingSequentiallyConsistent } LLVMAtomicOrdering; @@ -30,12 +29,15 @@ void LLVMLinkInMCJIT(void); void LLVMLinkInInterpreter(void); LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID); -LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID, LLVMContextRef C); -rename LLVMDisposeModule LLVMDisposeModuleTcl +rename LLVMDisposeModule LLVMDisposeModuleTcl; void LLVMDisposeModule(LLVMModuleRef M); -rename LLVMDumpModule LLVMDumpModuleTcl +rename LLVMDumpModule LLVMDumpModuleTcl; std::string LLVMDumpModule(LLVMModuleRef M); + +// ------------------------------------------------------------------- // Builder +// ------------------------------------------------------------------- + LLVMBuilderRef LLVMCreateBuilder(void); void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block, LLVMValueRef Instr); void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr); @@ -44,19 +46,17 @@ LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder); void LLVMClearInsertionPosition(LLVMBuilderRef Builder); void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr); void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr, const char * Name); -rename LLVMDisposeBuilder LLVMDisposeBuilderTcl +rename LLVMDisposeBuilder LLVMDisposeBuilderTcl; void LLVMDisposeBuilder(LLVMBuilderRef Builder); void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L); LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder); void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst); LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef); LLVMValueRef LLVMBuildRet(LLVMBuilderRef, LLVMValueRef V); -// (added manualy) LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef, LLVMValueRef * RetVals, unsigned N); LLVMValueRef LLVMBuildBr(LLVMBuilderRef, LLVMBasicBlockRef Dest); LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef, LLVMValueRef If, LLVMBasicBlockRef Then, LLVMBasicBlockRef Else); LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef, LLVMValueRef V, LLVMBasicBlockRef Else, unsigned NumCases); LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr, unsigned NumDests); -// (added manualy) LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef, LLVMValueRef Fn, LLVMValueRef * Args, unsigned NumArgs, LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch, const char * Name); LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef); void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal, LLVMBasicBlockRef Dest); void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest); @@ -141,15 +141,11 @@ LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef, LLVMValueRef Val, const char * N LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS, const char * Name); LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef, LLVMAtomicRMWBinOp, LLVMValueRef, LLVMValueRef, LLVMAtomicOrdering, LLVMBool); +// ------------------------------------------------------------------- // Types +// ------------------------------------------------------------------- + LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty); -LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty); -LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C); -LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C); -LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C); -LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C); -LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C); -LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits); LLVMTypeRef LLVMInt1Type(void); LLVMTypeRef LLVMInt8Type(void); LLVMTypeRef LLVMInt16Type(void); @@ -157,11 +153,6 @@ LLVMTypeRef LLVMInt32Type(void); LLVMTypeRef LLVMInt64Type(void); LLVMTypeRef LLVMIntType(unsigned NumBits); unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy); -LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C); -LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C); -LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C); -LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C); -LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C); LLVMTypeRef LLVMFloatType(void); LLVMTypeRef LLVMDoubleType(void); LLVMTypeRef LLVMX86FP80Type(void); @@ -171,12 +162,9 @@ LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType, LLVMTypeRef *ParamTypes, un LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy); LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy); unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy); -// (added manually) void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest); -LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes, unsigned ElementCount, LLVMBool Packed); LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes, unsigned ElementCount, LLVMBool Packed); void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes, unsigned ElementCount, LLVMBool Packed); unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy); -// (added manually) void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest); LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy); LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount); LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace); @@ -185,39 +173,37 @@ LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty); unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy); unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy); unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy); -LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C); -LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C); LLVMTypeRef LLVMVoidType(void); LLVMTypeRef LLVMLabelType(void); + +// ------------------------------------------------------------------- // Functions +// ------------------------------------------------------------------- + LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name, LLVMTypeRef FunctionTy); LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name); -LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M); -LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M); -LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn); -LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn); -rename LLVMDeleteFunction LLVMDeleteFunctionTcl +rename LLVMDeleteFunction LLVMDeleteFunctionTcl; void LLVMDeleteFunction(LLVMValueRef Fn); unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn); void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC); std::string LLVMGetGC(LLVMValueRef Fn); void LLVMSetGC(LLVMValueRef Fn, const char *Name); -//void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA); -//LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn); -//void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA); + +// ------------------------------------------------------------------- // Values +// ------------------------------------------------------------------- + LLVMTypeRef LLVMTypeOf(LLVMValueRef Val); std::string LLVMGetValueName(LLVMValueRef Val); void LLVMSetValueName(LLVMValueRef Val, const char *Name); -void LLVMDumpValue(LLVMValueRef Val); void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal); int LLVMHasMetadata(LLVMValueRef Val); LLVMValueRef LLVMGetMetadata(LLVMValueRef Val, unsigned KindID); void LLVMSetMetadata(LLVMValueRef Val, unsigned KindID, LLVMValueRef Node); -LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val); -LLVMUseRef LLVMGetNextUse(LLVMUseRef U); -LLVMValueRef LLVMGetUser(LLVMUseRef U); -LLVMValueRef LLVMGetUsedValue(LLVMUseRef U); +//LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val); +//LLVMUseRef LLVMGetNextUse(LLVMUseRef U); +//LLVMValueRef LLVMGetUser(LLVMUseRef U); +//LLVMValueRef LLVMGetUsedValue(LLVMUseRef U); LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index); LLVMValueRef LLVMConstNull(LLVMTypeRef Ty); LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty); @@ -226,9 +212,7 @@ LLVMBool LLVMIsConstant(LLVMValueRef Val); LLVMBool LLVMIsNull(LLVMValueRef Val); LLVMBool LLVMIsUndef(LLVMValueRef Val); LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty); -LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str, unsigned SLen); LLVMValueRef LLVMMDString(const char *Str, unsigned SLen); -LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals, unsigned Count); LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count); LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N, LLVMBool SignExtend); LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char *Text, uint8_t Radix); @@ -238,8 +222,6 @@ LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text); LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char *Text, unsigned SLen); unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal); long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal); -LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str, unsigned Length, LLVMBool DontNullTerminate); -LLVMValueRef LLVMConstStructInContext(LLVMContextRef C, LLVMValueRef *ConstantVals, unsigned Count, LLVMBool Packed); LLVMValueRef LLVMConstString(const char *Str, unsigned Length, LLVMBool DontNullTerminate); LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy, LLVMValueRef *ConstantVals, unsigned Length); LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count, LLVMBool Packed); @@ -306,7 +288,7 @@ LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant, LLVMValueRef El LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant, LLVMValueRef VectorBConstant, LLVMValueRef MaskConstant); //LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList, unsigned NumIdx); //LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant, LLVMValueRef ElementValueConstant, unsigned *IdxList, unsigned NumIdx); -LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString, const char *Constraints, LLVMBool HasSideEffects, LLVMBool IsAlignStack); +//LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString, const char *Constraints, LLVMBool HasSideEffects, LLVMBool IsAlignStack); LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB); LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global); LLVMBool LLVMIsDeclaration(LLVMValueRef Global); @@ -319,66 +301,41 @@ void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz); unsigned LLVMGetAlignment(LLVMValueRef Global); void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes); LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name); -LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name, unsigned AddressSpace); LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name); -LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M); -LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M); -LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar); -LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar); void LLVMDeleteGlobal(LLVMValueRef GlobalVar); LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar); void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal); -LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar); -void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal); LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar); void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant); LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee, const char *Name); unsigned LLVMCountParams(LLVMValueRef Fn); -// (added manually) void LLVMGetParams(LLVMValueRef Fn, LLVMValueRef *Params); LLVMValueRef LLVMGetParam(LLVMValueRef Fn, unsigned Index); LLVMValueRef LLVMGetParamParent(LLVMValueRef Inst); -LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn); -LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn); -LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg); -LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg); -//void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA); -//void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA); -//LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg); void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align); LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB); LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val); LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val); LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB); unsigned LLVMCountBasicBlocks(LLVMValueRef Fn); -// (added manually) void LLVMGetBasicBlocks(LLVMValueRef Fn, LLVMBasicBlockRef *BasicBlocks); -LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn); -LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn); -LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB); -LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB); LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn); -LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C, LLVMValueRef Fn, const char *Name); -LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C, LLVMBasicBlockRef BB, const char *Name); LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef Fn, const char *Name); LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBB, const char *Name); -rename LLVMDeleteBasicBlock LLVMDeleteBasicBlockTcl +rename LLVMDeleteBasicBlock LLVMDeleteBasicBlockTcl; void LLVMDeleteBasicBlock(LLVMBasicBlockRef BB); LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst); -LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB); -LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB); -LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst); -LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst); void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC); unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr); -//void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index, LLVMAttribute); -//void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index, LLVMAttribute); void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, unsigned align); LLVMBool LLVMIsTailCall(LLVMValueRef CallInst); void LLVMSetTailCall(LLVMValueRef CallInst, LLVMBool IsTailCall); -// (added manually) void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues, LLVMBasicBlockRef *IncomingBlocks, unsigned Count); unsigned LLVMCountIncoming(LLVMValueRef PhiNode); LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index); LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index); +// ------------------------------------------------------------------- +// Execution and optimisation +// ------------------------------------------------------------------- + LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE, LLVMModuleRef M, char **OutError); LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp, LLVMModuleRef M, char **OutError); LLVMBool LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT, LLVMModuleRef M, unsigned OptLevel, char **OutError); @@ -397,55 +354,10 @@ LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M); LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM); LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F); LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM); -rename LLVMDisposePassManager LLVMDisposePassManagerTcl +rename LLVMDisposePassManager LLVMDisposePassManagerTcl; void LLVMDisposePassManager(LLVMPassManagerRef PM); int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path); -void LLVMAddAggressiveDCEPass(LLVMPassManagerRef PM); -void LLVMAddCFGSimplificationPass(LLVMPassManagerRef PM); -void LLVMAddDeadStoreEliminationPass(LLVMPassManagerRef PM); -void LLVMAddGVNPass(LLVMPassManagerRef PM); -void LLVMAddIndVarSimplifyPass(LLVMPassManagerRef PM); -void LLVMAddInstructionCombiningPass(LLVMPassManagerRef PM); -void LLVMAddJumpThreadingPass(LLVMPassManagerRef PM); -void LLVMAddLICMPass(LLVMPassManagerRef PM); -void LLVMAddLoopDeletionPass(LLVMPassManagerRef PM); -void LLVMAddLoopIdiomPass(LLVMPassManagerRef); -void LLVMAddLoopRotatePass(LLVMPassManagerRef PM); -void LLVMAddLoopUnrollPass(LLVMPassManagerRef PM); -void LLVMAddLoopUnswitchPass(LLVMPassManagerRef PM); -void LLVMAddMemCpyOptPass(LLVMPassManagerRef PM); -void LLVMAddPromoteMemoryToRegisterPass(LLVMPassManagerRef PM); -void LLVMAddReassociatePass(LLVMPassManagerRef PM); -void LLVMAddSCCPPass(LLVMPassManagerRef PM); -void LLVMAddScalarReplAggregatesPass(LLVMPassManagerRef PM); -void LLVMAddScalarReplAggregatesPassSSA(LLVMPassManagerRef PM); -void LLVMAddScalarReplAggregatesPassWithThreshold(LLVMPassManagerRef PM, int); -void LLVMAddSimplifyLibCallsPass(LLVMPassManagerRef PM); -void LLVMAddTailCallEliminationPass(LLVMPassManagerRef PM); -void LLVMAddConstantPropagationPass(LLVMPassManagerRef PM); -void LLVMAddDemoteMemoryToRegisterPass(LLVMPassManagerRef PM); -void LLVMAddVerifierPass(LLVMPassManagerRef PM); -void LLVMAddCorrelatedValuePropagationPass(LLVMPassManagerRef PM); -void LLVMAddEarlyCSEPass(LLVMPassManagerRef PM); -void LLVMAddLowerExpectIntrinsicPass(LLVMPassManagerRef PM); -void LLVMAddTypeBasedAliasAnalysisPass(LLVMPassManagerRef PM); -void LLVMAddBasicAliasAnalysisPass(LLVMPassManagerRef PM); -void LLVMAddArgumentPromotionPass(LLVMPassManagerRef PM); -void LLVMAddConstantMergePass(LLVMPassManagerRef PM); -void LLVMAddDeadArgEliminationPass(LLVMPassManagerRef PM); -void LLVMAddFunctionAttrsPass(LLVMPassManagerRef PM); -void LLVMAddFunctionInliningPass(LLVMPassManagerRef PM); -void LLVMAddAlwaysInlinerPass(LLVMPassManagerRef PM); -void LLVMAddGlobalDCEPass(LLVMPassManagerRef PM); -void LLVMAddGlobalOptimizerPass(LLVMPassManagerRef PM); -void LLVMAddIPConstantPropagationPass(LLVMPassManagerRef PM); -void LLVMAddPruneEHPass(LLVMPassManagerRef PM); -void LLVMAddIPSCCPPass(LLVMPassManagerRef PM); -void LLVMAddInternalizePass(LLVMPassManagerRef, unsigned AllButMain); -void LLVMAddStripDeadPrototypesPass(LLVMPassManagerRef PM); -void LLVMAddStripSymbolsPass(LLVMPassManagerRef PM); -void LLVMAddLoopVectorizePass(LLVMPassManagerRef); std::string LLVMGetDataLayout(LLVMModuleRef M); void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple); @@ -453,12 +365,6 @@ void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple); std::string LLVMGetTarget(LLVMModuleRef M); void LLVMSetTarget(LLVMModuleRef M, const char *Triple); -//LLVMBool LLVMVerifyModule(LLVMModuleRef M, LLVMVerifierFailureAction Action, char **OutMessage); -//LLVMBool LLVMVerifyFunction(LLVMValueRef Fn, LLVMVerifierFailureAction Action); - -void LLVMViewFunctionCFG(LLVMValueRef Fn); -void LLVMViewFunctionCFGOnly(LLVMValueRef Fn); - LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate(void); void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef); void LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef, unsigned); @@ -473,15 +379,13 @@ void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef, LLV void LLVMAddCoroutinePassesToExtensionPoints(LLVMPassManagerBuilderRef); -void LLVMEnablePrettyStackTrace(void); - -rename LLVMPrintModuleToString LLVMPrintModuleToStringTcl +rename LLVMPrintModuleToString LLVMPrintModuleToStringTcl; std::string LLVMPrintModuleToString(LLVMModuleRef M); -rename LLVMPrintTypeToString LLVMPrintTypeToStringTcl +rename LLVMPrintTypeToString LLVMPrintTypeToStringTcl; std::string LLVMPrintTypeToString(LLVMTypeRef M); -rename LLVMPrintValueToString LLVMPrintValueToStringTcl +rename LLVMPrintValueToString LLVMPrintValueToStringTcl; std::string LLVMPrintValueToString(LLVMValueRef M); LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef); @@ -493,4 +397,4 @@ LLVMTargetMachineRef LLVMGetExecutionEngineTargetMachine(LLVMExecutionEngineRef) std::string LLVMGetTargetMachineTriple(LLVMTargetMachineRef); LLVMTargetDataRef LLVMCreateTargetDataLayout(LLVMTargetMachineRef); void LLVMAddAnalysisPasses(LLVMTargetMachineRef, LLVMPassManagerRef); -std::string LLVMCopyStringRepOfTargetData(LLVMTargetDataRef); \ No newline at end of file +std::string LLVMCopyStringRepOfTargetData(LLVMTargetDataRef); diff --git a/llvmtcl-gen.tcl b/llvmtcl-gen.tcl index 972fae2..0e96019 100644 --- a/llvmtcl-gen.tcl +++ b/llvmtcl-gen.tcl @@ -1,4 +1,64 @@ -package require Tcl 8.5 +package require Tcl 8.6 + +proc val_as_obj {type value} { + switch -exact -- $type { + "LLVMBuilderRef" - + "LLVMContextRef" - + "LLVMBasicBlockRef" - + "LLVMValueRef" - + "LLVMTargetDataRef" - + "LLVMTypeKind" - + "LLVMVisibility" - + "LLVMCallConv" - + "LLVMOpcode" - + "LLVMLinkage" - + "LLVMVerifierFailureAction" - + "LLVMIntPredicate" - + "LLVMRealPredicate" - + "LLVMAttribute" - + "LLVMTypeHandleRef" - + "LLVMTypeRef" - + "LLVMPassManagerRef" - + "LLVMPassManagerBuilderRef" - + "LLVMTargetDataRef" - + "LLVMTargetMachineRef" - + "LLVMUseRef" - + "LLVMGenericValueRef" - + "LLVMModuleRef" { + return "Set${type}AsObj(interp, $value)" + } + "LLVMExecutionEngineRef *" { + return "Set[string trim $type { *}]AsObj(interp, $value)" + } + "std::string" { + return "Tcl_NewStringObj($value.c_str(), -1)" + } + "const char *" - + "char **" { + return "Tcl_NewStringObj($value, -1)" + } + "LLVMBool" - + "bool" - + "int" - + "unsigned" { + return "Tcl_NewIntObj($value)" + } + "long long" - + "unsigned long long" { + return "Tcl_NewWideIntObj((Tcl_WideInt)$value)" + } + "double" { + return "Tcl_NewDoubleObj($value)" + } + "void" { + } + default { + upvar 1 l definition + return -code error \ + "Unknown return type '$type' for '$value' in '$definition'" + } + } +} proc gen_api_call {cf of l} { lassign [split $l "("] rtnm fargs @@ -50,10 +110,9 @@ proc gen_api_call {cf of l} { } incr n 2 } - puts $cf " if (objc != $an) \{" - puts -nonewline $cf " Tcl_WrongNumArgs(interp, 1, objv, \"" set n 1 set skip_next 0 + set wnamsg "" foreach {fargtype fargname} $fargsl { if {[string match "Out*" $fargname]} { # Skip this @@ -64,21 +123,22 @@ proc gen_api_call {cf of l} { error "Unknown type '$fargtype' in '$l'" } if {[string length $fargname]} { - puts -nonewline $cf "$fargname " + append wnamsg "$fargname " } else { - puts -nonewline $cf "$fargtype " + append wnamsg "$fargtype " } set skip_next 1 } elseif {$fargname ne ""} { - puts -nonewline $cf "$fargname " + append wnamsg "$fargname " } else { - puts -nonewline $cf "$fargtype " + append wnamsg "$fargtype " } incr n } - puts $cf "\");" - puts $cf " return TCL_ERROR;" - puts $cf " \}" + puts $cf " if (objc != $an) { + Tcl_WrongNumArgs(interp, 1, objv, \"[string trim $wnamsg]\"); + return TCL_ERROR; + }" # Read arguments set n 1 set on 1 @@ -90,10 +150,10 @@ proc gen_api_call {cf of l} { lappend out_args arg$n $fargtype switch -exact -- $fargtype { "LLVMExecutionEngineRef *" { - puts $cf " [string trim $fargtype { *}] arg$n = 0; // output argument" + puts $cf " [string trim $fargtype { *}] arg$n = nullptr; // output argument" } "char **" { - puts $cf " char* arg$n = 0;" + puts $cf " char* arg$n = nullptr;" } default { error "Unknown type '$fargtype' in '$l'" @@ -118,7 +178,7 @@ proc gen_api_call {cf of l} { "LLVMTargetDataRef" - "LLVMTargetMachineRef" - "LLVMModuleRef" { - puts $cf " $fargtype arg$n = 0;" + puts $cf " $fargtype arg$n = nullptr;" puts $cf " if (Get${fargtype}FromObj(interp, objv\[$on\], arg$n) != TCL_OK)" puts $cf " return TCL_ERROR;" } @@ -140,20 +200,19 @@ proc gen_api_call {cf of l} { "LLVMGenericValueRef *" - "LLVMValueRef *" - "LLVMTypeRef *" { - if {[lindex $fargsl [expr {$n*2}]] ne "unsigned"} { - error "Unknown type '$fargtype' in '$l'" + if {[lindex $fargsl [expr {$n * 2}]] ne "unsigned"} { + error "Unknown type '$fargtype' in '$l': $n, $fargsl" } - puts $cf " int iarg[expr {$n+1}] = 0;" - puts $cf " $fargtype arg$n = 0;" - puts $cf " if (GetListOf[string trim $fargtype { *}]FromObj(interp, objv\[$on\], arg$n, iarg[expr {$n+1}]) != TCL_OK)" + puts $cf " unsigned arg[expr {$n+1}] = 0;" + puts $cf " $fargtype arg$n = nullptr;" + puts $cf " if (GetListOf[string trim $fargtype { *}]FromObj(interp, objv\[$on\], arg$n, arg[expr {$n+1}]) != TCL_OK)" puts $cf " return TCL_ERROR;" - puts $cf " unsigned arg[expr {$n+1}] = (unsigned)iarg[expr {$n+1}];" set skip_next 1 - lappend delete_args $n + lappend delete_args arg$n } "std::string" - "const char *" { - puts $cf " std::string arg$n = Tcl_GetStringFromObj(objv\[$on\], 0);" + puts $cf " std::string arg$n = Tcl_GetString(objv\[$on\]);" } "LLVMBool" - "bool" - @@ -169,17 +228,12 @@ proc gen_api_call {cf of l} { puts $cf " return TCL_ERROR;" puts $cf " $fargtype arg$n = ($fargtype)iarg$n;" } - "long long" { - puts $cf " Tcl_WideInt iarg$n = 0;" - puts $cf " if (Tcl_GetWideIntFromObj(interp, objv\[$on\], &arg$n) != TCL_OK)" - puts $cf " return TCL_ERROR;" - puts $cf " long long arg$n = (long long)iarg$n;" - } + "long long" - "unsigned long long" { puts $cf " Tcl_WideInt iarg$n = 0;" puts $cf " if (Tcl_GetWideIntFromObj(interp, objv\[$on\], &iarg$n) != TCL_OK)" puts $cf " return TCL_ERROR;" - puts $cf " unsigned long long arg$n = (unsigned long long)iarg$n;" + puts $cf " $fargtype arg$n = ($fargtype)iarg$n;" } "double" { puts $cf " double arg$n = 0;" @@ -196,53 +250,16 @@ proc gen_api_call {cf of l} { } incr n } - # Variable for return value puts -nonewline $cf " " - switch -exact -- $rt { - "LLVMBuilderRef" - - "LLVMBasicBlockRef" - - "LLVMValueRef" - - "LLVMTargetDataRef" - - "LLVMContextRef" - - "LLVMTypeKind" - - "LLVMVisibility" - - "LLVMCallConv" - - "LLVMOpcode" - - "LLVMLinkage" - - "LLVMVerifierFailureAction" - - "LLVMIntPredicate" - - "LLVMRealPredicate" - - "LLVMAttribute" - - "LLVMTypeHandleRef" - - "LLVMTypeRef" - - "LLVMBool" - - "bool" - - "LLVMModuleRef" - - "LLVMGenericValueRef" - - "LLVMPassManagerRef" - - "LLVMPassManagerBuilderRef" - - "LLVMTargetDataRef" - - "LLVMTargetMachineRef" - - "LLVMUseRef" - - "int" - - "long long" - - "double" - - "unsigned" - - "unsigned long long" - - "std::string" { - puts -nonewline $cf "$rt rt = " - } - "void" { - } - default { - error "Unknown return type '$rt' in '$l'" - } + # Variable for return value + if {$rt ne "void"} { + puts -nonewline $cf "$rt rt = " } # Call function if {[info exists ::rename($nm)]} { - puts -nonewline $cf "$::rename($nm) (" + puts -nonewline $cf "$::rename($nm)(" } else { - puts -nonewline $cf "$nm (" + puts -nonewline $cf "${nm}(" } set n 1 foreach {fargtype fargname} $fargsl { @@ -250,24 +267,11 @@ proc gen_api_call {cf of l} { puts -nonewline $cf "," } if {[string match "Out*" $fargname]} { - switch -exact -- $fargtype { - "char **" - - "LLVMExecutionEngineRef *" { - puts -nonewline $cf "&arg$n" - } - default { - error "Unknown type '$fargtype' in '$l'" - } - } - } else { - switch -exact -- $fargtype { - "const char *" { - puts -nonewline $cf "arg$n.c_str()" - } - default { - puts -nonewline $cf "arg$n" - } - } + puts -nonewline $cf "&" + } + puts -nonewline $cf "arg$n" + if {$fargtype eq "const char *"} { + puts -nonewline $cf ".c_str()" } incr n } @@ -275,112 +279,23 @@ proc gen_api_call {cf of l} { # Return result if {[llength $out_args]} { puts $cf " Tcl_Obj* rtl = Tcl_NewListObj(0, NULL);" - foreach {rnm rtp} [linsert $out_args 0 rt $rt] { - switch -exact -- $rtp { - "LLVMBuilderRef" - - "LLVMContextRef" - - "LLVMBasicBlockRef" - - "LLVMValueRef" - - "LLVMTargetDataRef" - - "LLVMTypeKind" - - "LLVMVisibility" - - "LLVMCallConv" - - "LLVMOpcode" - - "LLVMLinkage" - - "LLVMVerifierFailureAction" - - "LLVMIntPredicate" - - "LLVMRealPredicate" - - "LLVMAttribute" - - "LLVMTypeHandleRef" - - "LLVMTypeRef" - - "LLVMPassManagerRef" - - "LLVMPassManagerBuilderRef" - - "LLVMTargetDatRef" - - "LLVMTargetMachineRef" - - "LLVMUseRef" - - "LLVMGenericValueRef" - - "LLVMModuleRef" { - puts $cf " Tcl_ListObjAppendElement(interp, rtl, Set${rtp}AsObj(interp, $rnm));" - } - "LLVMExecutionEngineRef *" { - puts $cf " Tcl_ListObjAppendElement(interp, rtl, Set[string trim $rtp { *}]AsObj(interp, $rnm));" - } - "char **" { - puts $cf " Tcl_ListObjAppendElement(interp, rtl, Tcl_NewStringObj($rnm, -1));" - } - "LLVMBool" - - "bool" - - "int" - - "unsigned" { - puts $cf " Tcl_ListObjAppendElement(interp, rtl, Tcl_NewIntObj($rnm));" - } - "long long" - - "unsigned long long" { - puts $cf " Tcl_WideInt w$rnm = (Tcl_WideInt)$rnm;" - puts $cf " Tcl_ListObjAppendElement(interp, rtl, Tcl_NewWideIntObj(w$rnm));" - } - "double" { - puts $cf " Tcl_ListObjAppendElement(interp, rtl, Tcl_NewDoubleObj($rnm));" - } - "void" { - } - default { - error "Unknown return type '$rtp' for '$rnm' in '$l'" - } + set oa [linsert $out_args 0 rt $rt] + foreach {rnm rtp} $oa { + if {$rtp ne "void"} { + set rnm [val_as_obj $rtp $rnm] + puts $cf " Tcl_ListObjAppendElement(interp, rtl, $rnm);" } } puts $cf " Tcl_SetObjResult(interp, rtl);" } else { - switch -exact -- $rt { - "LLVMBuilderRef" - - "LLVMContextRef" - - "LLVMBasicBlockRef" - - "LLVMValueRef" - - "LLVMTargetDataRef" - - "LLVMTypeKind" - - "LLVMVisibility" - - "LLVMCallConv" - - "LLVMOpcode" - - "LLVMLinkage" - - "LLVMVerifierFailureAction" - - "LLVMIntPredicate" - - "LLVMRealPredicate" - - "LLVMAttribute" - - "LLVMTypeHandleRef" - - "LLVMTypeRef" - - "LLVMPassManagerRef" - - "LLVMPassManagerBuilderRef" - - "LLVMTargetDataRef" - - "LLVMTargetMachineRef" - - "LLVMUseRef" - - "LLVMGenericValueRef" - - "LLVMModuleRef" { - puts $cf " Tcl_SetObjResult(interp, Set${rt}AsObj(interp, rt));" - } - "LLVMBool" - - "int" - - "bool" - - "unsigned" { - puts $cf " Tcl_SetObjResult(interp, Tcl_NewIntObj(rt));" - } - "long long" - - "unsigned long long" { - puts $cf " Tcl_WideInt wrt = (Tcl_WideInt)rt;" - puts $cf " Tcl_SetObjResult(interp, Tcl_NewWideIntObj(wrt));" - } - "double" { - puts $cf " Tcl_SetObjResult(interp, Tcl_NewDoubleObj(rt));" - } - "std::string" { - puts $cf " Tcl_SetObjResult(interp, Tcl_NewStringObj(rt.c_str(), -1));" - } - "void" { - } - default { - error "Unknown return type '$rt' in '$l'" - } + if {$rt ne "void"} { + set rv [val_as_obj $rt rt] + puts $cf " Tcl_SetObjResult(interp, $rv);" } } + foreach da $delete_args { + puts $cf " delete [] $da;" + } puts $cf " return TCL_OK;" puts $cf "\}" puts $cf "" @@ -392,92 +307,110 @@ proc gen_api_call {cf of l} { puts $of " LLVMObjCmd(\"llvmtcl::$cmdnm\", ${nm}ObjCmd);" } +proc maptokens {vals body} { + upvar 1 pfx_len prefix sfx_len suffix + return [join [lmap val $vals { + set token "\"[string tolower [string range $val $prefix end-$suffix]]\"" + string trim [subst $body] "\n" + }] "\n"] +} proc gen_enum {cf l} { - set idx1 [string first \{ $l] - set idx2 [string last \} $l] - set vals [split [string trim [string range $l [expr {$idx1 + 1}] [expr {$idx2 - 1}]]] ,] - set nm [string trim [string trim [string trim [string range $l [expr {$idx2+1}] end]] \;]] - puts $cf "int Get${nm}FromObj(Tcl_Interp* interp, Tcl_Obj* obj, $nm& e) \{" - puts $cf " static std::map s2e;" - puts $cf " if (s2e.size() == 0) \{" - foreach val $vals { - set val [string trim $val] - puts $cf " s2e\[\"$val\"\] = $val;" + set template { +int Get${nm}FromObj(Tcl_Interp* interp, Tcl_Obj* obj, $nm& e) { + static std::map s2e; + if (s2e.size() == 0) { +[maptokens $vals { + s2e\[$token\] = $val; +}] } - puts $cf " \}" - puts $cf " std::string s = Tcl_GetStringFromObj(obj, 0);" - puts $cf " if (s2e.find(s) == s2e.end()) \{" - puts $cf " std::ostringstream os;" - puts $cf " os << \"expected $nm but got '\" << s << \"'\";" - puts $cf " Tcl_SetObjResult(interp, Tcl_NewStringObj(os.str().c_str(), -1));" - puts $cf " return TCL_ERROR;" - puts $cf " \}" - puts $cf " e = s2e\[s\];" - puts $cf " return TCL_OK;" - puts $cf "\}" - puts $cf "Tcl_Obj* Set${nm}AsObj(Tcl_Interp* interp, $nm e) \{" - puts $cf " static std::map<$nm, std::string> e2s;" - puts $cf " if (e2s.size() == 0) \{" - foreach val $vals { - set val [string trim $val] - puts $cf " e2s\[$val\] = \"$val\";" + std::string s = Tcl_GetStringFromObj(obj, 0); + if (s2e.find(s) == s2e.end()) { + std::ostringstream os; + os << \"expected $nm but got \\\"\" << s << \"\\\"\"; + Tcl_SetObjResult(interp, Tcl_NewStringObj(os.str().c_str(), -1)); + return TCL_ERROR; } - puts $cf " \}" - puts $cf " std::string s;" - puts $cf " if (e2s.find(e) == e2s.end())" - puts $cf " s = \"\";" - puts $cf " else" - puts $cf " s = e2s\[e\];" - puts $cf " return Tcl_NewStringObj(s.c_str(), -1);" - puts $cf "\}" + e = s2e\[s\]; + return TCL_OK; +} +Tcl_Obj* Set${nm}AsObj(Tcl_Interp* interp, $nm e) { + static std::map<$nm, std::string> e2s; + if (e2s.size() == 0) { +[maptokens $vals { + e2s\[$val\] = $token; +}] + } + std::string s; + if (e2s.find(e) == e2s.end()) + s = \"\"; + else + s = e2s\[e\]; + return Tcl_NewStringObj(s.c_str(), -1); +} + } + + regexp {\{(.*)\} (.*);} $l -> vals nm + set vals [lmap val [split $vals ,] {string trim $val}] + set nm [string trim $nm] + set pfx_len [string length [tcl::prefix longest $vals ""]] + set sfx_len [string length [tcl::prefix longest [lmap val $vals { + string reverse $val + }] ""]] + puts $cf [string trim [subst $template]] } proc gen_map {mf l} { - set tp [lindex [string trim $l] end] - puts $mf "static std::map ${tp}_map;" - puts $mf "static std::map<$tp, std::string> ${tp}_refmap;" - puts $mf "int Get${tp}FromObj(Tcl_Interp* interp, Tcl_Obj* obj, $tp& ref) \{" - puts $mf " ref = 0;" - puts $mf " std::string refName = Tcl_GetStringFromObj(obj, 0);" - puts $mf " if (${tp}_map.find(refName) == ${tp}_map.end()) \{" - puts $mf " std::ostringstream os;" - puts $mf " os << \"expected $tp but got '\" << refName << \"'\";" - puts $mf " Tcl_SetObjResult(interp, Tcl_NewStringObj(os.str().c_str(), -1));" - puts $mf " return TCL_ERROR;" - puts $mf " \}" - puts $mf " ref = ${tp}_map\[refName\];" - puts $mf " return TCL_OK;" - puts $mf "\}" - puts $mf "int GetListOf${tp}FromObj(Tcl_Interp* interp, Tcl_Obj* obj, $tp*& refs, int& count) \{" - puts $mf " refs = 0;" - puts $mf " count = 0;" - puts $mf " Tcl_Obj** objs = 0;" - puts $mf " if (Tcl_ListObjGetElements(interp, obj, &count, &objs) != TCL_OK) \{" - puts $mf " std::ostringstream os;" - puts $mf " os << \"expected list of types but got \\\"\" << Tcl_GetStringFromObj(obj, 0) << \"\\\"\";" - puts $mf " Tcl_SetObjResult(interp, Tcl_NewStringObj(os.str().c_str(), -1));" - puts $mf " return TCL_ERROR;" - puts $mf " \}" - puts $mf " if (count == 0)" - puts $mf " return TCL_OK;" - puts $mf " refs = new $tp\[count\];" - puts $mf " for(int i =0; i < count; i++) \{" - puts $mf " if (Get${tp}FromObj(interp, objs\[i\], refs\[i\])) \{" - puts $mf " delete [] refs;" - puts $mf " return TCL_ERROR;" - puts $mf " \}" - puts $mf " \}" - puts $mf " return TCL_OK;" - puts $mf "\}" - puts $mf "Tcl_Obj* Set${tp}AsObj(Tcl_Interp* interp, $tp ref) \{" - puts $mf " if (!ref) return Tcl_NewObj();" - puts $mf " if (${tp}_refmap.find(ref) == ${tp}_refmap.end()) \{" - puts $mf " std::string nm = GetRefName(\"${tp}_\");" - puts $mf " ${tp}_map\[nm\] = ref;" - puts $mf " ${tp}_refmap\[ref\] = nm;" - puts $mf " \}" - puts $mf " return Tcl_NewStringObj(${tp}_refmap\[ref\].c_str(), -1);" - puts $mf "\}" + set template { +static std::map ${tp}_map; +static std::map<$tp, std::string> ${tp}_refmap; +int Get${tp}FromObj(Tcl_Interp* interp, Tcl_Obj* obj, $tp& ref) { + ref = 0; + std::string refName = Tcl_GetStringFromObj(obj, 0); + if (${tp}_map.find(refName) == ${tp}_map.end()) { + std::ostringstream os; + os << "expected $tp but got \"" << refName << "\""; + Tcl_SetObjResult(interp, Tcl_NewStringObj(os.str().c_str(), -1)); + return TCL_ERROR; + } + ref = ${tp}_map[refName]; + return TCL_OK; +} +int GetListOf${tp}FromObj(Tcl_Interp* interp, Tcl_Obj* obj, $tp*& refs, unsigned& count) { + refs = 0; + count = 0; + Tcl_Obj** objs = 0; + int length; + if (Tcl_ListObjGetElements(interp, obj, &length, &objs) != TCL_OK) { + std::ostringstream os; + os << "expected list of types but got \"" << Tcl_GetStringFromObj(obj, 0) << "\""; + Tcl_SetObjResult(interp, Tcl_NewStringObj(os.str().c_str(), -1)); + return TCL_ERROR; + } + count = unsigned(length); + if (count == 0) + return TCL_OK; + refs = new $tp[count]; + for(unsigned i = 0; i < count; i++) { + if (Get${tp}FromObj(interp, objs[i], refs[i])) { + delete [] refs; + return TCL_ERROR; + } + } + return TCL_OK; +} +Tcl_Obj* Set${tp}AsObj(Tcl_Interp* interp, $tp ref) { + if (!ref) return Tcl_NewObj(); + if (${tp}_refmap.find(ref) == ${tp}_refmap.end()) { + std::string nm = GetRefName("${tp}_"); + ${tp}_map[nm] = ref; + ${tp}_refmap[ref] = nm; + } + return Tcl_NewStringObj(${tp}_refmap[ref].c_str(), -1); +} +} + + set tp [lindex [string trim $l " ;"] end] + puts $mf [string trim [subst -nocommands $template] "\n"] } set srcdir [file dirname [info script]] @@ -498,10 +431,12 @@ set mf [open [file join $targetdir llvmtcl-gen-map.h] w] foreach l $ll { set l [string trim $l] - if {[llength $l] == 0} continue + if {$l eq ""} continue switch -glob -- $l { "//*" { continue } - "rename *" { set rename([lindex $l 1]) [lindex $l 2] } + "rename *" { + set rename([lindex $l 1]) [string trim [lindex $l 2] ";"] + } "enum *" { gen_enum $cf $l } "typedef *" { gen_map $mf $l } default { gen_api_call $cf $of $l }