intptr_t WINAPI MsgDlgProc(HANDLE hDlg,intptr_t Msg,intptr_t Param1,void* Param2) { switch (Msg) { case DN_INITDIALOG: { FarDialogItem di; for (int i=0; SendDlgMessage(hDlg,DM_GETDLGITEMSHORT,i,&di); i++) { if (di.Type==DI_EDIT) { COORD pos={}; SendDlgMessage(hDlg,DM_SETCURSORPOS,i,&pos); } } } break; case DN_CTLCOLORDLGITEM: { FarDialogItem di; SendDlgMessage(hDlg,DM_GETDLGITEMSHORT,Param1,&di); if (di.Type==DI_EDIT) { FarColor Color=ColorIndexToColor(IsWarningStyle?COL_WARNDIALOGTEXT:COL_DIALOGTEXT); FarDialogItemColors* Colors = static_cast<FarDialogItemColors*>(Param2); Colors->Colors[0] = Color; Colors->Colors[2] = Color; } } break; case DN_CONTROLINPUT: { const INPUT_RECORD* record=(const INPUT_RECORD *)Param2; if (record->EventType==KEY_EVENT) { int key = InputRecordToKey(record); switch(key) { case KEY_F3: if(IsErrorType) { string Txt[2]; GetWin32ErrorString(LastError, Txt[0]); GetNtErrorString(NtStatus, Txt[1]); DialogBuilder Builder(MError, nullptr); Builder.AddConstEditField(FormatString() << L"LastError: 0x" << fmt::MinWidth(8) << fmt::FillChar(L'0') << fmt::Radix(16) << LastError << L" - " << Txt[0], 65); Builder.AddConstEditField(FormatString() << L"NTSTATUS: 0x" << fmt::MinWidth(8) << fmt::FillChar(L'0') << fmt::Radix(16) << NtStatus << L" - " << Txt[1], 65); Builder.AddOK(); Builder.ShowDialog(); } break; case KEY_TAB: case KEY_RIGHT: case KEY_NUMPAD6: if(Param1==LastButtonIndex) { SendDlgMessage(hDlg,DM_SETFOCUS,FirstButtonIndex,0); return TRUE; } break; case KEY_SHIFTTAB: case KEY_LEFT: case KEY_NUMPAD4: if(Param1==FirstButtonIndex) { SendDlgMessage(hDlg,DM_SETFOCUS,LastButtonIndex,0); return TRUE; } break; case KEY_CTRLC: case KEY_RCTRLC: case KEY_CTRLINS: case KEY_RCTRLINS: case KEY_CTRLNUMPAD0: case KEY_RCTRLNUMPAD0: { string* strText = reinterpret_cast<string*>(SendDlgMessage(hDlg, DM_GETDLGDATA, 0, 0)); CopyToClipboard(*strText); } break; } } } break; default: break; } return DefDlgProc(hDlg,Msg,Param1,Param2); }
void do_kill (dbref player, dbref cause, int key, char *what, char *costchar) { dbref victim; char *buf1, *buf2; int cost; init_match(player, what, TYPE_PLAYER); match_neighbor(); match_me(); match_here(); if (Wizard(player)) { match_player(); match_absolute(); } victim = match_result(); switch (victim) { case NOTHING: notify(player, "I don't see that player here."); break; case AMBIGUOUS: notify(player, "I don't know who you mean!"); break; default: if ((Typeof(victim) != TYPE_PLAYER) && (Typeof(victim) != TYPE_THING)) { notify(player, "Sorry, you can only kill players and things."); break; } if ((Haven(Location(victim)) && !Wizard(player)) || (controls(victim, Location(victim)) && !controls(player, Location(victim))) || Immortal(victim)) { notify(player, "Sorry."); break; } if (key == KILL_SLAY) { if (Builder(player) && Builder(victim)) { notify(player, "Sorry."); break; } } /* go for it */ cost = atoi(costchar); if (key == KILL_KILL) { if (HasPriv(victim,player,POWER_NOKILL,POWER4,NOTHING)) { notify(player, "Sorry."); break; } if (cost < mudconf.killmin) cost = mudconf.killmin; if (cost > mudconf.killmax) cost = mudconf.killmax; /* see if it works */ if (!payfor(player, cost)) { notify(player, unsafe_tprintf("You don't have enough %s.", mudconf.many_coins)); return; } } else { cost = 0; } if (!(((random() % mudconf.killguarantee) < cost) || (key == KILL_SLAY)) || Wizard(victim)) { /* Failure: notify player and victim only */ notify(player, "Your murder attempt failed."); buf1 = alloc_lbuf("do_kill.failed"); sprintf(buf1, "%s tried to kill you!", Name(player)); notify_with_cause(victim, player, buf1); if (Suspect(player)) { strcpy(buf1, Name(player)); if (player == Owner(player)) { raw_broadcast(0, WIZARD, "[Suspect] %s tried to kill %s(#%d).", buf1, Name(victim), victim); } else { buf2 = alloc_lbuf("do_kill.SUSP.failed"); strcpy(buf2, Name(Owner(player))); raw_broadcast(0, WIZARD, "[Suspect] %s <via %s(#%d)> tried to kill %s(#%d).", buf2, buf1, player, Name(victim), victim); free_lbuf(buf2); } } free_lbuf(buf1); break; } /* Success! You killed him */ buf1 = alloc_lbuf("do_kill.succ.1"); buf2 = alloc_lbuf("do_kill.succ.2"); if (Suspect(player)) { strcpy(buf1, Name(player)); if (player == Owner(player)) { raw_broadcast(0, WIZARD, "[Suspect] %s killed %s(#%d).", buf1, Name(victim), victim); } else { strcpy(buf2, Name(Owner(player))); raw_broadcast(0, WIZARD, "[Suspect] %s <via %s(#%d)> killed %s(#%d).", buf2, buf1, player, Name(victim), victim); } } sprintf(buf1, "You killed %s!", Name(victim)); sprintf(buf2, "killed %s!", Name(victim)); if (Typeof(victim) != TYPE_PLAYER) if (halt_que(NOTHING, victim) > 0) if (!Quiet(victim)) notify(Owner(victim), "Halted."); did_it(player, victim, A_KILL, buf1, A_OKILL, buf2, A_AKILL, (char **)NULL, 0); /* notify victim */ sprintf(buf1, "%s killed you!", Name(player)); notify_with_cause(victim, player, buf1); /* Pay off the bonus */ if (key == KILL_KILL) { cost /= 2; /* victim gets half */ if (Pennies(Owner(victim)) < mudconf.paylimit) { sprintf(buf1, "Your insurance policy pays %d %s.", cost, mudconf.many_coins); notify(victim, buf1); giveto(Owner(victim), cost, NOTHING); } else { notify(victim, "Your insurance policy has been revoked."); } } free_lbuf(buf1); free_lbuf(buf2); /* send him home */ move_via_generic(victim, HOME, NOTHING, 0); divest_object(victim); break; } }
static void give_money (dbref giver, dbref recipient, int key, int amount) { dbref aowner; int cost, pcost, rcost, aflags, dpamount; char *str; /* do amount consistency check */ if (amount < 0 && ((!Builder(giver) && !HasPriv(giver,recipient,POWER_STEAL,POWER3,NOTHING)) || DePriv(giver,recipient,DP_STEAL,POWER6,NOTHING))) { notify(giver, unsafe_tprintf("You look through your pockets. Nope, no negative %s.", mudconf.many_coins)); return; } if (!amount) { notify(giver, unsafe_tprintf("You must specify a positive number of %s.", mudconf.many_coins)); return; } dpamount = 0; if (amount < 0) dpamount = DePriv(giver,NOTHING,DP_NOSTEAL,POWER7,POWER_LEVEL_NA); else dpamount = DePriv(giver,NOTHING,DP_NOGOLD,POWER7,POWER_LEVEL_NA); if (dpamount) { if (DPShift(giver)) dpamount--; dpamount = mudconf.money_limit[dpamount]; } else dpamount = -1; if (!Admin(Owner(giver))) { if ((Typeof(recipient) == TYPE_PLAYER) && (Pennies(recipient) + amount > mudconf.paylimit)) { notify(giver, unsafe_tprintf("That player doesn't need that many %s!", mudconf.many_coins)); return; } if ((Typeof(recipient) != TYPE_PLAYER) && (!could_doit(giver, recipient, A_LUSE, 1, 1))) { notify(giver, unsafe_tprintf("%s won't take your money.", Name(recipient))); return; } } str = atr_get(Owner(giver), A_PAYLIM, &aowner, &aflags); pcost = atoi(str); free_lbuf(str); if (!Immortal(Owner(giver)) && pcost) { if ((Typeof(recipient) == TYPE_PLAYER) && (amount > 0) && (Pennies(recipient) + amount > pcost)) { notify(giver, unsafe_tprintf("That player doesn't need that many %s!", mudconf.many_coins)); return; } else if (Pennies(recipient) + amount < (-pcost)) { notify(giver,"That player doesn't need that much debt!"); return; } } str = atr_get(Owner(recipient), A_RECEIVELIM, &aowner, &aflags); rcost = atoi(str); free_lbuf(str); if (!Immortal(Owner(giver)) && rcost) { if ((Typeof(recipient) == TYPE_PLAYER) && (amount > 0) && (Pennies(recipient) + amount > rcost)) { notify(giver, unsafe_tprintf("That player doesn't need that many %s!", mudconf.many_coins)); return; } else if (Pennies(recipient) + amount < (-rcost)) { notify(giver,"That player doesn't need that much debt!"); return; } } if (!Immortal(Owner(giver))) { if (dpamount >= 0) { if (amount > 0) { if ((Typeof(recipient) == TYPE_PLAYER) && (Pennies(recipient) + amount > dpamount)) { notify(giver, unsafe_tprintf("That player doesn't need that many %s!", mudconf.many_coins)); return; } else if (amount > dpamount) { notify(giver, "Permission denied."); return; } } else { if ((Typeof(recipient) == TYPE_PLAYER) && (Pennies(recipient) + amount < (-dpamount))) { notify(giver, unsafe_tprintf("That player doesn't need that many %s!", mudconf.many_coins)); return; } else if (amount < (-dpamount)) { notify(giver, "Permission denied."); return; } } } } /* try to do the give */ if (!payfor_give(giver, amount)) { notify(giver, unsafe_tprintf("You don't have that many %s to give!", mudconf.many_coins)); return; } /* Find out cost if an object */ if (Typeof(recipient) == TYPE_THING) { str = atr_pget(recipient, A_COST, &aowner, &aflags); cost = atoi(str); free_lbuf(str); /* Can't afford it? */ if (amount < cost) { notify(giver, "Feeling poor today?"); giveto(giver, amount, NOTHING); return; } /* Negative cost */ if (cost < 0) { return; } } else { cost = amount; } if (!(key & GIVE_QUIET)) { if (amount == 1) { notify(giver, unsafe_tprintf("You give a %s to %s.", mudconf.one_coin, Name(recipient))); notify_with_cause(recipient, giver, unsafe_tprintf("%s gives you a %s.", Name(giver), mudconf.one_coin)); } else { notify(giver, unsafe_tprintf("You give %d %s to %s.", amount, mudconf.many_coins, Name(recipient))); notify_with_cause(recipient, giver, unsafe_tprintf("%s gives you %d %s.", Name(giver), amount, mudconf.many_coins)); } } else { if (amount == 1) { notify(giver, unsafe_tprintf("You give a %s to %s. (quiet)", mudconf.one_coin, Name(recipient))); } else { notify(giver, unsafe_tprintf("You give %d %s to %s. (quiet)", amount, mudconf.many_coins, Name(recipient))); } } /* Report change given */ if((amount - cost) == 1) { notify(giver, unsafe_tprintf("You get 1 %s in change.", mudconf.one_coin)); giveto(giver, 1, NOTHING); } else if (amount != cost) { notify(giver, unsafe_tprintf("You get %d %s in change.", (amount - cost), mudconf.many_coins)); giveto(giver, (amount - cost), NOTHING); } if (pcost && (Pennies(Owner(recipient)) + cost > pcost)) { pcost = pcost - Pennies(Owner(recipient)); if (pcost < 0) pcost = 0; } else pcost = cost; if (!giveto(recipient, pcost, giver)) giveto(giver, cost, NOTHING); /* Transfer the money and run PAY attributes */ /* Rooms should not kick off the PAY attribute */ if ( !isRoom(giver) ) did_it(giver, recipient, A_PAY, NULL, A_OPAY, NULL, A_APAY, (char **)NULL, 0); return; }
int main() { llvm::LLVMContext &Context = llvm::getGlobalContext(); llvm::Module *OurModule; llvm::IRBuilder<> Builder(Context); OurModule = new llvm::Module("Our first intermediary code", Context); /***** Main: a function that gets no parameters and returns an integer *****/ /* http://llvm.org/docs/doxygen/html/classllvm_1_1FunctionType.html * static FunctionType * get (Type *Result, bool isVarArg) * FunctionType::get - Create a FunctionType taking no parameters. */ llvm::Type* intType = llvm::Type::getInt64Ty(Context); llvm::FunctionType* typeOfMain = llvm::FunctionType::get(intType,false); /* http://llvm.org/docs/doxygen/html/classllvm_1_1Function.html * static Function * Create (FunctionType *Ty, LinkageTypes Linkage, const Twine &N="", Module *M=nullptr) */ llvm::Function* ourMain = llvm::Function::Create(typeOfMain,llvm::Function::ExternalLinkage, "main", OurModule); /* http://llvm.org/docs/doxygen/html/classllvm_1_1BasicBlock.html * static BasicBlock * Create (LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr) * Creates a new BasicBlock. */ llvm::BasicBlock *mainBB = llvm::BasicBlock::Create(Context, "entry", ourMain); /* http://llvm.org/docs/doxygen/html/classllvm_1_1IRBuilderBase.html * void SetInsertPoint (BasicBlock *TheBB) * This specifies that created instructions should be appended to the end of the specified block. */ Builder.SetInsertPoint(mainBB); /*** Constants are all unified ***/ /* http://llvm.org/docs/doxygen/html/classllvm_1_1ConstantInt.html * static ConstantInt * get (LLVMContext &Context, const APInt &V) * Return a ConstantInt with the specified value and an implied Type. * APInt = Arbitrary Precision Integer */ llvm::ConstantInt* Ten = llvm::ConstantInt::get(Context, llvm::APInt(64,10)); auto One = llvm::ConstantInt::get(Context, llvm::APInt(64, 1)); auto Zero = llvm::ConstantInt::get(Context, llvm::APInt(64, 0)); /* static ConstantInt * get (IntegerType *Ty, uint64_t V, bool isSigned=false) * Return a ConstantInt with the specified integer value for the specified type. */ llvm::Constant* NegativeOne = llvm::ConstantInt::get( intType, -1, true); /*** A and B are variables in memory, so we can read and write from them ***/ /* http://llvm.org/docs/doxygen/html/classllvm_1_1IRBuilder.html * AllocaInst * CreateAlloca (Type *Ty, Value *ArraySize=nullptr, const Twine &Name="") */ llvm::AllocaInst* A = Builder.CreateAlloca(intType, NULL, "A"); llvm::AllocaInst* B = Builder.CreateAlloca(intType, NULL, "B"); /*** lets compute * 0. A = 0; * 1. x = A; * 2. y = 0; * 3. x = y + 10 - (-1) + x; * 4. x = x + 1 + 10 + 10 + 10 * 5. A = x; * 6. return x; ***/ Builder.CreateStore(Zero, A); //0 llvm::Value* x = Builder.CreateLoad(A, "x"); //1 auto y = Builder.CreateAdd(Zero, Zero, "y"); //2 (one way to set to zero ;) auto tmp = Builder.CreateAdd(y, Ten, "tmp"); //3.1 tmp = Builder.CreateSub(tmp, NegativeOne, "tmp"); //3.2 x = Builder.CreateAdd(tmp, x, "x"); //3.3 x = Builder.CreateAdd(x, One, "x"); //4 x = Builder.CreateAdd(x, Ten, "x"); //4 x = Builder.CreateAdd(x, Ten, "x"); //4 x = Builder.CreateAdd(x, Ten, "x"); //4 Builder.CreateStore(x, A); //5 Builder.CreateRet(x); //6 llvm::verifyFunction(*ourMain); //Checks if everything is okay with our function /*** Lets print the intermediary representation generated ***/ OurModule->dump(); //ourMain->dump(); /*** Now lets compute it with a just in time (JIT) compiler ***/ llvm::ExecutionEngine* OurExecutionEngine; std::string Error; LLVMInitializeNativeTarget(); //target = generates code for my processor OurExecutionEngine = llvm::EngineBuilder(OurModule).setErrorStr(&Error).create(); if (!OurExecutionEngine) { fprintf(stderr, "Could not create OurExecutionEngine: %s\n", Error.c_str()); exit(1); } // JIT our main. It returns a function pointer. void *mainPointer = OurExecutionEngine->getPointerToFunction(ourMain); // Translate the pointer and run our main to get its results int (*result)() = (int (*)())(intptr_t)mainPointer; std::cout << "Result of our main = " << result() << std::endl; return 0; }
ShellDelete::ShellDelete(Panel *SrcPanel,bool Wipe): ReadOnlyDeleteMode(-1), m_SkipMode(-1), SkipWipeMode(-1), SkipFoldersMode(-1), ProcessedItems(0) { SCOPED_ACTION(ChangePriority)(Global->Opt->DelThreadPriority); SCOPED_ACTION(TPreRedrawFuncGuard)(std::make_unique<DelPreRedrawItem>()); os::FAR_FIND_DATA FindData; string strDeleteFilesMsg; string strSelName; string strSelShortName; string strDizName; DWORD FileAttr; size_t SelCount; int UpdateDiz; int DizPresent; int Ret; BOOL NeedUpdate=TRUE, NeedSetUpADir=FALSE; bool Opt_DeleteToRecycleBin=Global->Opt->DeleteToRecycleBin; /*& 31.05.2001 OT Запретить перерисовку текущего окна*/ auto WindowFromLaunched = Global->WindowManager->GetCurrentWindow(); WindowFromLaunched->Lock(); bool DeleteAllFolders=!Global->Opt->Confirm.DeleteFolder; UpdateDiz=(Global->Opt->Diz.UpdateMode==DIZ_UPDATE_ALWAYS || (SrcPanel->IsDizDisplayed() && Global->Opt->Diz.UpdateMode==DIZ_UPDATE_IF_DISPLAYED)); SCOPE_EXIT { Global->Opt->DeleteToRecycleBin=Opt_DeleteToRecycleBin; // Разрешить перерисовку окна WindowFromLaunched->Unlock(); if (NeedUpdate) { ShellUpdatePanels(SrcPanel,NeedSetUpADir); } }; if (!(SelCount=SrcPanel->GetSelCount())) return; // Удаление в корзину только для FIXED-дисков { string strRoot; SrcPanel->GetSelName(nullptr,FileAttr); SrcPanel->GetSelName(&strSelName,FileAttr); ConvertNameToFull(strSelName, strRoot); GetPathRoot(strRoot,strRoot); if (Global->Opt->DeleteToRecycleBin && FAR_GetDriveType(strRoot) != DRIVE_FIXED) Global->Opt->DeleteToRecycleBin=0; } if (SelCount==1) { SrcPanel->GetSelName(nullptr,FileAttr); SrcPanel->GetSelName(&strSelName,FileAttr); if (TestParentFolderName(strSelName) || strSelName.empty()) { NeedUpdate=FALSE; return; } strDeleteFilesMsg = strSelName; QuoteLeadingSpace(strDeleteFilesMsg); } else { // в зависимости от числа ставим нужное окончание const wchar_t *Ends; FormatString StrItems; StrItems << SelCount; Ends=MSG(MAskDeleteItemsA); size_t LenItems = StrItems.size(); if (LenItems > 0) { if ((LenItems >= 2 && StrItems[LenItems-2] == L'1') || StrItems[LenItems-1] >= L'5' || StrItems[LenItems-1] == L'0') Ends=MSG(MAskDeleteItemsS); else if (StrItems[LenItems-1] == L'1') Ends=MSG(MAskDeleteItems0); } strDeleteFilesMsg = LangString(MAskDeleteItems) << SelCount << Ends; } Ret=1; // Обработка "удаления" линков if ((FileAttr & FILE_ATTRIBUTE_REPARSE_POINT) && SelCount==1) { string strJuncName; ConvertNameToFull(strSelName,strJuncName); if (GetReparsePointInfo(strJuncName, strJuncName)) // ? SelName ? { NormalizeSymlinkName(strJuncName); string strAskDeleteLink=MSG(MAskDeleteLink); os::fs::file_status Status(strJuncName); if (os::fs::exists(Status)) { strAskDeleteLink+=L" "; strAskDeleteLink += MSG(is_directory(Status)? MAskDeleteLinkFolder : MAskDeleteLinkFile); } Ret=Message(0,2,MSG(MDeleteLinkTitle), strDeleteFilesMsg.data(), strAskDeleteLink.data(), strJuncName.data(), MSG(MDeleteLinkDelete), MSG(MCancel)); if (Ret) return; } } if (Ret && Global->Opt->Confirm.Delete) { LNGID mTitle = Wipe ? MDeleteWipeTitle : MDeleteTitle; LNGID mDText; string tText; LNGID mDBttn = Wipe ? MDeleteWipe : Global->Opt->DeleteToRecycleBin ? MDeleteRecycle : MDelete; bool bHilite = Global->Opt->DelOpt.HighlightSelected; int mshow = std::min(std::max((int)Global->Opt->DelOpt.ShowSelected, 1), ScrY/2); std::vector<string> items; items.push_back(strDeleteFilesMsg); if (SelCount == 1) { bool folder = (FileAttr & FILE_ATTRIBUTE_DIRECTORY) != 0; if (Wipe && !(FileAttr & FILE_ATTRIBUTE_REPARSE_POINT)) mDText = folder ? MAskWipeFolder : MAskWipeFile; else { if (Global->Opt->DeleteToRecycleBin) mDText = folder ? MAskDeleteRecycleFolder : MAskDeleteRecycleFile; else mDText = folder ? MAskDeleteFolder : MAskDeleteFile; } if (bHilite) { string name, sname; SrcPanel->GetCurName(name, sname); QuoteLeadingSpace(name); bHilite = strDeleteFilesMsg != name; } } else { if (Wipe) { mDText = MAskWipe; mTitle = MDeleteWipeTitle; } else mDText = Global->Opt->DeleteToRecycleBin ? MAskDeleteRecycle : MAskDelete; if (mshow > 1) { tText = MSG(mDText) + string(L" ") + strDeleteFilesMsg; items.clear(); DWORD attr; string name; SrcPanel->GetSelName(nullptr, attr); for (size_t i = 0; i < SelCount; ++i) { if (i == (size_t)mshow-1 && i+1 < SelCount) { items.push_back(L"..."); break; } SrcPanel->GetSelName(&name, attr); QuoteLeadingSpace(name); items.push_back(name); } } } intptr_t start_hilite = 0, end_hilite = 0; DialogBuilder Builder(mTitle, nullptr, [&](Dialog* Dlg, intptr_t Msg, intptr_t Param1, void* Param2) -> intptr_t { if (bHilite && Msg == DN_CTLCOLORDLGITEM && Param1 >= start_hilite && Param1 <= end_hilite) { auto Colors = static_cast<FarDialogItemColors*>(Param2); Colors->Colors[0] = Colors->Colors[1]; } return Dlg->DefProc(Msg, Param1, Param2); }); if (tText.empty()) tText = MSG(mDText); Builder.AddText(tText.data())->Flags = DIF_CENTERTEXT; if (bHilite || (mshow > 1 && SelCount > 1)) Builder.AddSeparator(); std::for_each(RANGE(items, i) { TruncStrFromCenter(i, ScrX+1-6*2); auto dx = Builder.AddText(i.data()); dx->Flags = (SelCount <= 1 || mshow <= 1 ? DIF_CENTERTEXT : 0) | DIF_SHOWAMPERSAND; size_t index = Builder.GetLastID(); end_hilite = index; if (!start_hilite) start_hilite = index; });
void SymtabBuilder::Do(const Program* Prog, SymbolTable* TheSymbolTable) { SymtabBuilder Builder(TheSymbolTable); Prog->Accept(&Builder); }
/* TODO: funcao booleana teste(booleano x, booleano y, booleano z) retorna (x & y) | (x & z) | (~z & ~y) teste(falso, falso, verdadeiro) funcao real exemplo (real a, real b) retorna (a*b) - 2.0 * (a mod b) exemplo(10.0, 5.0) static Type* getDoubleTy(context) */ int main(){ llvm::LLVMContext &Context = llvm::getGlobalContext(); llvm::Module *OurModule; llvm::IRBuilder<> Builder(Context); OurModule = new llvm::Module("Our first intermediary code", Context); // main llvm::Type* intType = llvm::Type::getInt64Ty(Context); llvm::FunctionType* typeOfMain = llvm::FunctionType::get(intType,false); llvm::Function* ourMain = llvm::Function::Create(typeOfMain,llvm::Function::ExternalLinkage, "main", OurModule); llvm::BasicBlock *mainBB = llvm::BasicBlock::Create(Context, "entry", ourMain); Builder.SetInsertPoint(mainBB); // constants auto Zero_I = llvm::ConstantInt::get(Context, llvm::APInt(1, 0)); auto One_I = llvm::ConstantInt::get(Context, llvm::APInt(1, 1)); auto Two_D = llvm::ConstantFP::get(Context, llvm::APFloat(2.0)); auto Five_D = llvm::ConstantFP::get(Context, llvm::APFloat(5.0)); auto Ten_D = llvm::ConstantFP::get(Context, llvm::APFloat(10.0)); // teste auto boolType = llvm::Type::getInt1Ty(Context); std::vector<llvm::Type*> TesteParameters(3, boolType); llvm::FunctionType* typeOfTeste = llvm::FunctionType::get(boolType, TesteParameters, false); llvm::Function* ourTeste = llvm::Function::Create(typeOfTeste,llvm::Function::ExternalLinkage, "teste", OurModule); llvm::Function::arg_iterator params = ourTeste->arg_begin(); params->setName("x"); ++params; params->setName("y"); ++params; params->setName("z"); llvm::BasicBlock *testeBB = llvm::BasicBlock::Create(Context, "tst", ourTeste); Builder.SetInsertPoint(testeBB); auto x = ourTeste->arg_begin(); auto y = ++(ourTeste->arg_begin()); auto z = ++(++(ourTeste->arg_begin())); auto xANDy = Builder.CreateAnd(x,y,"xANDy"); auto xANDz = Builder.CreateAnd(x,z,"xANDz"); auto notZ = Builder.CreateNot(z, "notZ"); auto notY = Builder.CreateNot(y, "notY"); auto zANDy = Builder.CreateAnd(notZ, notY,"nzANDny"); auto tmp = Builder.CreateOr(xANDy, xANDz, "ortmp"); tmp = Builder.CreateOr(tmp, zANDy, "ortmp"); Builder.CreateRet(tmp); llvm::verifyFunction(*ourTeste); // main Builder.SetInsertPoint(mainBB); std::vector<llvm::Value*> arguments; arguments.push_back(Zero_I); arguments.push_back(Zero_I); arguments.push_back(One_I); auto tstReturn = Builder.CreateCall(ourTeste, arguments, "calltst"); auto intRet = Builder.CreateIntCast(tstReturn, intType, false); Builder.CreateRet(intRet); llvm::verifyFunction(*ourMain); OurModule->dump(); llvm::ExecutionEngine* OurExecutionEngine; std::string Error; LLVMInitializeNativeTarget(); //target = generates code for my processor OurExecutionEngine = llvm::EngineBuilder(OurModule).setErrorStr(&Error).create(); if (!OurExecutionEngine) { fprintf(stderr, "Could not create OurExecutionEngine: %s\n", Error.c_str()); exit(1); } // JIT our main. It returns a function pointer. void *mainPointer = OurExecutionEngine->getPointerToFunction(ourMain); // Translate the pointer and run our main to get its results int (*result)() = (int (*)())(intptr_t)mainPointer; std::cout << "Result of our main = " << result() << std::endl; return 0; }
/** * Builds the "sera.lang" assembly */ int Main::build() { Builder builder = Builder(); return builder.build(); }