vm_rv32imac.cpp 200KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817
  1. /*******************************************************************************
  2. * Copyright (C) 2017, 2018 MINRES Technologies GmbH
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * 3. Neither the name of the copyright holder nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. *******************************************************************************/
  32. #include <iss/arch/rv32imac.h>
  33. #include <iss/arch/riscv_hart_msu_vp.h>
  34. #include <iss/debugger/gdb_session.h>
  35. #include <iss/debugger/server.h>
  36. #include <iss/iss.h>
  37. #include <iss/llvm/vm_base.h>
  38. #include <util/logging.h>
  39. #define FMT_HEADER_ONLY
  40. #include <fmt/format.h>
  41. #include <array>
  42. #include <iss/debugger/riscv_target_adapter.h>
  43. namespace iss {
  44. namespace vm {
  45. namespace fp_impl {
  46. void add_fp_functions_2_module(llvm::Module *, unsigned, unsigned);
  47. }
  48. }
  49. namespace rv32imac {
  50. using namespace iss::arch;
  51. using namespace llvm;
  52. using namespace iss::debugger;
  53. using namespace iss::vm::llvm;
  54. template <typename ARCH> class vm_impl : public vm_base<ARCH> {
  55. public:
  56. using super = typename iss::vm::llvm::vm_base<ARCH>;
  57. using virt_addr_t = typename super::virt_addr_t;
  58. using phys_addr_t = typename super::phys_addr_t;
  59. using code_word_t = typename super::code_word_t;
  60. using addr_t = typename super::addr_t;
  61. vm_impl();
  62. vm_impl(ARCH &core, unsigned core_id = 0, unsigned cluster_id = 0);
  63. void enableDebug(bool enable) { super::sync_exec = super::ALL_SYNC; }
  64. target_adapter_if *accquire_target_adapter(server_if *srv) override {
  65. debugger_if::dbg_enabled = true;
  66. if (vm_base<ARCH>::tgt_adapter == nullptr)
  67. vm_base<ARCH>::tgt_adapter = new riscv_target_adapter<ARCH>(srv, this->get_arch());
  68. return vm_base<ARCH>::tgt_adapter;
  69. }
  70. protected:
  71. using vm_base<ARCH>::get_reg_ptr;
  72. inline const char *name(size_t index){return traits<ARCH>::reg_aliases.at(index);}
  73. template <typename T> inline ConstantInt *size(T type) {
  74. return ConstantInt::get(getContext(), APInt(32, type->getType()->getScalarSizeInBits()));
  75. }
  76. void setup_module(Module* m) override {
  77. super::setup_module(m);
  78. iss::vm::fp_impl::add_fp_functions_2_module(m, traits<ARCH>::FP_REGS_SIZE, traits<ARCH>::XLEN);
  79. }
  80. inline Value *gen_choose(Value *cond, Value *trueVal, Value *falseVal, unsigned size) {
  81. return super::gen_cond_assign(cond, this->gen_ext(trueVal, size), this->gen_ext(falseVal, size));
  82. }
  83. std::tuple<continuation_e, BasicBlock *> gen_single_inst_behavior(virt_addr_t &, unsigned int &, BasicBlock *) override;
  84. void gen_leave_behavior(BasicBlock *leave_blk) override;
  85. void gen_raise_trap(uint16_t trap_id, uint16_t cause);
  86. void gen_leave_trap(unsigned lvl);
  87. void gen_wait(unsigned type);
  88. void gen_trap_behavior(BasicBlock *) override;
  89. void gen_trap_check(BasicBlock *bb);
  90. inline Value *gen_reg_load(unsigned i, unsigned level = 0) {
  91. return this->builder.CreateLoad(get_reg_ptr(i), false);
  92. }
  93. inline void gen_set_pc(virt_addr_t pc, unsigned reg_num) {
  94. Value *next_pc_v = this->builder.CreateSExtOrTrunc(this->gen_const(traits<ARCH>::XLEN, pc.val),
  95. this->get_type(traits<ARCH>::XLEN));
  96. this->builder.CreateStore(next_pc_v, get_reg_ptr(reg_num), true);
  97. }
  98. // some compile time constants
  99. // enum { MASK16 = 0b1111110001100011, MASK32 = 0b11111111111100000111000001111111 };
  100. enum { MASK16 = 0b1111111111111111, MASK32 = 0b11111111111100000111000001111111 };
  101. enum { EXTR_MASK16 = MASK16 >> 2, EXTR_MASK32 = MASK32 >> 2 };
  102. enum { LUT_SIZE = 1 << util::bit_count(EXTR_MASK32), LUT_SIZE_C = 1 << util::bit_count(EXTR_MASK16) };
  103. using this_class = vm_impl<ARCH>;
  104. using compile_func = std::tuple<continuation_e, BasicBlock *> (this_class::*)(virt_addr_t &pc,
  105. code_word_t instr,
  106. BasicBlock *bb);
  107. std::array<compile_func, LUT_SIZE> lut;
  108. std::array<compile_func, LUT_SIZE_C> lut_00, lut_01, lut_10;
  109. std::array<compile_func, LUT_SIZE> lut_11;
  110. std::array<compile_func *, 4> qlut;
  111. std::array<const uint32_t, 4> lutmasks = {{EXTR_MASK16, EXTR_MASK16, EXTR_MASK16, EXTR_MASK32}};
  112. void expand_bit_mask(int pos, uint32_t mask, uint32_t value, uint32_t valid, uint32_t idx, compile_func lut[],
  113. compile_func f) {
  114. if (pos < 0) {
  115. lut[idx] = f;
  116. } else {
  117. auto bitmask = 1UL << pos;
  118. if ((mask & bitmask) == 0) {
  119. expand_bit_mask(pos - 1, mask, value, valid, idx, lut, f);
  120. } else {
  121. if ((valid & bitmask) == 0) {
  122. expand_bit_mask(pos - 1, mask, value, valid, (idx << 1), lut, f);
  123. expand_bit_mask(pos - 1, mask, value, valid, (idx << 1) + 1, lut, f);
  124. } else {
  125. auto new_val = idx << 1;
  126. if ((value & bitmask) != 0) new_val++;
  127. expand_bit_mask(pos - 1, mask, value, valid, new_val, lut, f);
  128. }
  129. }
  130. }
  131. }
  132. inline uint32_t extract_fields(uint32_t val) { return extract_fields(29, val >> 2, lutmasks[val & 0x3], 0); }
  133. uint32_t extract_fields(int pos, uint32_t val, uint32_t mask, uint32_t lut_val) {
  134. if (pos >= 0) {
  135. auto bitmask = 1UL << pos;
  136. if ((mask & bitmask) == 0) {
  137. lut_val = extract_fields(pos - 1, val, mask, lut_val);
  138. } else {
  139. auto new_val = lut_val << 1;
  140. if ((val & bitmask) != 0) new_val++;
  141. lut_val = extract_fields(pos - 1, val, mask, new_val);
  142. }
  143. }
  144. return lut_val;
  145. }
  146. private:
  147. /****************************************************************************
  148. * start opcode definitions
  149. ****************************************************************************/
  150. struct InstructionDesriptor {
  151. size_t length;
  152. uint32_t value;
  153. uint32_t mask;
  154. compile_func op;
  155. };
  156. const std::array<InstructionDesriptor, 99> instr_descr = {{
  157. /* entries are: size, valid value, valid mask, function ptr */
  158. /* instruction JALR */
  159. {32, 0b00000000000000000000000001100111, 0b00000000000000000111000001111111, &this_class::__jalr},
  160. /* instruction C.ADDI4SPN */
  161. {16, 0b0000000000000000, 0b1110000000000011, &this_class::__c_addi4spn},
  162. /* instruction C.LW */
  163. {16, 0b0100000000000000, 0b1110000000000011, &this_class::__c_lw},
  164. /* instruction C.SW */
  165. {16, 0b1100000000000000, 0b1110000000000011, &this_class::__c_sw},
  166. /* instruction C.ADDI */
  167. {16, 0b0000000000000001, 0b1110000000000011, &this_class::__c_addi},
  168. /* instruction C.NOP */
  169. {16, 0b0000000000000001, 0b1111111111111111, &this_class::__c_nop},
  170. /* instruction C.JAL */
  171. {16, 0b0010000000000001, 0b1110000000000011, &this_class::__c_jal},
  172. /* instruction C.LI */
  173. {16, 0b0100000000000001, 0b1110000000000011, &this_class::__c_li},
  174. /* instruction C.LUI */
  175. {16, 0b0110000000000001, 0b1110000000000011, &this_class::__c_lui},
  176. /* instruction C.ADDI16SP */
  177. {16, 0b0110000100000001, 0b1110111110000011, &this_class::__c_addi16sp},
  178. /* instruction C.SRLI */
  179. {16, 0b1000000000000001, 0b1111110000000011, &this_class::__c_srli},
  180. /* instruction C.SRAI */
  181. {16, 0b1000010000000001, 0b1111110000000011, &this_class::__c_srai},
  182. /* instruction C.ANDI */
  183. {16, 0b1000100000000001, 0b1110110000000011, &this_class::__c_andi},
  184. /* instruction C.SUB */
  185. {16, 0b1000110000000001, 0b1111110001100011, &this_class::__c_sub},
  186. /* instruction C.XOR */
  187. {16, 0b1000110000100001, 0b1111110001100011, &this_class::__c_xor},
  188. /* instruction C.OR */
  189. {16, 0b1000110001000001, 0b1111110001100011, &this_class::__c_or},
  190. /* instruction C.AND */
  191. {16, 0b1000110001100001, 0b1111110001100011, &this_class::__c_and},
  192. /* instruction C.J */
  193. {16, 0b1010000000000001, 0b1110000000000011, &this_class::__c_j},
  194. /* instruction C.BEQZ */
  195. {16, 0b1100000000000001, 0b1110000000000011, &this_class::__c_beqz},
  196. /* instruction C.BNEZ */
  197. {16, 0b1110000000000001, 0b1110000000000011, &this_class::__c_bnez},
  198. /* instruction C.SLLI */
  199. {16, 0b0000000000000010, 0b1111000000000011, &this_class::__c_slli},
  200. /* instruction C.LWSP */
  201. {16, 0b0100000000000010, 0b1110000000000011, &this_class::__c_lwsp},
  202. /* instruction C.MV */
  203. {16, 0b1000000000000010, 0b1111000000000011, &this_class::__c_mv},
  204. /* instruction C.JR */
  205. {16, 0b1000000000000010, 0b1111000001111111, &this_class::__c_jr},
  206. /* instruction C.ADD */
  207. {16, 0b1001000000000010, 0b1111000000000011, &this_class::__c_add},
  208. /* instruction C.JALR */
  209. {16, 0b1001000000000010, 0b1111000001111111, &this_class::__c_jalr},
  210. /* instruction C.EBREAK */
  211. {16, 0b1001000000000010, 0b1111111111111111, &this_class::__c_ebreak},
  212. /* instruction C.SWSP */
  213. {16, 0b1100000000000010, 0b1110000000000011, &this_class::__c_swsp},
  214. /* instruction DII */
  215. {16, 0b0000000000000000, 0b1111111111111111, &this_class::__dii},
  216. /* instruction LR.W */
  217. {32, 0b00010000000000000010000000101111, 0b11111001111100000111000001111111, &this_class::__lr_w},
  218. /* instruction SC.W */
  219. {32, 0b00011000000000000010000000101111, 0b11111000000000000111000001111111, &this_class::__sc_w},
  220. /* instruction AMOSWAP.W */
  221. {32, 0b00001000000000000010000000101111, 0b11111000000000000111000001111111, &this_class::__amoswap_w},
  222. /* instruction AMOADD.W */
  223. {32, 0b00000000000000000010000000101111, 0b11111000000000000111000001111111, &this_class::__amoadd_w},
  224. /* instruction AMOXOR.W */
  225. {32, 0b00100000000000000010000000101111, 0b11111000000000000111000001111111, &this_class::__amoxor_w},
  226. /* instruction AMOAND.W */
  227. {32, 0b01100000000000000010000000101111, 0b11111000000000000111000001111111, &this_class::__amoand_w},
  228. /* instruction AMOOR.W */
  229. {32, 0b01000000000000000010000000101111, 0b11111000000000000111000001111111, &this_class::__amoor_w},
  230. /* instruction AMOMIN.W */
  231. {32, 0b10000000000000000010000000101111, 0b11111000000000000111000001111111, &this_class::__amomin_w},
  232. /* instruction AMOMAX.W */
  233. {32, 0b10100000000000000010000000101111, 0b11111000000000000111000001111111, &this_class::__amomax_w},
  234. /* instruction AMOMINU.W */
  235. {32, 0b11000000000000000010000000101111, 0b11111000000000000111000001111111, &this_class::__amominu_w},
  236. /* instruction AMOMAXU.W */
  237. {32, 0b11100000000000000010000000101111, 0b11111000000000000111000001111111, &this_class::__amomaxu_w},
  238. /* instruction MUL */
  239. {32, 0b00000010000000000000000000110011, 0b11111110000000000111000001111111, &this_class::__mul},
  240. /* instruction MULH */
  241. {32, 0b00000010000000000001000000110011, 0b11111110000000000111000001111111, &this_class::__mulh},
  242. /* instruction MULHSU */
  243. {32, 0b00000010000000000010000000110011, 0b11111110000000000111000001111111, &this_class::__mulhsu},
  244. /* instruction MULHU */
  245. {32, 0b00000010000000000011000000110011, 0b11111110000000000111000001111111, &this_class::__mulhu},
  246. /* instruction DIV */
  247. {32, 0b00000010000000000100000000110011, 0b11111110000000000111000001111111, &this_class::__div},
  248. /* instruction DIVU */
  249. {32, 0b00000010000000000101000000110011, 0b11111110000000000111000001111111, &this_class::__divu},
  250. /* instruction REM */
  251. {32, 0b00000010000000000110000000110011, 0b11111110000000000111000001111111, &this_class::__rem},
  252. /* instruction REMU */
  253. {32, 0b00000010000000000111000000110011, 0b11111110000000000111000001111111, &this_class::__remu},
  254. /* instruction LUI */
  255. {32, 0b00000000000000000000000000110111, 0b00000000000000000000000001111111, &this_class::__lui},
  256. /* instruction AUIPC */
  257. {32, 0b00000000000000000000000000010111, 0b00000000000000000000000001111111, &this_class::__auipc},
  258. /* instruction JAL */
  259. {32, 0b00000000000000000000000001101111, 0b00000000000000000000000001111111, &this_class::__jal},
  260. /* instruction BEQ */
  261. {32, 0b00000000000000000000000001100011, 0b00000000000000000111000001111111, &this_class::__beq},
  262. /* instruction BNE */
  263. {32, 0b00000000000000000001000001100011, 0b00000000000000000111000001111111, &this_class::__bne},
  264. /* instruction BLT */
  265. {32, 0b00000000000000000100000001100011, 0b00000000000000000111000001111111, &this_class::__blt},
  266. /* instruction BGE */
  267. {32, 0b00000000000000000101000001100011, 0b00000000000000000111000001111111, &this_class::__bge},
  268. /* instruction BLTU */
  269. {32, 0b00000000000000000110000001100011, 0b00000000000000000111000001111111, &this_class::__bltu},
  270. /* instruction BGEU */
  271. {32, 0b00000000000000000111000001100011, 0b00000000000000000111000001111111, &this_class::__bgeu},
  272. /* instruction LB */
  273. {32, 0b00000000000000000000000000000011, 0b00000000000000000111000001111111, &this_class::__lb},
  274. /* instruction LH */
  275. {32, 0b00000000000000000001000000000011, 0b00000000000000000111000001111111, &this_class::__lh},
  276. /* instruction LW */
  277. {32, 0b00000000000000000010000000000011, 0b00000000000000000111000001111111, &this_class::__lw},
  278. /* instruction LBU */
  279. {32, 0b00000000000000000100000000000011, 0b00000000000000000111000001111111, &this_class::__lbu},
  280. /* instruction LHU */
  281. {32, 0b00000000000000000101000000000011, 0b00000000000000000111000001111111, &this_class::__lhu},
  282. /* instruction SB */
  283. {32, 0b00000000000000000000000000100011, 0b00000000000000000111000001111111, &this_class::__sb},
  284. /* instruction SH */
  285. {32, 0b00000000000000000001000000100011, 0b00000000000000000111000001111111, &this_class::__sh},
  286. /* instruction SW */
  287. {32, 0b00000000000000000010000000100011, 0b00000000000000000111000001111111, &this_class::__sw},
  288. /* instruction ADDI */
  289. {32, 0b00000000000000000000000000010011, 0b00000000000000000111000001111111, &this_class::__addi},
  290. /* instruction SLTI */
  291. {32, 0b00000000000000000010000000010011, 0b00000000000000000111000001111111, &this_class::__slti},
  292. /* instruction SLTIU */
  293. {32, 0b00000000000000000011000000010011, 0b00000000000000000111000001111111, &this_class::__sltiu},
  294. /* instruction XORI */
  295. {32, 0b00000000000000000100000000010011, 0b00000000000000000111000001111111, &this_class::__xori},
  296. /* instruction ORI */
  297. {32, 0b00000000000000000110000000010011, 0b00000000000000000111000001111111, &this_class::__ori},
  298. /* instruction ANDI */
  299. {32, 0b00000000000000000111000000010011, 0b00000000000000000111000001111111, &this_class::__andi},
  300. /* instruction SLLI */
  301. {32, 0b00000000000000000001000000010011, 0b11111110000000000111000001111111, &this_class::__slli},
  302. /* instruction SRLI */
  303. {32, 0b00000000000000000101000000010011, 0b11111110000000000111000001111111, &this_class::__srli},
  304. /* instruction SRAI */
  305. {32, 0b01000000000000000101000000010011, 0b11111110000000000111000001111111, &this_class::__srai},
  306. /* instruction ADD */
  307. {32, 0b00000000000000000000000000110011, 0b11111110000000000111000001111111, &this_class::__add},
  308. /* instruction SUB */
  309. {32, 0b01000000000000000000000000110011, 0b11111110000000000111000001111111, &this_class::__sub},
  310. /* instruction SLL */
  311. {32, 0b00000000000000000001000000110011, 0b11111110000000000111000001111111, &this_class::__sll},
  312. /* instruction SLT */
  313. {32, 0b00000000000000000010000000110011, 0b11111110000000000111000001111111, &this_class::__slt},
  314. /* instruction SLTU */
  315. {32, 0b00000000000000000011000000110011, 0b11111110000000000111000001111111, &this_class::__sltu},
  316. /* instruction XOR */
  317. {32, 0b00000000000000000100000000110011, 0b11111110000000000111000001111111, &this_class::__xor},
  318. /* instruction SRL */
  319. {32, 0b00000000000000000101000000110011, 0b11111110000000000111000001111111, &this_class::__srl},
  320. /* instruction SRA */
  321. {32, 0b01000000000000000101000000110011, 0b11111110000000000111000001111111, &this_class::__sra},
  322. /* instruction OR */
  323. {32, 0b00000000000000000110000000110011, 0b11111110000000000111000001111111, &this_class::__or},
  324. /* instruction AND */
  325. {32, 0b00000000000000000111000000110011, 0b11111110000000000111000001111111, &this_class::__and},
  326. /* instruction FENCE */
  327. {32, 0b00000000000000000000000000001111, 0b11110000000000000111000001111111, &this_class::__fence},
  328. /* instruction FENCE_I */
  329. {32, 0b00000000000000000001000000001111, 0b00000000000000000111000001111111, &this_class::__fence_i},
  330. /* instruction ECALL */
  331. {32, 0b00000000000000000000000001110011, 0b11111111111111111111111111111111, &this_class::__ecall},
  332. /* instruction EBREAK */
  333. {32, 0b00000000000100000000000001110011, 0b11111111111111111111111111111111, &this_class::__ebreak},
  334. /* instruction URET */
  335. {32, 0b00000000001000000000000001110011, 0b11111111111111111111111111111111, &this_class::__uret},
  336. /* instruction SRET */
  337. {32, 0b00010000001000000000000001110011, 0b11111111111111111111111111111111, &this_class::__sret},
  338. /* instruction MRET */
  339. {32, 0b00110000001000000000000001110011, 0b11111111111111111111111111111111, &this_class::__mret},
  340. /* instruction WFI */
  341. {32, 0b00010000010100000000000001110011, 0b11111111111111111111111111111111, &this_class::__wfi},
  342. /* instruction SFENCE.VMA */
  343. {32, 0b00010010000000000000000001110011, 0b11111110000000000111111111111111, &this_class::__sfence_vma},
  344. /* instruction CSRRW */
  345. {32, 0b00000000000000000001000001110011, 0b00000000000000000111000001111111, &this_class::__csrrw},
  346. /* instruction CSRRS */
  347. {32, 0b00000000000000000010000001110011, 0b00000000000000000111000001111111, &this_class::__csrrs},
  348. /* instruction CSRRC */
  349. {32, 0b00000000000000000011000001110011, 0b00000000000000000111000001111111, &this_class::__csrrc},
  350. /* instruction CSRRWI */
  351. {32, 0b00000000000000000101000001110011, 0b00000000000000000111000001111111, &this_class::__csrrwi},
  352. /* instruction CSRRSI */
  353. {32, 0b00000000000000000110000001110011, 0b00000000000000000111000001111111, &this_class::__csrrsi},
  354. /* instruction CSRRCI */
  355. {32, 0b00000000000000000111000001110011, 0b00000000000000000111000001111111, &this_class::__csrrci},
  356. }};
  357. /* instruction definitions */
  358. /* instruction 0: JALR */
  359. std::tuple<continuation_e, BasicBlock*> __jalr(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  360. bb->setName("JALR");
  361. this->gen_sync(PRE_SYNC, 0);
  362. uint8_t rd = ((bit_sub<7,5>(instr)));
  363. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  364. int16_t imm = signextend<int16_t,12>((bit_sub<20,12>(instr)));
  365. if(this->disass_enabled){
  366. /* generate console output when executing the command */
  367. auto mnemonic = fmt::format(
  368. "{mnemonic:10} {rd}, {rs1}, {imm:#0x}", fmt::arg("mnemonic", "jalr"),
  369. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("imm", imm));
  370. std::vector<Value*> args {
  371. this->core_ptr,
  372. this->gen_const(64, pc.val),
  373. this->builder.CreateGlobalStringPtr(mnemonic),
  374. };
  375. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  376. }
  377. Value* cur_pc_val = this->gen_const(32, pc.val);
  378. pc=pc+4;
  379. Value* new_pc_val = this->builder.CreateAdd(
  380. this->gen_ext(
  381. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  382. 32, true),
  383. this->gen_const(32U, imm));
  384. Value* align_val = this->builder.CreateAnd(
  385. new_pc_val,
  386. this->gen_const(32U, 0x2));
  387. {
  388. BasicBlock* bbnext = BasicBlock::Create(this->mod->getContext(), "endif", this->func, this->leave_blk);
  389. BasicBlock* bb_then = BasicBlock::Create(this->mod->getContext(), "thenbr", this->func, bbnext);
  390. BasicBlock* bb_else = BasicBlock::Create(this->mod->getContext(), "elsebr", this->func, bbnext);
  391. // this->builder.SetInsertPoint(bb);
  392. this->gen_cond_branch(this->builder.CreateICmp(
  393. ICmpInst::ICMP_NE,
  394. align_val,
  395. this->gen_const(32U, 0)),
  396. bb_then,
  397. bb_else);
  398. this->builder.SetInsertPoint(bb_then);
  399. {
  400. this->gen_raise_trap(0, 0);
  401. }
  402. this->builder.CreateBr(bbnext);
  403. this->builder.SetInsertPoint(bb_else);
  404. {
  405. if(rd != 0){
  406. Value* Xtmp0_val = this->builder.CreateAdd(
  407. cur_pc_val,
  408. this->gen_const(32U, 4));
  409. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  410. }
  411. Value* PC_val = this->builder.CreateAnd(
  412. new_pc_val,
  413. this->builder.CreateNot(this->gen_const(32U, 0x1)));
  414. this->builder.CreateStore(PC_val, get_reg_ptr(traits<ARCH>::NEXT_PC), false);
  415. this->builder.CreateStore(this->gen_const(32U, std::numeric_limits<uint32_t>::max()), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  416. }
  417. this->builder.CreateBr(bbnext);
  418. bb=bbnext;
  419. }
  420. this->builder.SetInsertPoint(bb);
  421. this->gen_sync(POST_SYNC, 0);
  422. this->gen_trap_check(this->leave_blk);
  423. return std::make_tuple(BRANCH, nullptr);
  424. }
  425. /* instruction 1: C.ADDI4SPN */
  426. std::tuple<continuation_e, BasicBlock*> __c_addi4spn(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  427. bb->setName("C.ADDI4SPN");
  428. this->gen_sync(PRE_SYNC, 1);
  429. uint8_t rd = ((bit_sub<2,3>(instr)));
  430. uint16_t imm = ((bit_sub<5,1>(instr) << 3) | (bit_sub<6,1>(instr) << 2) | (bit_sub<7,4>(instr) << 6) | (bit_sub<11,2>(instr) << 4));
  431. if(this->disass_enabled){
  432. /* generate console output when executing the command */
  433. auto mnemonic = fmt::format(
  434. "{mnemonic:10} {rd}, {imm:#05x}", fmt::arg("mnemonic", "c.addi4spn"),
  435. fmt::arg("rd", name(rd)), fmt::arg("imm", imm));
  436. std::vector<Value*> args {
  437. this->core_ptr,
  438. this->gen_const(64, pc.val),
  439. this->builder.CreateGlobalStringPtr(mnemonic),
  440. };
  441. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  442. }
  443. Value* cur_pc_val = this->gen_const(32, pc.val);
  444. pc=pc+2;
  445. if(imm == 0){
  446. this->gen_raise_trap(0, 2);
  447. }
  448. Value* Xtmp0_val = this->builder.CreateAdd(
  449. this->gen_reg_load(2 + traits<ARCH>::X0, 0),
  450. this->gen_const(32U, imm));
  451. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + 8 + traits<ARCH>::X0), false);
  452. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  453. this->gen_sync(POST_SYNC, 1);
  454. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  455. this->gen_trap_check(bb);
  456. return std::make_tuple(CONT, bb);
  457. }
  458. /* instruction 2: C.LW */
  459. std::tuple<continuation_e, BasicBlock*> __c_lw(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  460. bb->setName("C.LW");
  461. this->gen_sync(PRE_SYNC, 2);
  462. uint8_t rd = ((bit_sub<2,3>(instr)));
  463. uint8_t uimm = ((bit_sub<5,1>(instr) << 6) | (bit_sub<6,1>(instr) << 2) | (bit_sub<10,3>(instr) << 3));
  464. uint8_t rs1 = ((bit_sub<7,3>(instr)));
  465. if(this->disass_enabled){
  466. /* generate console output when executing the command */
  467. auto mnemonic = fmt::format(
  468. "{mnemonic:10} {rd}, {uimm:#05x}({rs1})", fmt::arg("mnemonic", "c.lw"),
  469. fmt::arg("rd", name(8+rd)), fmt::arg("uimm", uimm), fmt::arg("rs1", name(8+rs1)));
  470. std::vector<Value*> args {
  471. this->core_ptr,
  472. this->gen_const(64, pc.val),
  473. this->builder.CreateGlobalStringPtr(mnemonic),
  474. };
  475. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  476. }
  477. Value* cur_pc_val = this->gen_const(32, pc.val);
  478. pc=pc+2;
  479. Value* offs_val = this->builder.CreateAdd(
  480. this->gen_reg_load(rs1 + 8 + traits<ARCH>::X0, 0),
  481. this->gen_const(32U, uimm));
  482. Value* Xtmp0_val = this->gen_ext(
  483. this->gen_read_mem(traits<ARCH>::MEM, offs_val, 32/8),
  484. 32,
  485. true);
  486. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + 8 + traits<ARCH>::X0), false);
  487. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  488. this->gen_sync(POST_SYNC, 2);
  489. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  490. this->gen_trap_check(bb);
  491. return std::make_tuple(CONT, bb);
  492. }
  493. /* instruction 3: C.SW */
  494. std::tuple<continuation_e, BasicBlock*> __c_sw(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  495. bb->setName("C.SW");
  496. this->gen_sync(PRE_SYNC, 3);
  497. uint8_t rs2 = ((bit_sub<2,3>(instr)));
  498. uint8_t uimm = ((bit_sub<5,1>(instr) << 6) | (bit_sub<6,1>(instr) << 2) | (bit_sub<10,3>(instr) << 3));
  499. uint8_t rs1 = ((bit_sub<7,3>(instr)));
  500. if(this->disass_enabled){
  501. /* generate console output when executing the command */
  502. auto mnemonic = fmt::format(
  503. "{mnemonic:10} {rs2}, {uimm:#05x}({rs1})", fmt::arg("mnemonic", "c.sw"),
  504. fmt::arg("rs2", name(8+rs2)), fmt::arg("uimm", uimm), fmt::arg("rs1", name(8+rs1)));
  505. std::vector<Value*> args {
  506. this->core_ptr,
  507. this->gen_const(64, pc.val),
  508. this->builder.CreateGlobalStringPtr(mnemonic),
  509. };
  510. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  511. }
  512. Value* cur_pc_val = this->gen_const(32, pc.val);
  513. pc=pc+2;
  514. Value* offs_val = this->builder.CreateAdd(
  515. this->gen_reg_load(rs1 + 8 + traits<ARCH>::X0, 0),
  516. this->gen_const(32U, uimm));
  517. Value* MEMtmp0_val = this->gen_reg_load(rs2 + 8 + traits<ARCH>::X0, 0);
  518. this->gen_write_mem(
  519. traits<ARCH>::MEM,
  520. offs_val,
  521. this->builder.CreateZExtOrTrunc(MEMtmp0_val,this->get_type(32)));
  522. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  523. this->gen_sync(POST_SYNC, 3);
  524. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  525. this->gen_trap_check(bb);
  526. return std::make_tuple(CONT, bb);
  527. }
  528. /* instruction 4: C.ADDI */
  529. std::tuple<continuation_e, BasicBlock*> __c_addi(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  530. bb->setName("C.ADDI");
  531. this->gen_sync(PRE_SYNC, 4);
  532. int8_t imm = signextend<int8_t,6>((bit_sub<2,5>(instr)) | (bit_sub<12,1>(instr) << 5));
  533. uint8_t rs1 = ((bit_sub<7,5>(instr)));
  534. if(this->disass_enabled){
  535. /* generate console output when executing the command */
  536. auto mnemonic = fmt::format(
  537. "{mnemonic:10} {rs1}, {imm:#05x}", fmt::arg("mnemonic", "c.addi"),
  538. fmt::arg("rs1", name(rs1)), fmt::arg("imm", imm));
  539. std::vector<Value*> args {
  540. this->core_ptr,
  541. this->gen_const(64, pc.val),
  542. this->builder.CreateGlobalStringPtr(mnemonic),
  543. };
  544. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  545. }
  546. Value* cur_pc_val = this->gen_const(32, pc.val);
  547. pc=pc+2;
  548. Value* Xtmp0_val = this->builder.CreateAdd(
  549. this->gen_ext(
  550. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  551. 32, true),
  552. this->gen_const(32U, imm));
  553. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rs1 + traits<ARCH>::X0), false);
  554. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  555. this->gen_sync(POST_SYNC, 4);
  556. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  557. this->gen_trap_check(bb);
  558. return std::make_tuple(CONT, bb);
  559. }
  560. /* instruction 5: C.NOP */
  561. std::tuple<continuation_e, BasicBlock*> __c_nop(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  562. bb->setName("C.NOP");
  563. this->gen_sync(PRE_SYNC, 5);
  564. if(this->disass_enabled){
  565. /* generate console output when executing the command */
  566. std::vector<Value*> args {
  567. this->core_ptr,
  568. this->gen_const(64, pc.val),
  569. this->builder.CreateGlobalStringPtr("c.nop"),
  570. };
  571. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  572. }
  573. Value* cur_pc_val = this->gen_const(32, pc.val);
  574. pc=pc+2;
  575. /* TODO: describe operations for C.NOP ! */
  576. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  577. this->gen_sync(POST_SYNC, 5);
  578. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  579. this->gen_trap_check(bb);
  580. return std::make_tuple(CONT, bb);
  581. }
  582. /* instruction 6: C.JAL */
  583. std::tuple<continuation_e, BasicBlock*> __c_jal(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  584. bb->setName("C.JAL");
  585. this->gen_sync(PRE_SYNC, 6);
  586. int16_t imm = signextend<int16_t,12>((bit_sub<2,1>(instr) << 5) | (bit_sub<3,3>(instr) << 1) | (bit_sub<6,1>(instr) << 7) | (bit_sub<7,1>(instr) << 6) | (bit_sub<8,1>(instr) << 10) | (bit_sub<9,2>(instr) << 8) | (bit_sub<11,1>(instr) << 4) | (bit_sub<12,1>(instr) << 11));
  587. if(this->disass_enabled){
  588. /* generate console output when executing the command */
  589. auto mnemonic = fmt::format(
  590. "{mnemonic:10} {imm:#05x}", fmt::arg("mnemonic", "c.jal"),
  591. fmt::arg("imm", imm));
  592. std::vector<Value*> args {
  593. this->core_ptr,
  594. this->gen_const(64, pc.val),
  595. this->builder.CreateGlobalStringPtr(mnemonic),
  596. };
  597. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  598. }
  599. Value* cur_pc_val = this->gen_const(32, pc.val);
  600. pc=pc+2;
  601. Value* Xtmp0_val = this->builder.CreateAdd(
  602. cur_pc_val,
  603. this->gen_const(32U, 2));
  604. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(1 + traits<ARCH>::X0), false);
  605. Value* PC_val = this->builder.CreateAdd(
  606. this->gen_ext(
  607. cur_pc_val,
  608. 32, true),
  609. this->gen_const(32U, imm));
  610. this->builder.CreateStore(PC_val, get_reg_ptr(traits<ARCH>::NEXT_PC), false);
  611. Value* is_cont_v = this->builder.CreateICmp(ICmpInst::ICMP_NE, PC_val, this->gen_const(32U, pc.val), "is_cont_v");
  612. this->builder.CreateStore(this->gen_ext(is_cont_v, 32U, false), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  613. this->gen_sync(POST_SYNC, 6);
  614. this->gen_trap_check(this->leave_blk);
  615. return std::make_tuple(BRANCH, nullptr);
  616. }
  617. /* instruction 7: C.LI */
  618. std::tuple<continuation_e, BasicBlock*> __c_li(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  619. bb->setName("C.LI");
  620. this->gen_sync(PRE_SYNC, 7);
  621. int8_t imm = signextend<int8_t,6>((bit_sub<2,5>(instr)) | (bit_sub<12,1>(instr) << 5));
  622. uint8_t rd = ((bit_sub<7,5>(instr)));
  623. if(this->disass_enabled){
  624. /* generate console output when executing the command */
  625. auto mnemonic = fmt::format(
  626. "{mnemonic:10} {rd}, {imm:#05x}", fmt::arg("mnemonic", "c.li"),
  627. fmt::arg("rd", name(rd)), fmt::arg("imm", imm));
  628. std::vector<Value*> args {
  629. this->core_ptr,
  630. this->gen_const(64, pc.val),
  631. this->builder.CreateGlobalStringPtr(mnemonic),
  632. };
  633. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  634. }
  635. Value* cur_pc_val = this->gen_const(32, pc.val);
  636. pc=pc+2;
  637. if(rd == 0){
  638. this->gen_raise_trap(0, 2);
  639. }
  640. Value* Xtmp0_val = this->gen_const(32U, imm);
  641. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  642. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  643. this->gen_sync(POST_SYNC, 7);
  644. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  645. this->gen_trap_check(bb);
  646. return std::make_tuple(CONT, bb);
  647. }
  648. /* instruction 8: C.LUI */
  649. std::tuple<continuation_e, BasicBlock*> __c_lui(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  650. bb->setName("C.LUI");
  651. this->gen_sync(PRE_SYNC, 8);
  652. int32_t imm = signextend<int32_t,18>((bit_sub<2,5>(instr) << 12) | (bit_sub<12,1>(instr) << 17));
  653. uint8_t rd = ((bit_sub<7,5>(instr)));
  654. if(this->disass_enabled){
  655. /* generate console output when executing the command */
  656. auto mnemonic = fmt::format(
  657. "{mnemonic:10} {rd}, {imm:#05x}", fmt::arg("mnemonic", "c.lui"),
  658. fmt::arg("rd", name(rd)), fmt::arg("imm", imm));
  659. std::vector<Value*> args {
  660. this->core_ptr,
  661. this->gen_const(64, pc.val),
  662. this->builder.CreateGlobalStringPtr(mnemonic),
  663. };
  664. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  665. }
  666. Value* cur_pc_val = this->gen_const(32, pc.val);
  667. pc=pc+2;
  668. if(rd == 0){
  669. this->gen_raise_trap(0, 2);
  670. }
  671. if(imm == 0){
  672. this->gen_raise_trap(0, 2);
  673. }
  674. Value* Xtmp0_val = this->gen_const(32U, imm);
  675. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  676. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  677. this->gen_sync(POST_SYNC, 8);
  678. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  679. this->gen_trap_check(bb);
  680. return std::make_tuple(CONT, bb);
  681. }
  682. /* instruction 9: C.ADDI16SP */
  683. std::tuple<continuation_e, BasicBlock*> __c_addi16sp(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  684. bb->setName("C.ADDI16SP");
  685. this->gen_sync(PRE_SYNC, 9);
  686. int16_t imm = signextend<int16_t,10>((bit_sub<2,1>(instr) << 5) | (bit_sub<3,2>(instr) << 7) | (bit_sub<5,1>(instr) << 6) | (bit_sub<6,1>(instr) << 4) | (bit_sub<12,1>(instr) << 9));
  687. if(this->disass_enabled){
  688. /* generate console output when executing the command */
  689. auto mnemonic = fmt::format(
  690. "{mnemonic:10} {imm:#05x}", fmt::arg("mnemonic", "c.addi16sp"),
  691. fmt::arg("imm", imm));
  692. std::vector<Value*> args {
  693. this->core_ptr,
  694. this->gen_const(64, pc.val),
  695. this->builder.CreateGlobalStringPtr(mnemonic),
  696. };
  697. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  698. }
  699. Value* cur_pc_val = this->gen_const(32, pc.val);
  700. pc=pc+2;
  701. Value* Xtmp0_val = this->builder.CreateAdd(
  702. this->gen_ext(
  703. this->gen_reg_load(2 + traits<ARCH>::X0, 0),
  704. 32, true),
  705. this->gen_const(32U, imm));
  706. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(2 + traits<ARCH>::X0), false);
  707. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  708. this->gen_sync(POST_SYNC, 9);
  709. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  710. this->gen_trap_check(bb);
  711. return std::make_tuple(CONT, bb);
  712. }
  713. /* instruction 10: C.SRLI */
  714. std::tuple<continuation_e, BasicBlock*> __c_srli(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  715. bb->setName("C.SRLI");
  716. this->gen_sync(PRE_SYNC, 10);
  717. uint8_t shamt = ((bit_sub<2,5>(instr)));
  718. uint8_t rs1 = ((bit_sub<7,3>(instr)));
  719. if(this->disass_enabled){
  720. /* generate console output when executing the command */
  721. auto mnemonic = fmt::format(
  722. "{mnemonic:10} {rs1}, {shamt}", fmt::arg("mnemonic", "c.srli"),
  723. fmt::arg("rs1", name(8+rs1)), fmt::arg("shamt", shamt));
  724. std::vector<Value*> args {
  725. this->core_ptr,
  726. this->gen_const(64, pc.val),
  727. this->builder.CreateGlobalStringPtr(mnemonic),
  728. };
  729. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  730. }
  731. Value* cur_pc_val = this->gen_const(32, pc.val);
  732. pc=pc+2;
  733. uint8_t rs1_idx_val = rs1 + 8;
  734. Value* Xtmp0_val = this->builder.CreateLShr(
  735. this->gen_reg_load(rs1_idx_val + traits<ARCH>::X0, 0),
  736. this->gen_const(32U, shamt));
  737. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rs1_idx_val + traits<ARCH>::X0), false);
  738. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  739. this->gen_sync(POST_SYNC, 10);
  740. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  741. this->gen_trap_check(bb);
  742. return std::make_tuple(CONT, bb);
  743. }
  744. /* instruction 11: C.SRAI */
  745. std::tuple<continuation_e, BasicBlock*> __c_srai(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  746. bb->setName("C.SRAI");
  747. this->gen_sync(PRE_SYNC, 11);
  748. uint8_t shamt = ((bit_sub<2,5>(instr)));
  749. uint8_t rs1 = ((bit_sub<7,3>(instr)));
  750. if(this->disass_enabled){
  751. /* generate console output when executing the command */
  752. auto mnemonic = fmt::format(
  753. "{mnemonic:10} {rs1}, {shamt}", fmt::arg("mnemonic", "c.srai"),
  754. fmt::arg("rs1", name(8+rs1)), fmt::arg("shamt", shamt));
  755. std::vector<Value*> args {
  756. this->core_ptr,
  757. this->gen_const(64, pc.val),
  758. this->builder.CreateGlobalStringPtr(mnemonic),
  759. };
  760. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  761. }
  762. Value* cur_pc_val = this->gen_const(32, pc.val);
  763. pc=pc+2;
  764. uint8_t rs1_idx_val = rs1 + 8;
  765. Value* Xtmp0_val = this->builder.CreateAShr(
  766. this->gen_reg_load(rs1_idx_val + traits<ARCH>::X0, 0),
  767. this->gen_const(32U, shamt));
  768. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rs1_idx_val + traits<ARCH>::X0), false);
  769. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  770. this->gen_sync(POST_SYNC, 11);
  771. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  772. this->gen_trap_check(bb);
  773. return std::make_tuple(CONT, bb);
  774. }
  775. /* instruction 12: C.ANDI */
  776. std::tuple<continuation_e, BasicBlock*> __c_andi(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  777. bb->setName("C.ANDI");
  778. this->gen_sync(PRE_SYNC, 12);
  779. int8_t imm = signextend<int8_t,6>((bit_sub<2,5>(instr)) | (bit_sub<12,1>(instr) << 5));
  780. uint8_t rs1 = ((bit_sub<7,3>(instr)));
  781. if(this->disass_enabled){
  782. /* generate console output when executing the command */
  783. auto mnemonic = fmt::format(
  784. "{mnemonic:10} {rs1}, {imm:#05x}", fmt::arg("mnemonic", "c.andi"),
  785. fmt::arg("rs1", name(8+rs1)), fmt::arg("imm", imm));
  786. std::vector<Value*> args {
  787. this->core_ptr,
  788. this->gen_const(64, pc.val),
  789. this->builder.CreateGlobalStringPtr(mnemonic),
  790. };
  791. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  792. }
  793. Value* cur_pc_val = this->gen_const(32, pc.val);
  794. pc=pc+2;
  795. uint8_t rs1_idx_val = rs1 + 8;
  796. Value* Xtmp0_val = this->builder.CreateAnd(
  797. this->gen_ext(
  798. this->gen_reg_load(rs1_idx_val + traits<ARCH>::X0, 0),
  799. 32, true),
  800. this->gen_const(32U, imm));
  801. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rs1_idx_val + traits<ARCH>::X0), false);
  802. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  803. this->gen_sync(POST_SYNC, 12);
  804. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  805. this->gen_trap_check(bb);
  806. return std::make_tuple(CONT, bb);
  807. }
  808. /* instruction 13: C.SUB */
  809. std::tuple<continuation_e, BasicBlock*> __c_sub(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  810. bb->setName("C.SUB");
  811. this->gen_sync(PRE_SYNC, 13);
  812. uint8_t rs2 = ((bit_sub<2,3>(instr)));
  813. uint8_t rd = ((bit_sub<7,3>(instr)));
  814. if(this->disass_enabled){
  815. /* generate console output when executing the command */
  816. auto mnemonic = fmt::format(
  817. "{mnemonic:10} {rd}, {rs2}", fmt::arg("mnemonic", "c.sub"),
  818. fmt::arg("rd", name(8+rd)), fmt::arg("rs2", name(8+rs2)));
  819. std::vector<Value*> args {
  820. this->core_ptr,
  821. this->gen_const(64, pc.val),
  822. this->builder.CreateGlobalStringPtr(mnemonic),
  823. };
  824. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  825. }
  826. Value* cur_pc_val = this->gen_const(32, pc.val);
  827. pc=pc+2;
  828. uint8_t rd_idx_val = rd + 8;
  829. Value* Xtmp0_val = this->builder.CreateSub(
  830. this->gen_reg_load(rd_idx_val + traits<ARCH>::X0, 0),
  831. this->gen_reg_load(rs2 + 8 + traits<ARCH>::X0, 0));
  832. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd_idx_val + traits<ARCH>::X0), false);
  833. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  834. this->gen_sync(POST_SYNC, 13);
  835. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  836. this->gen_trap_check(bb);
  837. return std::make_tuple(CONT, bb);
  838. }
  839. /* instruction 14: C.XOR */
  840. std::tuple<continuation_e, BasicBlock*> __c_xor(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  841. bb->setName("C.XOR");
  842. this->gen_sync(PRE_SYNC, 14);
  843. uint8_t rs2 = ((bit_sub<2,3>(instr)));
  844. uint8_t rd = ((bit_sub<7,3>(instr)));
  845. if(this->disass_enabled){
  846. /* generate console output when executing the command */
  847. auto mnemonic = fmt::format(
  848. "{mnemonic:10} {rd}, {rs2}", fmt::arg("mnemonic", "c.xor"),
  849. fmt::arg("rd", name(8+rd)), fmt::arg("rs2", name(8+rs2)));
  850. std::vector<Value*> args {
  851. this->core_ptr,
  852. this->gen_const(64, pc.val),
  853. this->builder.CreateGlobalStringPtr(mnemonic),
  854. };
  855. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  856. }
  857. Value* cur_pc_val = this->gen_const(32, pc.val);
  858. pc=pc+2;
  859. uint8_t rd_idx_val = rd + 8;
  860. Value* Xtmp0_val = this->builder.CreateXor(
  861. this->gen_reg_load(rd_idx_val + traits<ARCH>::X0, 0),
  862. this->gen_reg_load(rs2 + 8 + traits<ARCH>::X0, 0));
  863. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd_idx_val + traits<ARCH>::X0), false);
  864. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  865. this->gen_sync(POST_SYNC, 14);
  866. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  867. this->gen_trap_check(bb);
  868. return std::make_tuple(CONT, bb);
  869. }
  870. /* instruction 15: C.OR */
  871. std::tuple<continuation_e, BasicBlock*> __c_or(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  872. bb->setName("C.OR");
  873. this->gen_sync(PRE_SYNC, 15);
  874. uint8_t rs2 = ((bit_sub<2,3>(instr)));
  875. uint8_t rd = ((bit_sub<7,3>(instr)));
  876. if(this->disass_enabled){
  877. /* generate console output when executing the command */
  878. auto mnemonic = fmt::format(
  879. "{mnemonic:10} {rd}, {rs2}", fmt::arg("mnemonic", "c.or"),
  880. fmt::arg("rd", name(8+rd)), fmt::arg("rs2", name(8+rs2)));
  881. std::vector<Value*> args {
  882. this->core_ptr,
  883. this->gen_const(64, pc.val),
  884. this->builder.CreateGlobalStringPtr(mnemonic),
  885. };
  886. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  887. }
  888. Value* cur_pc_val = this->gen_const(32, pc.val);
  889. pc=pc+2;
  890. uint8_t rd_idx_val = rd + 8;
  891. Value* Xtmp0_val = this->builder.CreateOr(
  892. this->gen_reg_load(rd_idx_val + traits<ARCH>::X0, 0),
  893. this->gen_reg_load(rs2 + 8 + traits<ARCH>::X0, 0));
  894. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd_idx_val + traits<ARCH>::X0), false);
  895. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  896. this->gen_sync(POST_SYNC, 15);
  897. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  898. this->gen_trap_check(bb);
  899. return std::make_tuple(CONT, bb);
  900. }
  901. /* instruction 16: C.AND */
  902. std::tuple<continuation_e, BasicBlock*> __c_and(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  903. bb->setName("C.AND");
  904. this->gen_sync(PRE_SYNC, 16);
  905. uint8_t rs2 = ((bit_sub<2,3>(instr)));
  906. uint8_t rd = ((bit_sub<7,3>(instr)));
  907. if(this->disass_enabled){
  908. /* generate console output when executing the command */
  909. auto mnemonic = fmt::format(
  910. "{mnemonic:10} {rd}, {rs2}", fmt::arg("mnemonic", "c.and"),
  911. fmt::arg("rd", name(8+rd)), fmt::arg("rs2", name(8+rs2)));
  912. std::vector<Value*> args {
  913. this->core_ptr,
  914. this->gen_const(64, pc.val),
  915. this->builder.CreateGlobalStringPtr(mnemonic),
  916. };
  917. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  918. }
  919. Value* cur_pc_val = this->gen_const(32, pc.val);
  920. pc=pc+2;
  921. uint8_t rd_idx_val = rd + 8;
  922. Value* Xtmp0_val = this->builder.CreateAnd(
  923. this->gen_reg_load(rd_idx_val + traits<ARCH>::X0, 0),
  924. this->gen_reg_load(rs2 + 8 + traits<ARCH>::X0, 0));
  925. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd_idx_val + traits<ARCH>::X0), false);
  926. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  927. this->gen_sync(POST_SYNC, 16);
  928. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  929. this->gen_trap_check(bb);
  930. return std::make_tuple(CONT, bb);
  931. }
  932. /* instruction 17: C.J */
  933. std::tuple<continuation_e, BasicBlock*> __c_j(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  934. bb->setName("C.J");
  935. this->gen_sync(PRE_SYNC, 17);
  936. int16_t imm = signextend<int16_t,12>((bit_sub<2,1>(instr) << 5) | (bit_sub<3,3>(instr) << 1) | (bit_sub<6,1>(instr) << 7) | (bit_sub<7,1>(instr) << 6) | (bit_sub<8,1>(instr) << 10) | (bit_sub<9,2>(instr) << 8) | (bit_sub<11,1>(instr) << 4) | (bit_sub<12,1>(instr) << 11));
  937. if(this->disass_enabled){
  938. /* generate console output when executing the command */
  939. auto mnemonic = fmt::format(
  940. "{mnemonic:10} {imm:#05x}", fmt::arg("mnemonic", "c.j"),
  941. fmt::arg("imm", imm));
  942. std::vector<Value*> args {
  943. this->core_ptr,
  944. this->gen_const(64, pc.val),
  945. this->builder.CreateGlobalStringPtr(mnemonic),
  946. };
  947. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  948. }
  949. Value* cur_pc_val = this->gen_const(32, pc.val);
  950. pc=pc+2;
  951. Value* PC_val = this->builder.CreateAdd(
  952. this->gen_ext(
  953. cur_pc_val,
  954. 32, true),
  955. this->gen_const(32U, imm));
  956. this->builder.CreateStore(PC_val, get_reg_ptr(traits<ARCH>::NEXT_PC), false);
  957. Value* is_cont_v = this->builder.CreateICmp(ICmpInst::ICMP_NE, PC_val, this->gen_const(32U, pc.val), "is_cont_v");
  958. this->builder.CreateStore(this->gen_ext(is_cont_v, 32U, false), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  959. this->gen_sync(POST_SYNC, 17);
  960. this->gen_trap_check(this->leave_blk);
  961. return std::make_tuple(BRANCH, nullptr);
  962. }
  963. /* instruction 18: C.BEQZ */
  964. std::tuple<continuation_e, BasicBlock*> __c_beqz(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  965. bb->setName("C.BEQZ");
  966. this->gen_sync(PRE_SYNC, 18);
  967. int16_t imm = signextend<int16_t,9>((bit_sub<2,1>(instr) << 5) | (bit_sub<3,2>(instr) << 1) | (bit_sub<5,2>(instr) << 6) | (bit_sub<10,2>(instr) << 3) | (bit_sub<12,1>(instr) << 8));
  968. uint8_t rs1 = ((bit_sub<7,3>(instr)));
  969. if(this->disass_enabled){
  970. /* generate console output when executing the command */
  971. auto mnemonic = fmt::format(
  972. "{mnemonic:10} {rs1}, {imm:#05x}", fmt::arg("mnemonic", "c.beqz"),
  973. fmt::arg("rs1", name(8+rs1)), fmt::arg("imm", imm));
  974. std::vector<Value*> args {
  975. this->core_ptr,
  976. this->gen_const(64, pc.val),
  977. this->builder.CreateGlobalStringPtr(mnemonic),
  978. };
  979. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  980. }
  981. Value* cur_pc_val = this->gen_const(32, pc.val);
  982. pc=pc+2;
  983. Value* PC_val = this->gen_choose(
  984. this->builder.CreateICmp(
  985. ICmpInst::ICMP_EQ,
  986. this->gen_reg_load(rs1 + 8 + traits<ARCH>::X0, 0),
  987. this->gen_const(32U, 0)),
  988. this->builder.CreateAdd(
  989. this->gen_ext(
  990. cur_pc_val,
  991. 32, true),
  992. this->gen_const(32U, imm)),
  993. this->builder.CreateAdd(
  994. cur_pc_val,
  995. this->gen_const(32U, 2)),
  996. 32);
  997. this->builder.CreateStore(PC_val, get_reg_ptr(traits<ARCH>::NEXT_PC), false);
  998. Value* is_cont_v = this->builder.CreateICmp(ICmpInst::ICMP_NE, PC_val, this->gen_const(32U, pc.val), "is_cont_v");
  999. this->builder.CreateStore(this->gen_ext(is_cont_v, 32U, false), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  1000. this->gen_sync(POST_SYNC, 18);
  1001. this->gen_trap_check(this->leave_blk);
  1002. return std::make_tuple(BRANCH, nullptr);
  1003. }
  1004. /* instruction 19: C.BNEZ */
  1005. std::tuple<continuation_e, BasicBlock*> __c_bnez(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1006. bb->setName("C.BNEZ");
  1007. this->gen_sync(PRE_SYNC, 19);
  1008. int16_t imm = signextend<int16_t,9>((bit_sub<2,1>(instr) << 5) | (bit_sub<3,2>(instr) << 1) | (bit_sub<5,2>(instr) << 6) | (bit_sub<10,2>(instr) << 3) | (bit_sub<12,1>(instr) << 8));
  1009. uint8_t rs1 = ((bit_sub<7,3>(instr)));
  1010. if(this->disass_enabled){
  1011. /* generate console output when executing the command */
  1012. auto mnemonic = fmt::format(
  1013. "{mnemonic:10} {rs1}, {imm:#05x}", fmt::arg("mnemonic", "c.bnez"),
  1014. fmt::arg("rs1", name(8+rs1)), fmt::arg("imm", imm));
  1015. std::vector<Value*> args {
  1016. this->core_ptr,
  1017. this->gen_const(64, pc.val),
  1018. this->builder.CreateGlobalStringPtr(mnemonic),
  1019. };
  1020. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1021. }
  1022. Value* cur_pc_val = this->gen_const(32, pc.val);
  1023. pc=pc+2;
  1024. Value* PC_val = this->gen_choose(
  1025. this->builder.CreateICmp(
  1026. ICmpInst::ICMP_NE,
  1027. this->gen_reg_load(rs1 + 8 + traits<ARCH>::X0, 0),
  1028. this->gen_const(32U, 0)),
  1029. this->builder.CreateAdd(
  1030. this->gen_ext(
  1031. cur_pc_val,
  1032. 32, true),
  1033. this->gen_const(32U, imm)),
  1034. this->builder.CreateAdd(
  1035. cur_pc_val,
  1036. this->gen_const(32U, 2)),
  1037. 32);
  1038. this->builder.CreateStore(PC_val, get_reg_ptr(traits<ARCH>::NEXT_PC), false);
  1039. Value* is_cont_v = this->builder.CreateICmp(ICmpInst::ICMP_NE, PC_val, this->gen_const(32U, pc.val), "is_cont_v");
  1040. this->builder.CreateStore(this->gen_ext(is_cont_v, 32U, false), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  1041. this->gen_sync(POST_SYNC, 19);
  1042. this->gen_trap_check(this->leave_blk);
  1043. return std::make_tuple(BRANCH, nullptr);
  1044. }
  1045. /* instruction 20: C.SLLI */
  1046. std::tuple<continuation_e, BasicBlock*> __c_slli(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1047. bb->setName("C.SLLI");
  1048. this->gen_sync(PRE_SYNC, 20);
  1049. uint8_t shamt = ((bit_sub<2,5>(instr)));
  1050. uint8_t rs1 = ((bit_sub<7,5>(instr)));
  1051. if(this->disass_enabled){
  1052. /* generate console output when executing the command */
  1053. auto mnemonic = fmt::format(
  1054. "{mnemonic:10} {rs1}, {shamt}", fmt::arg("mnemonic", "c.slli"),
  1055. fmt::arg("rs1", name(rs1)), fmt::arg("shamt", shamt));
  1056. std::vector<Value*> args {
  1057. this->core_ptr,
  1058. this->gen_const(64, pc.val),
  1059. this->builder.CreateGlobalStringPtr(mnemonic),
  1060. };
  1061. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1062. }
  1063. Value* cur_pc_val = this->gen_const(32, pc.val);
  1064. pc=pc+2;
  1065. if(rs1 == 0){
  1066. this->gen_raise_trap(0, 2);
  1067. }
  1068. Value* Xtmp0_val = this->builder.CreateShl(
  1069. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  1070. this->gen_const(32U, shamt));
  1071. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rs1 + traits<ARCH>::X0), false);
  1072. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1073. this->gen_sync(POST_SYNC, 20);
  1074. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1075. this->gen_trap_check(bb);
  1076. return std::make_tuple(CONT, bb);
  1077. }
  1078. /* instruction 21: C.LWSP */
  1079. std::tuple<continuation_e, BasicBlock*> __c_lwsp(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1080. bb->setName("C.LWSP");
  1081. this->gen_sync(PRE_SYNC, 21);
  1082. uint8_t uimm = ((bit_sub<2,2>(instr) << 6) | (bit_sub<4,3>(instr) << 2) | (bit_sub<12,1>(instr) << 5));
  1083. uint8_t rd = ((bit_sub<7,5>(instr)));
  1084. if(this->disass_enabled){
  1085. /* generate console output when executing the command */
  1086. auto mnemonic = fmt::format(
  1087. "{mnemonic:10} {rd}, sp, {uimm:#05x}", fmt::arg("mnemonic", "c.lwsp"),
  1088. fmt::arg("rd", name(rd)), fmt::arg("uimm", uimm));
  1089. std::vector<Value*> args {
  1090. this->core_ptr,
  1091. this->gen_const(64, pc.val),
  1092. this->builder.CreateGlobalStringPtr(mnemonic),
  1093. };
  1094. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1095. }
  1096. Value* cur_pc_val = this->gen_const(32, pc.val);
  1097. pc=pc+2;
  1098. Value* offs_val = this->builder.CreateAdd(
  1099. this->gen_reg_load(2 + traits<ARCH>::X0, 0),
  1100. this->gen_const(32U, uimm));
  1101. Value* Xtmp0_val = this->gen_ext(
  1102. this->gen_read_mem(traits<ARCH>::MEM, offs_val, 32/8),
  1103. 32,
  1104. true);
  1105. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1106. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1107. this->gen_sync(POST_SYNC, 21);
  1108. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1109. this->gen_trap_check(bb);
  1110. return std::make_tuple(CONT, bb);
  1111. }
  1112. /* instruction 22: C.MV */
  1113. std::tuple<continuation_e, BasicBlock*> __c_mv(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1114. bb->setName("C.MV");
  1115. this->gen_sync(PRE_SYNC, 22);
  1116. uint8_t rs2 = ((bit_sub<2,5>(instr)));
  1117. uint8_t rd = ((bit_sub<7,5>(instr)));
  1118. if(this->disass_enabled){
  1119. /* generate console output when executing the command */
  1120. auto mnemonic = fmt::format(
  1121. "{mnemonic:10} {rd}, {rs2}", fmt::arg("mnemonic", "c.mv"),
  1122. fmt::arg("rd", name(rd)), fmt::arg("rs2", name(rs2)));
  1123. std::vector<Value*> args {
  1124. this->core_ptr,
  1125. this->gen_const(64, pc.val),
  1126. this->builder.CreateGlobalStringPtr(mnemonic),
  1127. };
  1128. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1129. }
  1130. Value* cur_pc_val = this->gen_const(32, pc.val);
  1131. pc=pc+2;
  1132. Value* Xtmp0_val = this->gen_reg_load(rs2 + traits<ARCH>::X0, 0);
  1133. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1134. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1135. this->gen_sync(POST_SYNC, 22);
  1136. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1137. this->gen_trap_check(bb);
  1138. return std::make_tuple(CONT, bb);
  1139. }
  1140. /* instruction 23: C.JR */
  1141. std::tuple<continuation_e, BasicBlock*> __c_jr(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1142. bb->setName("C.JR");
  1143. this->gen_sync(PRE_SYNC, 23);
  1144. uint8_t rs1 = ((bit_sub<7,5>(instr)));
  1145. if(this->disass_enabled){
  1146. /* generate console output when executing the command */
  1147. auto mnemonic = fmt::format(
  1148. "{mnemonic:10} {rs1}", fmt::arg("mnemonic", "c.jr"),
  1149. fmt::arg("rs1", name(rs1)));
  1150. std::vector<Value*> args {
  1151. this->core_ptr,
  1152. this->gen_const(64, pc.val),
  1153. this->builder.CreateGlobalStringPtr(mnemonic),
  1154. };
  1155. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1156. }
  1157. Value* cur_pc_val = this->gen_const(32, pc.val);
  1158. pc=pc+2;
  1159. Value* PC_val = this->gen_reg_load(rs1 + traits<ARCH>::X0, 0);
  1160. this->builder.CreateStore(PC_val, get_reg_ptr(traits<ARCH>::NEXT_PC), false);
  1161. this->builder.CreateStore(this->gen_const(32U, std::numeric_limits<uint32_t>::max()), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  1162. this->gen_sync(POST_SYNC, 23);
  1163. this->gen_trap_check(this->leave_blk);
  1164. return std::make_tuple(BRANCH, nullptr);
  1165. }
  1166. /* instruction 24: C.ADD */
  1167. std::tuple<continuation_e, BasicBlock*> __c_add(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1168. bb->setName("C.ADD");
  1169. this->gen_sync(PRE_SYNC, 24);
  1170. uint8_t rs2 = ((bit_sub<2,5>(instr)));
  1171. uint8_t rd = ((bit_sub<7,5>(instr)));
  1172. if(this->disass_enabled){
  1173. /* generate console output when executing the command */
  1174. auto mnemonic = fmt::format(
  1175. "{mnemonic:10} {rd}, {rs2}", fmt::arg("mnemonic", "c.add"),
  1176. fmt::arg("rd", name(rd)), fmt::arg("rs2", name(rs2)));
  1177. std::vector<Value*> args {
  1178. this->core_ptr,
  1179. this->gen_const(64, pc.val),
  1180. this->builder.CreateGlobalStringPtr(mnemonic),
  1181. };
  1182. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1183. }
  1184. Value* cur_pc_val = this->gen_const(32, pc.val);
  1185. pc=pc+2;
  1186. Value* Xtmp0_val = this->builder.CreateAdd(
  1187. this->gen_reg_load(rd + traits<ARCH>::X0, 0),
  1188. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0));
  1189. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1190. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1191. this->gen_sync(POST_SYNC, 24);
  1192. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1193. this->gen_trap_check(bb);
  1194. return std::make_tuple(CONT, bb);
  1195. }
  1196. /* instruction 25: C.JALR */
  1197. std::tuple<continuation_e, BasicBlock*> __c_jalr(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1198. bb->setName("C.JALR");
  1199. this->gen_sync(PRE_SYNC, 25);
  1200. uint8_t rs1 = ((bit_sub<7,5>(instr)));
  1201. if(this->disass_enabled){
  1202. /* generate console output when executing the command */
  1203. auto mnemonic = fmt::format(
  1204. "{mnemonic:10} {rs1}", fmt::arg("mnemonic", "c.jalr"),
  1205. fmt::arg("rs1", name(rs1)));
  1206. std::vector<Value*> args {
  1207. this->core_ptr,
  1208. this->gen_const(64, pc.val),
  1209. this->builder.CreateGlobalStringPtr(mnemonic),
  1210. };
  1211. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1212. }
  1213. Value* cur_pc_val = this->gen_const(32, pc.val);
  1214. pc=pc+2;
  1215. Value* Xtmp0_val = this->builder.CreateAdd(
  1216. cur_pc_val,
  1217. this->gen_const(32U, 2));
  1218. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(1 + traits<ARCH>::X0), false);
  1219. Value* PC_val = this->gen_reg_load(rs1 + traits<ARCH>::X0, 0);
  1220. this->builder.CreateStore(PC_val, get_reg_ptr(traits<ARCH>::NEXT_PC), false);
  1221. this->builder.CreateStore(this->gen_const(32U, std::numeric_limits<uint32_t>::max()), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  1222. this->gen_sync(POST_SYNC, 25);
  1223. this->gen_trap_check(this->leave_blk);
  1224. return std::make_tuple(BRANCH, nullptr);
  1225. }
  1226. /* instruction 26: C.EBREAK */
  1227. std::tuple<continuation_e, BasicBlock*> __c_ebreak(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1228. bb->setName("C.EBREAK");
  1229. this->gen_sync(PRE_SYNC, 26);
  1230. if(this->disass_enabled){
  1231. /* generate console output when executing the command */
  1232. std::vector<Value*> args {
  1233. this->core_ptr,
  1234. this->gen_const(64, pc.val),
  1235. this->builder.CreateGlobalStringPtr("c.ebreak"),
  1236. };
  1237. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1238. }
  1239. Value* cur_pc_val = this->gen_const(32, pc.val);
  1240. pc=pc+2;
  1241. this->gen_raise_trap(0, 3);
  1242. this->gen_sync(POST_SYNC, 26);
  1243. this->gen_trap_check(this->leave_blk);
  1244. return std::make_tuple(BRANCH, nullptr);
  1245. }
  1246. /* instruction 27: C.SWSP */
  1247. std::tuple<continuation_e, BasicBlock*> __c_swsp(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1248. bb->setName("C.SWSP");
  1249. this->gen_sync(PRE_SYNC, 27);
  1250. uint8_t rs2 = ((bit_sub<2,5>(instr)));
  1251. uint8_t uimm = ((bit_sub<7,2>(instr) << 6) | (bit_sub<9,4>(instr) << 2));
  1252. if(this->disass_enabled){
  1253. /* generate console output when executing the command */
  1254. auto mnemonic = fmt::format(
  1255. "{mnemonic:10} {rs2}, {uimm:#05x}(sp)", fmt::arg("mnemonic", "c.swsp"),
  1256. fmt::arg("rs2", name(rs2)), fmt::arg("uimm", uimm));
  1257. std::vector<Value*> args {
  1258. this->core_ptr,
  1259. this->gen_const(64, pc.val),
  1260. this->builder.CreateGlobalStringPtr(mnemonic),
  1261. };
  1262. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1263. }
  1264. Value* cur_pc_val = this->gen_const(32, pc.val);
  1265. pc=pc+2;
  1266. Value* offs_val = this->builder.CreateAdd(
  1267. this->gen_reg_load(2 + traits<ARCH>::X0, 0),
  1268. this->gen_const(32U, uimm));
  1269. Value* MEMtmp0_val = this->gen_reg_load(rs2 + traits<ARCH>::X0, 0);
  1270. this->gen_write_mem(
  1271. traits<ARCH>::MEM,
  1272. offs_val,
  1273. this->builder.CreateZExtOrTrunc(MEMtmp0_val,this->get_type(32)));
  1274. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1275. this->gen_sync(POST_SYNC, 27);
  1276. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1277. this->gen_trap_check(bb);
  1278. return std::make_tuple(CONT, bb);
  1279. }
  1280. /* instruction 28: DII */
  1281. std::tuple<continuation_e, BasicBlock*> __dii(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1282. bb->setName("DII");
  1283. this->gen_sync(PRE_SYNC, 28);
  1284. if(this->disass_enabled){
  1285. /* generate console output when executing the command */
  1286. std::vector<Value*> args {
  1287. this->core_ptr,
  1288. this->gen_const(64, pc.val),
  1289. this->builder.CreateGlobalStringPtr("dii"),
  1290. };
  1291. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1292. }
  1293. Value* cur_pc_val = this->gen_const(32, pc.val);
  1294. pc=pc+2;
  1295. this->gen_raise_trap(0, 2);
  1296. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1297. this->gen_sync(POST_SYNC, 28);
  1298. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1299. this->gen_trap_check(bb);
  1300. return std::make_tuple(CONT, bb);
  1301. }
  1302. /* instruction 29: LR.W */
  1303. std::tuple<continuation_e, BasicBlock*> __lr_w(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1304. bb->setName("LR.W");
  1305. this->gen_sync(PRE_SYNC, 29);
  1306. uint8_t rd = ((bit_sub<7,5>(instr)));
  1307. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1308. uint8_t rl = ((bit_sub<25,1>(instr)));
  1309. uint8_t aq = ((bit_sub<26,1>(instr)));
  1310. if(this->disass_enabled){
  1311. /* generate console output when executing the command */
  1312. auto mnemonic = fmt::format(
  1313. "{mnemonic:10} {rd}, {rs1}", fmt::arg("mnemonic", "lr.w"),
  1314. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)));
  1315. std::vector<Value*> args {
  1316. this->core_ptr,
  1317. this->gen_const(64, pc.val),
  1318. this->builder.CreateGlobalStringPtr(mnemonic),
  1319. };
  1320. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1321. }
  1322. Value* cur_pc_val = this->gen_const(32, pc.val);
  1323. pc=pc+4;
  1324. if(rd != 0){
  1325. Value* offs_val = this->gen_reg_load(rs1 + traits<ARCH>::X0, 0);
  1326. Value* Xtmp0_val = this->gen_ext(
  1327. this->gen_read_mem(traits<ARCH>::MEM, offs_val, 32/8),
  1328. 32,
  1329. true);
  1330. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1331. Value* REStmp1_val = this->gen_ext(
  1332. this->builder.CreateNeg(this->gen_const(8U, 1)),
  1333. 32,
  1334. true);
  1335. this->gen_write_mem(
  1336. traits<ARCH>::RES,
  1337. offs_val,
  1338. this->builder.CreateZExtOrTrunc(REStmp1_val,this->get_type(32)));
  1339. }
  1340. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1341. this->gen_sync(POST_SYNC, 29);
  1342. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1343. this->gen_trap_check(bb);
  1344. return std::make_tuple(CONT, bb);
  1345. }
  1346. /* instruction 30: SC.W */
  1347. std::tuple<continuation_e, BasicBlock*> __sc_w(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1348. bb->setName("SC.W");
  1349. this->gen_sync(PRE_SYNC, 30);
  1350. uint8_t rd = ((bit_sub<7,5>(instr)));
  1351. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1352. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  1353. uint8_t rl = ((bit_sub<25,1>(instr)));
  1354. uint8_t aq = ((bit_sub<26,1>(instr)));
  1355. if(this->disass_enabled){
  1356. /* generate console output when executing the command */
  1357. auto mnemonic = fmt::format(
  1358. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "sc.w"),
  1359. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  1360. std::vector<Value*> args {
  1361. this->core_ptr,
  1362. this->gen_const(64, pc.val),
  1363. this->builder.CreateGlobalStringPtr(mnemonic),
  1364. };
  1365. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1366. }
  1367. Value* cur_pc_val = this->gen_const(32, pc.val);
  1368. pc=pc+4;
  1369. Value* offs_val = this->gen_reg_load(rs1 + traits<ARCH>::X0, 0);
  1370. Value* res1_val = this->gen_read_mem(traits<ARCH>::RES, offs_val, 32/8);
  1371. {
  1372. BasicBlock* bbnext = BasicBlock::Create(this->mod->getContext(), "endif", this->func, this->leave_blk);
  1373. BasicBlock* bb_then = BasicBlock::Create(this->mod->getContext(), "thenbr", this->func, bbnext);
  1374. // this->builder.SetInsertPoint(bb);
  1375. this->gen_cond_branch(this->builder.CreateICmp(
  1376. ICmpInst::ICMP_NE,
  1377. res1_val,
  1378. this->gen_const(32U, 0)),
  1379. bb_then,
  1380. bbnext);
  1381. this->builder.SetInsertPoint(bb_then);
  1382. {
  1383. Value* MEMtmp0_val = this->gen_reg_load(rs2 + traits<ARCH>::X0, 1);
  1384. this->gen_write_mem(
  1385. traits<ARCH>::MEM,
  1386. offs_val,
  1387. this->builder.CreateZExtOrTrunc(MEMtmp0_val,this->get_type(32)));
  1388. }
  1389. this->builder.CreateBr(bbnext);
  1390. bb=bbnext;
  1391. }
  1392. this->builder.SetInsertPoint(bb);
  1393. if(rd != 0){
  1394. Value* Xtmp1_val = this->gen_choose(
  1395. this->builder.CreateICmp(
  1396. ICmpInst::ICMP_NE,
  1397. res1_val,
  1398. this->gen_ext(
  1399. this->gen_const(32U, 0),
  1400. 32,
  1401. false)),
  1402. this->gen_const(32U, 0),
  1403. this->gen_const(32U, 1),
  1404. 32);
  1405. this->builder.CreateStore(Xtmp1_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1406. }
  1407. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1408. this->gen_sync(POST_SYNC, 30);
  1409. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1410. this->gen_trap_check(bb);
  1411. return std::make_tuple(CONT, bb);
  1412. }
  1413. /* instruction 31: AMOSWAP.W */
  1414. std::tuple<continuation_e, BasicBlock*> __amoswap_w(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1415. bb->setName("AMOSWAP.W");
  1416. this->gen_sync(PRE_SYNC, 31);
  1417. uint8_t rd = ((bit_sub<7,5>(instr)));
  1418. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1419. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  1420. uint8_t rl = ((bit_sub<25,1>(instr)));
  1421. uint8_t aq = ((bit_sub<26,1>(instr)));
  1422. if(this->disass_enabled){
  1423. /* generate console output when executing the command */
  1424. auto mnemonic = fmt::format(
  1425. "{mnemonic:10} {rd}, {rs1}, {rs2} (aqu={aq},rel={rl})", fmt::arg("mnemonic", "amoswap.w"),
  1426. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)), fmt::arg("aq", aq), fmt::arg("rl", rl));
  1427. std::vector<Value*> args {
  1428. this->core_ptr,
  1429. this->gen_const(64, pc.val),
  1430. this->builder.CreateGlobalStringPtr(mnemonic),
  1431. };
  1432. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1433. }
  1434. Value* cur_pc_val = this->gen_const(32, pc.val);
  1435. pc=pc+4;
  1436. Value* offs_val = this->gen_reg_load(rs1 + traits<ARCH>::X0, 0);
  1437. if(rd != 0){
  1438. Value* Xtmp0_val = this->gen_ext(
  1439. this->gen_read_mem(traits<ARCH>::MEM, offs_val, 32/8),
  1440. 32,
  1441. true);
  1442. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1443. }
  1444. Value* MEMtmp1_val = this->gen_reg_load(rs2 + traits<ARCH>::X0, 0);
  1445. this->gen_write_mem(
  1446. traits<ARCH>::MEM,
  1447. offs_val,
  1448. this->builder.CreateZExtOrTrunc(MEMtmp1_val,this->get_type(32)));
  1449. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1450. this->gen_sync(POST_SYNC, 31);
  1451. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1452. this->gen_trap_check(bb);
  1453. return std::make_tuple(CONT, bb);
  1454. }
  1455. /* instruction 32: AMOADD.W */
  1456. std::tuple<continuation_e, BasicBlock*> __amoadd_w(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1457. bb->setName("AMOADD.W");
  1458. this->gen_sync(PRE_SYNC, 32);
  1459. uint8_t rd = ((bit_sub<7,5>(instr)));
  1460. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1461. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  1462. uint8_t rl = ((bit_sub<25,1>(instr)));
  1463. uint8_t aq = ((bit_sub<26,1>(instr)));
  1464. if(this->disass_enabled){
  1465. /* generate console output when executing the command */
  1466. auto mnemonic = fmt::format(
  1467. "{mnemonic:10} {rd}, {rs1}, {rs2} (aqu={aq},rel={rl})", fmt::arg("mnemonic", "amoadd.w"),
  1468. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)), fmt::arg("aq", aq), fmt::arg("rl", rl));
  1469. std::vector<Value*> args {
  1470. this->core_ptr,
  1471. this->gen_const(64, pc.val),
  1472. this->builder.CreateGlobalStringPtr(mnemonic),
  1473. };
  1474. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1475. }
  1476. Value* cur_pc_val = this->gen_const(32, pc.val);
  1477. pc=pc+4;
  1478. Value* offs_val = this->gen_reg_load(rs1 + traits<ARCH>::X0, 0);
  1479. Value* res1_val = this->gen_ext(
  1480. this->gen_read_mem(traits<ARCH>::MEM, offs_val, 32/8),
  1481. 32,
  1482. true);
  1483. if(rd != 0){
  1484. Value* Xtmp0_val = res1_val;
  1485. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1486. }
  1487. Value* res2_val = this->builder.CreateAdd(
  1488. res1_val,
  1489. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0));
  1490. Value* MEMtmp1_val = res2_val;
  1491. this->gen_write_mem(
  1492. traits<ARCH>::MEM,
  1493. offs_val,
  1494. this->builder.CreateZExtOrTrunc(MEMtmp1_val,this->get_type(32)));
  1495. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1496. this->gen_sync(POST_SYNC, 32);
  1497. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1498. this->gen_trap_check(bb);
  1499. return std::make_tuple(CONT, bb);
  1500. }
  1501. /* instruction 33: AMOXOR.W */
  1502. std::tuple<continuation_e, BasicBlock*> __amoxor_w(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1503. bb->setName("AMOXOR.W");
  1504. this->gen_sync(PRE_SYNC, 33);
  1505. uint8_t rd = ((bit_sub<7,5>(instr)));
  1506. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1507. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  1508. uint8_t rl = ((bit_sub<25,1>(instr)));
  1509. uint8_t aq = ((bit_sub<26,1>(instr)));
  1510. if(this->disass_enabled){
  1511. /* generate console output when executing the command */
  1512. auto mnemonic = fmt::format(
  1513. "{mnemonic:10} {rd}, {rs1}, {rs2} (aqu={aq},rel={rl})", fmt::arg("mnemonic", "amoxor.w"),
  1514. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)), fmt::arg("aq", aq), fmt::arg("rl", rl));
  1515. std::vector<Value*> args {
  1516. this->core_ptr,
  1517. this->gen_const(64, pc.val),
  1518. this->builder.CreateGlobalStringPtr(mnemonic),
  1519. };
  1520. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1521. }
  1522. Value* cur_pc_val = this->gen_const(32, pc.val);
  1523. pc=pc+4;
  1524. Value* offs_val = this->gen_reg_load(rs1 + traits<ARCH>::X0, 0);
  1525. Value* res1_val = this->gen_ext(
  1526. this->gen_read_mem(traits<ARCH>::MEM, offs_val, 32/8),
  1527. 32,
  1528. true);
  1529. if(rd != 0){
  1530. Value* Xtmp0_val = res1_val;
  1531. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1532. }
  1533. Value* res2_val = this->builder.CreateXor(
  1534. res1_val,
  1535. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0));
  1536. Value* MEMtmp1_val = res2_val;
  1537. this->gen_write_mem(
  1538. traits<ARCH>::MEM,
  1539. offs_val,
  1540. this->builder.CreateZExtOrTrunc(MEMtmp1_val,this->get_type(32)));
  1541. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1542. this->gen_sync(POST_SYNC, 33);
  1543. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1544. this->gen_trap_check(bb);
  1545. return std::make_tuple(CONT, bb);
  1546. }
  1547. /* instruction 34: AMOAND.W */
  1548. std::tuple<continuation_e, BasicBlock*> __amoand_w(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1549. bb->setName("AMOAND.W");
  1550. this->gen_sync(PRE_SYNC, 34);
  1551. uint8_t rd = ((bit_sub<7,5>(instr)));
  1552. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1553. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  1554. uint8_t rl = ((bit_sub<25,1>(instr)));
  1555. uint8_t aq = ((bit_sub<26,1>(instr)));
  1556. if(this->disass_enabled){
  1557. /* generate console output when executing the command */
  1558. auto mnemonic = fmt::format(
  1559. "{mnemonic:10} {rd}, {rs1}, {rs2} (aqu={aq},rel={rl})", fmt::arg("mnemonic", "amoand.w"),
  1560. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)), fmt::arg("aq", aq), fmt::arg("rl", rl));
  1561. std::vector<Value*> args {
  1562. this->core_ptr,
  1563. this->gen_const(64, pc.val),
  1564. this->builder.CreateGlobalStringPtr(mnemonic),
  1565. };
  1566. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1567. }
  1568. Value* cur_pc_val = this->gen_const(32, pc.val);
  1569. pc=pc+4;
  1570. Value* offs_val = this->gen_reg_load(rs1 + traits<ARCH>::X0, 0);
  1571. Value* res1_val = this->gen_ext(
  1572. this->gen_read_mem(traits<ARCH>::MEM, offs_val, 32/8),
  1573. 32,
  1574. true);
  1575. if(rd != 0){
  1576. Value* Xtmp0_val = res1_val;
  1577. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1578. }
  1579. Value* res2_val = this->builder.CreateAnd(
  1580. res1_val,
  1581. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0));
  1582. Value* MEMtmp1_val = res2_val;
  1583. this->gen_write_mem(
  1584. traits<ARCH>::MEM,
  1585. offs_val,
  1586. this->builder.CreateZExtOrTrunc(MEMtmp1_val,this->get_type(32)));
  1587. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1588. this->gen_sync(POST_SYNC, 34);
  1589. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1590. this->gen_trap_check(bb);
  1591. return std::make_tuple(CONT, bb);
  1592. }
  1593. /* instruction 35: AMOOR.W */
  1594. std::tuple<continuation_e, BasicBlock*> __amoor_w(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1595. bb->setName("AMOOR.W");
  1596. this->gen_sync(PRE_SYNC, 35);
  1597. uint8_t rd = ((bit_sub<7,5>(instr)));
  1598. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1599. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  1600. uint8_t rl = ((bit_sub<25,1>(instr)));
  1601. uint8_t aq = ((bit_sub<26,1>(instr)));
  1602. if(this->disass_enabled){
  1603. /* generate console output when executing the command */
  1604. auto mnemonic = fmt::format(
  1605. "{mnemonic:10} {rd}, {rs1}, {rs2} (aqu={aq},rel={rl})", fmt::arg("mnemonic", "amoor.w"),
  1606. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)), fmt::arg("aq", aq), fmt::arg("rl", rl));
  1607. std::vector<Value*> args {
  1608. this->core_ptr,
  1609. this->gen_const(64, pc.val),
  1610. this->builder.CreateGlobalStringPtr(mnemonic),
  1611. };
  1612. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1613. }
  1614. Value* cur_pc_val = this->gen_const(32, pc.val);
  1615. pc=pc+4;
  1616. Value* offs_val = this->gen_reg_load(rs1 + traits<ARCH>::X0, 0);
  1617. Value* res1_val = this->gen_ext(
  1618. this->gen_read_mem(traits<ARCH>::MEM, offs_val, 32/8),
  1619. 32,
  1620. true);
  1621. if(rd != 0){
  1622. Value* Xtmp0_val = res1_val;
  1623. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1624. }
  1625. Value* res2_val = this->builder.CreateOr(
  1626. res1_val,
  1627. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0));
  1628. Value* MEMtmp1_val = res2_val;
  1629. this->gen_write_mem(
  1630. traits<ARCH>::MEM,
  1631. offs_val,
  1632. this->builder.CreateZExtOrTrunc(MEMtmp1_val,this->get_type(32)));
  1633. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1634. this->gen_sync(POST_SYNC, 35);
  1635. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1636. this->gen_trap_check(bb);
  1637. return std::make_tuple(CONT, bb);
  1638. }
  1639. /* instruction 36: AMOMIN.W */
  1640. std::tuple<continuation_e, BasicBlock*> __amomin_w(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1641. bb->setName("AMOMIN.W");
  1642. this->gen_sync(PRE_SYNC, 36);
  1643. uint8_t rd = ((bit_sub<7,5>(instr)));
  1644. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1645. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  1646. uint8_t rl = ((bit_sub<25,1>(instr)));
  1647. uint8_t aq = ((bit_sub<26,1>(instr)));
  1648. if(this->disass_enabled){
  1649. /* generate console output when executing the command */
  1650. auto mnemonic = fmt::format(
  1651. "{mnemonic:10} {rd}, {rs1}, {rs2} (aqu={aq},rel={rl})", fmt::arg("mnemonic", "amomin.w"),
  1652. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)), fmt::arg("aq", aq), fmt::arg("rl", rl));
  1653. std::vector<Value*> args {
  1654. this->core_ptr,
  1655. this->gen_const(64, pc.val),
  1656. this->builder.CreateGlobalStringPtr(mnemonic),
  1657. };
  1658. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1659. }
  1660. Value* cur_pc_val = this->gen_const(32, pc.val);
  1661. pc=pc+4;
  1662. Value* offs_val = this->gen_reg_load(rs1 + traits<ARCH>::X0, 0);
  1663. Value* res1_val = this->gen_ext(
  1664. this->gen_read_mem(traits<ARCH>::MEM, offs_val, 32/8),
  1665. 32,
  1666. true);
  1667. if(rd != 0){
  1668. Value* Xtmp0_val = res1_val;
  1669. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1670. }
  1671. Value* res2_val = this->gen_choose(
  1672. this->builder.CreateICmp(
  1673. ICmpInst::ICMP_SGT,
  1674. this->gen_ext(
  1675. res1_val,
  1676. 32, true),
  1677. this->gen_ext(
  1678. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  1679. 32, true)),
  1680. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  1681. res1_val,
  1682. 32);
  1683. Value* MEMtmp1_val = res2_val;
  1684. this->gen_write_mem(
  1685. traits<ARCH>::MEM,
  1686. offs_val,
  1687. this->builder.CreateZExtOrTrunc(MEMtmp1_val,this->get_type(32)));
  1688. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1689. this->gen_sync(POST_SYNC, 36);
  1690. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1691. this->gen_trap_check(bb);
  1692. return std::make_tuple(CONT, bb);
  1693. }
  1694. /* instruction 37: AMOMAX.W */
  1695. std::tuple<continuation_e, BasicBlock*> __amomax_w(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1696. bb->setName("AMOMAX.W");
  1697. this->gen_sync(PRE_SYNC, 37);
  1698. uint8_t rd = ((bit_sub<7,5>(instr)));
  1699. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1700. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  1701. uint8_t rl = ((bit_sub<25,1>(instr)));
  1702. uint8_t aq = ((bit_sub<26,1>(instr)));
  1703. if(this->disass_enabled){
  1704. /* generate console output when executing the command */
  1705. auto mnemonic = fmt::format(
  1706. "{mnemonic:10} {rd}, {rs1}, {rs2} (aqu={aq},rel={rl})", fmt::arg("mnemonic", "amomax.w"),
  1707. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)), fmt::arg("aq", aq), fmt::arg("rl", rl));
  1708. std::vector<Value*> args {
  1709. this->core_ptr,
  1710. this->gen_const(64, pc.val),
  1711. this->builder.CreateGlobalStringPtr(mnemonic),
  1712. };
  1713. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1714. }
  1715. Value* cur_pc_val = this->gen_const(32, pc.val);
  1716. pc=pc+4;
  1717. Value* offs_val = this->gen_reg_load(rs1 + traits<ARCH>::X0, 0);
  1718. Value* res1_val = this->gen_ext(
  1719. this->gen_read_mem(traits<ARCH>::MEM, offs_val, 32/8),
  1720. 32,
  1721. true);
  1722. if(rd != 0){
  1723. Value* Xtmp0_val = res1_val;
  1724. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1725. }
  1726. Value* res2_val = this->gen_choose(
  1727. this->builder.CreateICmp(
  1728. ICmpInst::ICMP_SLT,
  1729. this->gen_ext(
  1730. res1_val,
  1731. 32, true),
  1732. this->gen_ext(
  1733. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  1734. 32, true)),
  1735. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  1736. res1_val,
  1737. 32);
  1738. Value* MEMtmp1_val = res2_val;
  1739. this->gen_write_mem(
  1740. traits<ARCH>::MEM,
  1741. offs_val,
  1742. this->builder.CreateZExtOrTrunc(MEMtmp1_val,this->get_type(32)));
  1743. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1744. this->gen_sync(POST_SYNC, 37);
  1745. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1746. this->gen_trap_check(bb);
  1747. return std::make_tuple(CONT, bb);
  1748. }
  1749. /* instruction 38: AMOMINU.W */
  1750. std::tuple<continuation_e, BasicBlock*> __amominu_w(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1751. bb->setName("AMOMINU.W");
  1752. this->gen_sync(PRE_SYNC, 38);
  1753. uint8_t rd = ((bit_sub<7,5>(instr)));
  1754. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1755. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  1756. uint8_t rl = ((bit_sub<25,1>(instr)));
  1757. uint8_t aq = ((bit_sub<26,1>(instr)));
  1758. if(this->disass_enabled){
  1759. /* generate console output when executing the command */
  1760. auto mnemonic = fmt::format(
  1761. "{mnemonic:10} {rd}, {rs1}, {rs2} (aqu={aq},rel={rl})", fmt::arg("mnemonic", "amominu.w"),
  1762. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)), fmt::arg("aq", aq), fmt::arg("rl", rl));
  1763. std::vector<Value*> args {
  1764. this->core_ptr,
  1765. this->gen_const(64, pc.val),
  1766. this->builder.CreateGlobalStringPtr(mnemonic),
  1767. };
  1768. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1769. }
  1770. Value* cur_pc_val = this->gen_const(32, pc.val);
  1771. pc=pc+4;
  1772. Value* offs_val = this->gen_reg_load(rs1 + traits<ARCH>::X0, 0);
  1773. Value* res1_val = this->gen_ext(
  1774. this->gen_read_mem(traits<ARCH>::MEM, offs_val, 32/8),
  1775. 32,
  1776. true);
  1777. if(rd != 0){
  1778. Value* Xtmp0_val = res1_val;
  1779. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1780. }
  1781. Value* res2_val = this->gen_choose(
  1782. this->builder.CreateICmp(
  1783. ICmpInst::ICMP_UGT,
  1784. res1_val,
  1785. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0)),
  1786. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  1787. res1_val,
  1788. 32);
  1789. Value* MEMtmp1_val = res2_val;
  1790. this->gen_write_mem(
  1791. traits<ARCH>::MEM,
  1792. offs_val,
  1793. this->builder.CreateZExtOrTrunc(MEMtmp1_val,this->get_type(32)));
  1794. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1795. this->gen_sync(POST_SYNC, 38);
  1796. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1797. this->gen_trap_check(bb);
  1798. return std::make_tuple(CONT, bb);
  1799. }
  1800. /* instruction 39: AMOMAXU.W */
  1801. std::tuple<continuation_e, BasicBlock*> __amomaxu_w(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1802. bb->setName("AMOMAXU.W");
  1803. this->gen_sync(PRE_SYNC, 39);
  1804. uint8_t rd = ((bit_sub<7,5>(instr)));
  1805. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1806. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  1807. uint8_t rl = ((bit_sub<25,1>(instr)));
  1808. uint8_t aq = ((bit_sub<26,1>(instr)));
  1809. if(this->disass_enabled){
  1810. /* generate console output when executing the command */
  1811. auto mnemonic = fmt::format(
  1812. "{mnemonic:10} {rd}, {rs1}, {rs2} (aqu={aq},rel={rl})", fmt::arg("mnemonic", "amomaxu.w"),
  1813. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)), fmt::arg("aq", aq), fmt::arg("rl", rl));
  1814. std::vector<Value*> args {
  1815. this->core_ptr,
  1816. this->gen_const(64, pc.val),
  1817. this->builder.CreateGlobalStringPtr(mnemonic),
  1818. };
  1819. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1820. }
  1821. Value* cur_pc_val = this->gen_const(32, pc.val);
  1822. pc=pc+4;
  1823. Value* offs_val = this->gen_reg_load(rs1 + traits<ARCH>::X0, 0);
  1824. Value* res1_val = this->gen_ext(
  1825. this->gen_read_mem(traits<ARCH>::MEM, offs_val, 32/8),
  1826. 32,
  1827. true);
  1828. if(rd != 0){
  1829. Value* Xtmp0_val = res1_val;
  1830. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1831. }
  1832. Value* res2_val = this->gen_choose(
  1833. this->builder.CreateICmp(
  1834. ICmpInst::ICMP_ULT,
  1835. res1_val,
  1836. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0)),
  1837. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  1838. res1_val,
  1839. 32);
  1840. Value* MEMtmp1_val = res2_val;
  1841. this->gen_write_mem(
  1842. traits<ARCH>::MEM,
  1843. offs_val,
  1844. this->builder.CreateZExtOrTrunc(MEMtmp1_val,this->get_type(32)));
  1845. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1846. this->gen_sync(POST_SYNC, 39);
  1847. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1848. this->gen_trap_check(bb);
  1849. return std::make_tuple(CONT, bb);
  1850. }
  1851. /* instruction 40: MUL */
  1852. std::tuple<continuation_e, BasicBlock*> __mul(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1853. bb->setName("MUL");
  1854. this->gen_sync(PRE_SYNC, 40);
  1855. uint8_t rd = ((bit_sub<7,5>(instr)));
  1856. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1857. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  1858. if(this->disass_enabled){
  1859. /* generate console output when executing the command */
  1860. auto mnemonic = fmt::format(
  1861. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "mul"),
  1862. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  1863. std::vector<Value*> args {
  1864. this->core_ptr,
  1865. this->gen_const(64, pc.val),
  1866. this->builder.CreateGlobalStringPtr(mnemonic),
  1867. };
  1868. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1869. }
  1870. Value* cur_pc_val = this->gen_const(32, pc.val);
  1871. pc=pc+4;
  1872. if(rd != 0){
  1873. Value* res_val = this->builder.CreateMul(
  1874. this->gen_ext(
  1875. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  1876. 128,
  1877. false),
  1878. this->gen_ext(
  1879. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  1880. 128,
  1881. false));
  1882. Value* Xtmp0_val = this->gen_ext(
  1883. res_val,
  1884. 32,
  1885. false);
  1886. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1887. }
  1888. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1889. this->gen_sync(POST_SYNC, 40);
  1890. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1891. this->gen_trap_check(bb);
  1892. return std::make_tuple(CONT, bb);
  1893. }
  1894. /* instruction 41: MULH */
  1895. std::tuple<continuation_e, BasicBlock*> __mulh(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1896. bb->setName("MULH");
  1897. this->gen_sync(PRE_SYNC, 41);
  1898. uint8_t rd = ((bit_sub<7,5>(instr)));
  1899. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1900. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  1901. if(this->disass_enabled){
  1902. /* generate console output when executing the command */
  1903. auto mnemonic = fmt::format(
  1904. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "mulh"),
  1905. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  1906. std::vector<Value*> args {
  1907. this->core_ptr,
  1908. this->gen_const(64, pc.val),
  1909. this->builder.CreateGlobalStringPtr(mnemonic),
  1910. };
  1911. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1912. }
  1913. Value* cur_pc_val = this->gen_const(32, pc.val);
  1914. pc=pc+4;
  1915. if(rd != 0){
  1916. Value* res_val = this->builder.CreateMul(
  1917. this->gen_ext(
  1918. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  1919. 128,
  1920. true),
  1921. this->gen_ext(
  1922. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  1923. 128,
  1924. true));
  1925. Value* Xtmp0_val = this->gen_ext(
  1926. this->builder.CreateLShr(
  1927. res_val,
  1928. this->gen_const(32U, 32)),
  1929. 32,
  1930. false);
  1931. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1932. }
  1933. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1934. this->gen_sync(POST_SYNC, 41);
  1935. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1936. this->gen_trap_check(bb);
  1937. return std::make_tuple(CONT, bb);
  1938. }
  1939. /* instruction 42: MULHSU */
  1940. std::tuple<continuation_e, BasicBlock*> __mulhsu(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1941. bb->setName("MULHSU");
  1942. this->gen_sync(PRE_SYNC, 42);
  1943. uint8_t rd = ((bit_sub<7,5>(instr)));
  1944. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1945. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  1946. if(this->disass_enabled){
  1947. /* generate console output when executing the command */
  1948. auto mnemonic = fmt::format(
  1949. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "mulhsu"),
  1950. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  1951. std::vector<Value*> args {
  1952. this->core_ptr,
  1953. this->gen_const(64, pc.val),
  1954. this->builder.CreateGlobalStringPtr(mnemonic),
  1955. };
  1956. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1957. }
  1958. Value* cur_pc_val = this->gen_const(32, pc.val);
  1959. pc=pc+4;
  1960. if(rd != 0){
  1961. Value* res_val = this->builder.CreateMul(
  1962. this->gen_ext(
  1963. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  1964. 128,
  1965. true),
  1966. this->gen_ext(
  1967. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  1968. 128,
  1969. false));
  1970. Value* Xtmp0_val = this->gen_ext(
  1971. this->builder.CreateLShr(
  1972. res_val,
  1973. this->gen_const(32U, 32)),
  1974. 32,
  1975. false);
  1976. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1977. }
  1978. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1979. this->gen_sync(POST_SYNC, 42);
  1980. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1981. this->gen_trap_check(bb);
  1982. return std::make_tuple(CONT, bb);
  1983. }
  1984. /* instruction 43: MULHU */
  1985. std::tuple<continuation_e, BasicBlock*> __mulhu(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1986. bb->setName("MULHU");
  1987. this->gen_sync(PRE_SYNC, 43);
  1988. uint8_t rd = ((bit_sub<7,5>(instr)));
  1989. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1990. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  1991. if(this->disass_enabled){
  1992. /* generate console output when executing the command */
  1993. auto mnemonic = fmt::format(
  1994. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "mulhu"),
  1995. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  1996. std::vector<Value*> args {
  1997. this->core_ptr,
  1998. this->gen_const(64, pc.val),
  1999. this->builder.CreateGlobalStringPtr(mnemonic),
  2000. };
  2001. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2002. }
  2003. Value* cur_pc_val = this->gen_const(32, pc.val);
  2004. pc=pc+4;
  2005. if(rd != 0){
  2006. Value* res_val = this->builder.CreateMul(
  2007. this->gen_ext(
  2008. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  2009. 128,
  2010. false),
  2011. this->gen_ext(
  2012. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  2013. 128,
  2014. false));
  2015. Value* Xtmp0_val = this->gen_ext(
  2016. this->builder.CreateLShr(
  2017. res_val,
  2018. this->gen_const(32U, 32)),
  2019. 32,
  2020. false);
  2021. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2022. }
  2023. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2024. this->gen_sync(POST_SYNC, 43);
  2025. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2026. this->gen_trap_check(bb);
  2027. return std::make_tuple(CONT, bb);
  2028. }
  2029. /* instruction 44: DIV */
  2030. std::tuple<continuation_e, BasicBlock*> __div(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2031. bb->setName("DIV");
  2032. this->gen_sync(PRE_SYNC, 44);
  2033. uint8_t rd = ((bit_sub<7,5>(instr)));
  2034. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2035. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  2036. if(this->disass_enabled){
  2037. /* generate console output when executing the command */
  2038. auto mnemonic = fmt::format(
  2039. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "div"),
  2040. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  2041. std::vector<Value*> args {
  2042. this->core_ptr,
  2043. this->gen_const(64, pc.val),
  2044. this->builder.CreateGlobalStringPtr(mnemonic),
  2045. };
  2046. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2047. }
  2048. Value* cur_pc_val = this->gen_const(32, pc.val);
  2049. pc=pc+4;
  2050. if(rd != 0){
  2051. {
  2052. BasicBlock* bbnext = BasicBlock::Create(this->mod->getContext(), "endif", this->func, this->leave_blk);
  2053. BasicBlock* bb_then = BasicBlock::Create(this->mod->getContext(), "thenbr", this->func, bbnext);
  2054. BasicBlock* bb_else = BasicBlock::Create(this->mod->getContext(), "elsebr", this->func, bbnext);
  2055. // this->builder.SetInsertPoint(bb);
  2056. this->gen_cond_branch(this->builder.CreateICmp(
  2057. ICmpInst::ICMP_NE,
  2058. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  2059. this->gen_const(32U, 0)),
  2060. bb_then,
  2061. bb_else);
  2062. this->builder.SetInsertPoint(bb_then);
  2063. {
  2064. uint32_t M1_val = - 1;
  2065. uint8_t XLM1_val = 32 - 1;
  2066. uint32_t ONE_val = 1;
  2067. uint32_t MMIN_val = ONE_val << XLM1_val;
  2068. {
  2069. BasicBlock* bbnext = BasicBlock::Create(this->mod->getContext(), "endif", this->func, this->leave_blk);
  2070. BasicBlock* bb_then = BasicBlock::Create(this->mod->getContext(), "thenbr", this->func, bbnext);
  2071. BasicBlock* bb_else = BasicBlock::Create(this->mod->getContext(), "elsebr", this->func, bbnext);
  2072. // this->builder.SetInsertPoint(bb);
  2073. this->gen_cond_branch(this->builder.CreateAnd(
  2074. this->builder.CreateICmp(
  2075. ICmpInst::ICMP_EQ,
  2076. this->gen_reg_load(rs1 + traits<ARCH>::X0, 1),
  2077. this->gen_const(32U, MMIN_val)),
  2078. this->builder.CreateICmp(
  2079. ICmpInst::ICMP_EQ,
  2080. this->gen_reg_load(rs2 + traits<ARCH>::X0, 1),
  2081. this->gen_const(32U, M1_val))),
  2082. bb_then,
  2083. bb_else);
  2084. this->builder.SetInsertPoint(bb_then);
  2085. {
  2086. Value* Xtmp0_val = this->gen_const(32U, MMIN_val);
  2087. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2088. }
  2089. this->builder.CreateBr(bbnext);
  2090. this->builder.SetInsertPoint(bb_else);
  2091. {
  2092. Value* Xtmp1_val = this->builder.CreateSDiv(
  2093. this->gen_ext(
  2094. this->gen_reg_load(rs1 + traits<ARCH>::X0, 2),
  2095. 32, true),
  2096. this->gen_ext(
  2097. this->gen_reg_load(rs2 + traits<ARCH>::X0, 2),
  2098. 32, true));
  2099. this->builder.CreateStore(Xtmp1_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2100. }
  2101. this->builder.CreateBr(bbnext);
  2102. bb=bbnext;
  2103. }
  2104. this->builder.SetInsertPoint(bb);
  2105. }
  2106. this->builder.CreateBr(bbnext);
  2107. this->builder.SetInsertPoint(bb_else);
  2108. {
  2109. Value* Xtmp2_val = this->builder.CreateNeg(this->gen_const(32U, 1));
  2110. this->builder.CreateStore(Xtmp2_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2111. }
  2112. this->builder.CreateBr(bbnext);
  2113. bb=bbnext;
  2114. }
  2115. this->builder.SetInsertPoint(bb);
  2116. }
  2117. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2118. this->gen_sync(POST_SYNC, 44);
  2119. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2120. this->gen_trap_check(bb);
  2121. return std::make_tuple(CONT, bb);
  2122. }
  2123. /* instruction 45: DIVU */
  2124. std::tuple<continuation_e, BasicBlock*> __divu(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2125. bb->setName("DIVU");
  2126. this->gen_sync(PRE_SYNC, 45);
  2127. uint8_t rd = ((bit_sub<7,5>(instr)));
  2128. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2129. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  2130. if(this->disass_enabled){
  2131. /* generate console output when executing the command */
  2132. auto mnemonic = fmt::format(
  2133. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "divu"),
  2134. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  2135. std::vector<Value*> args {
  2136. this->core_ptr,
  2137. this->gen_const(64, pc.val),
  2138. this->builder.CreateGlobalStringPtr(mnemonic),
  2139. };
  2140. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2141. }
  2142. Value* cur_pc_val = this->gen_const(32, pc.val);
  2143. pc=pc+4;
  2144. if(rd != 0){
  2145. {
  2146. BasicBlock* bbnext = BasicBlock::Create(this->mod->getContext(), "endif", this->func, this->leave_blk);
  2147. BasicBlock* bb_then = BasicBlock::Create(this->mod->getContext(), "thenbr", this->func, bbnext);
  2148. BasicBlock* bb_else = BasicBlock::Create(this->mod->getContext(), "elsebr", this->func, bbnext);
  2149. // this->builder.SetInsertPoint(bb);
  2150. this->gen_cond_branch(this->builder.CreateICmp(
  2151. ICmpInst::ICMP_NE,
  2152. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  2153. this->gen_const(32U, 0)),
  2154. bb_then,
  2155. bb_else);
  2156. this->builder.SetInsertPoint(bb_then);
  2157. {
  2158. Value* Xtmp0_val = this->builder.CreateUDiv(
  2159. this->gen_reg_load(rs1 + traits<ARCH>::X0, 1),
  2160. this->gen_reg_load(rs2 + traits<ARCH>::X0, 1));
  2161. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2162. }
  2163. this->builder.CreateBr(bbnext);
  2164. this->builder.SetInsertPoint(bb_else);
  2165. {
  2166. Value* Xtmp1_val = this->builder.CreateNeg(this->gen_const(32U, 1));
  2167. this->builder.CreateStore(Xtmp1_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2168. }
  2169. this->builder.CreateBr(bbnext);
  2170. bb=bbnext;
  2171. }
  2172. this->builder.SetInsertPoint(bb);
  2173. }
  2174. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2175. this->gen_sync(POST_SYNC, 45);
  2176. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2177. this->gen_trap_check(bb);
  2178. return std::make_tuple(CONT, bb);
  2179. }
  2180. /* instruction 46: REM */
  2181. std::tuple<continuation_e, BasicBlock*> __rem(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2182. bb->setName("REM");
  2183. this->gen_sync(PRE_SYNC, 46);
  2184. uint8_t rd = ((bit_sub<7,5>(instr)));
  2185. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2186. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  2187. if(this->disass_enabled){
  2188. /* generate console output when executing the command */
  2189. auto mnemonic = fmt::format(
  2190. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "rem"),
  2191. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  2192. std::vector<Value*> args {
  2193. this->core_ptr,
  2194. this->gen_const(64, pc.val),
  2195. this->builder.CreateGlobalStringPtr(mnemonic),
  2196. };
  2197. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2198. }
  2199. Value* cur_pc_val = this->gen_const(32, pc.val);
  2200. pc=pc+4;
  2201. if(rd != 0){
  2202. {
  2203. BasicBlock* bbnext = BasicBlock::Create(this->mod->getContext(), "endif", this->func, this->leave_blk);
  2204. BasicBlock* bb_then = BasicBlock::Create(this->mod->getContext(), "thenbr", this->func, bbnext);
  2205. BasicBlock* bb_else = BasicBlock::Create(this->mod->getContext(), "elsebr", this->func, bbnext);
  2206. // this->builder.SetInsertPoint(bb);
  2207. this->gen_cond_branch(this->builder.CreateICmp(
  2208. ICmpInst::ICMP_NE,
  2209. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  2210. this->gen_const(32U, 0)),
  2211. bb_then,
  2212. bb_else);
  2213. this->builder.SetInsertPoint(bb_then);
  2214. {
  2215. uint32_t M1_val = - 1;
  2216. uint32_t XLM1_val = 32 - 1;
  2217. uint32_t ONE_val = 1;
  2218. uint32_t MMIN_val = ONE_val << XLM1_val;
  2219. {
  2220. BasicBlock* bbnext = BasicBlock::Create(this->mod->getContext(), "endif", this->func, this->leave_blk);
  2221. BasicBlock* bb_then = BasicBlock::Create(this->mod->getContext(), "thenbr", this->func, bbnext);
  2222. BasicBlock* bb_else = BasicBlock::Create(this->mod->getContext(), "elsebr", this->func, bbnext);
  2223. // this->builder.SetInsertPoint(bb);
  2224. this->gen_cond_branch(this->builder.CreateAnd(
  2225. this->builder.CreateICmp(
  2226. ICmpInst::ICMP_EQ,
  2227. this->gen_reg_load(rs1 + traits<ARCH>::X0, 1),
  2228. this->gen_const(32U, MMIN_val)),
  2229. this->builder.CreateICmp(
  2230. ICmpInst::ICMP_EQ,
  2231. this->gen_reg_load(rs2 + traits<ARCH>::X0, 1),
  2232. this->gen_const(32U, M1_val))),
  2233. bb_then,
  2234. bb_else);
  2235. this->builder.SetInsertPoint(bb_then);
  2236. {
  2237. Value* Xtmp0_val = this->gen_const(32U, 0);
  2238. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2239. }
  2240. this->builder.CreateBr(bbnext);
  2241. this->builder.SetInsertPoint(bb_else);
  2242. {
  2243. Value* Xtmp1_val = this->builder.CreateSRem(
  2244. this->gen_ext(
  2245. this->gen_reg_load(rs1 + traits<ARCH>::X0, 2),
  2246. 32, true),
  2247. this->gen_ext(
  2248. this->gen_reg_load(rs2 + traits<ARCH>::X0, 2),
  2249. 32, true));
  2250. this->builder.CreateStore(Xtmp1_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2251. }
  2252. this->builder.CreateBr(bbnext);
  2253. bb=bbnext;
  2254. }
  2255. this->builder.SetInsertPoint(bb);
  2256. }
  2257. this->builder.CreateBr(bbnext);
  2258. this->builder.SetInsertPoint(bb_else);
  2259. {
  2260. Value* Xtmp2_val = this->gen_reg_load(rs1 + traits<ARCH>::X0, 1);
  2261. this->builder.CreateStore(Xtmp2_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2262. }
  2263. this->builder.CreateBr(bbnext);
  2264. bb=bbnext;
  2265. }
  2266. this->builder.SetInsertPoint(bb);
  2267. }
  2268. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2269. this->gen_sync(POST_SYNC, 46);
  2270. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2271. this->gen_trap_check(bb);
  2272. return std::make_tuple(CONT, bb);
  2273. }
  2274. /* instruction 47: REMU */
  2275. std::tuple<continuation_e, BasicBlock*> __remu(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2276. bb->setName("REMU");
  2277. this->gen_sync(PRE_SYNC, 47);
  2278. uint8_t rd = ((bit_sub<7,5>(instr)));
  2279. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2280. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  2281. if(this->disass_enabled){
  2282. /* generate console output when executing the command */
  2283. auto mnemonic = fmt::format(
  2284. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "remu"),
  2285. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  2286. std::vector<Value*> args {
  2287. this->core_ptr,
  2288. this->gen_const(64, pc.val),
  2289. this->builder.CreateGlobalStringPtr(mnemonic),
  2290. };
  2291. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2292. }
  2293. Value* cur_pc_val = this->gen_const(32, pc.val);
  2294. pc=pc+4;
  2295. if(rd != 0){
  2296. {
  2297. BasicBlock* bbnext = BasicBlock::Create(this->mod->getContext(), "endif", this->func, this->leave_blk);
  2298. BasicBlock* bb_then = BasicBlock::Create(this->mod->getContext(), "thenbr", this->func, bbnext);
  2299. BasicBlock* bb_else = BasicBlock::Create(this->mod->getContext(), "elsebr", this->func, bbnext);
  2300. // this->builder.SetInsertPoint(bb);
  2301. this->gen_cond_branch(this->builder.CreateICmp(
  2302. ICmpInst::ICMP_NE,
  2303. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  2304. this->gen_const(32U, 0)),
  2305. bb_then,
  2306. bb_else);
  2307. this->builder.SetInsertPoint(bb_then);
  2308. {
  2309. Value* Xtmp0_val = this->builder.CreateURem(
  2310. this->gen_reg_load(rs1 + traits<ARCH>::X0, 1),
  2311. this->gen_reg_load(rs2 + traits<ARCH>::X0, 1));
  2312. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2313. }
  2314. this->builder.CreateBr(bbnext);
  2315. this->builder.SetInsertPoint(bb_else);
  2316. {
  2317. Value* Xtmp1_val = this->gen_reg_load(rs1 + traits<ARCH>::X0, 1);
  2318. this->builder.CreateStore(Xtmp1_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2319. }
  2320. this->builder.CreateBr(bbnext);
  2321. bb=bbnext;
  2322. }
  2323. this->builder.SetInsertPoint(bb);
  2324. }
  2325. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2326. this->gen_sync(POST_SYNC, 47);
  2327. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2328. this->gen_trap_check(bb);
  2329. return std::make_tuple(CONT, bb);
  2330. }
  2331. /* instruction 48: LUI */
  2332. std::tuple<continuation_e, BasicBlock*> __lui(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2333. bb->setName("LUI");
  2334. this->gen_sync(PRE_SYNC, 48);
  2335. uint8_t rd = ((bit_sub<7,5>(instr)));
  2336. int32_t imm = signextend<int32_t,32>((bit_sub<12,20>(instr) << 12));
  2337. if(this->disass_enabled){
  2338. /* generate console output when executing the command */
  2339. auto mnemonic = fmt::format(
  2340. "{mnemonic:10} {rd}, {imm:#05x}", fmt::arg("mnemonic", "lui"),
  2341. fmt::arg("rd", name(rd)), fmt::arg("imm", imm));
  2342. std::vector<Value*> args {
  2343. this->core_ptr,
  2344. this->gen_const(64, pc.val),
  2345. this->builder.CreateGlobalStringPtr(mnemonic),
  2346. };
  2347. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2348. }
  2349. Value* cur_pc_val = this->gen_const(32, pc.val);
  2350. pc=pc+4;
  2351. if(rd != 0){
  2352. Value* Xtmp0_val = this->gen_const(32U, imm);
  2353. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2354. }
  2355. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2356. this->gen_sync(POST_SYNC, 48);
  2357. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2358. this->gen_trap_check(bb);
  2359. return std::make_tuple(CONT, bb);
  2360. }
  2361. /* instruction 49: AUIPC */
  2362. std::tuple<continuation_e, BasicBlock*> __auipc(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2363. bb->setName("AUIPC");
  2364. this->gen_sync(PRE_SYNC, 49);
  2365. uint8_t rd = ((bit_sub<7,5>(instr)));
  2366. int32_t imm = signextend<int32_t,32>((bit_sub<12,20>(instr) << 12));
  2367. if(this->disass_enabled){
  2368. /* generate console output when executing the command */
  2369. auto mnemonic = fmt::format(
  2370. "{mnemonic:10} {rd}, {imm:#08x}", fmt::arg("mnemonic", "auipc"),
  2371. fmt::arg("rd", name(rd)), fmt::arg("imm", imm));
  2372. std::vector<Value*> args {
  2373. this->core_ptr,
  2374. this->gen_const(64, pc.val),
  2375. this->builder.CreateGlobalStringPtr(mnemonic),
  2376. };
  2377. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2378. }
  2379. Value* cur_pc_val = this->gen_const(32, pc.val);
  2380. pc=pc+4;
  2381. if(rd != 0){
  2382. Value* Xtmp0_val = this->builder.CreateAdd(
  2383. this->gen_ext(
  2384. cur_pc_val,
  2385. 32, true),
  2386. this->gen_const(32U, imm));
  2387. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2388. }
  2389. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2390. this->gen_sync(POST_SYNC, 49);
  2391. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2392. this->gen_trap_check(bb);
  2393. return std::make_tuple(CONT, bb);
  2394. }
  2395. /* instruction 50: JAL */
  2396. std::tuple<continuation_e, BasicBlock*> __jal(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2397. bb->setName("JAL");
  2398. this->gen_sync(PRE_SYNC, 50);
  2399. uint8_t rd = ((bit_sub<7,5>(instr)));
  2400. int32_t imm = signextend<int32_t,21>((bit_sub<12,8>(instr) << 12) | (bit_sub<20,1>(instr) << 11) | (bit_sub<21,10>(instr) << 1) | (bit_sub<31,1>(instr) << 20));
  2401. if(this->disass_enabled){
  2402. /* generate console output when executing the command */
  2403. auto mnemonic = fmt::format(
  2404. "{mnemonic:10} {rd}, {imm:#0x}", fmt::arg("mnemonic", "jal"),
  2405. fmt::arg("rd", name(rd)), fmt::arg("imm", imm));
  2406. std::vector<Value*> args {
  2407. this->core_ptr,
  2408. this->gen_const(64, pc.val),
  2409. this->builder.CreateGlobalStringPtr(mnemonic),
  2410. };
  2411. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2412. }
  2413. Value* cur_pc_val = this->gen_const(32, pc.val);
  2414. pc=pc+4;
  2415. if(rd != 0){
  2416. Value* Xtmp0_val = this->builder.CreateAdd(
  2417. cur_pc_val,
  2418. this->gen_const(32U, 4));
  2419. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2420. }
  2421. Value* PC_val = this->builder.CreateAdd(
  2422. this->gen_ext(
  2423. cur_pc_val,
  2424. 32, true),
  2425. this->gen_const(32U, imm));
  2426. this->builder.CreateStore(PC_val, get_reg_ptr(traits<ARCH>::NEXT_PC), false);
  2427. Value* is_cont_v = this->builder.CreateICmp(ICmpInst::ICMP_NE, PC_val, this->gen_const(32U, pc.val), "is_cont_v");
  2428. this->builder.CreateStore(this->gen_ext(is_cont_v, 32U, false), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  2429. this->gen_sync(POST_SYNC, 50);
  2430. this->gen_trap_check(this->leave_blk);
  2431. return std::make_tuple(BRANCH, nullptr);
  2432. }
  2433. /* instruction 51: BEQ */
  2434. std::tuple<continuation_e, BasicBlock*> __beq(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2435. bb->setName("BEQ");
  2436. this->gen_sync(PRE_SYNC, 51);
  2437. int16_t imm = signextend<int16_t,13>((bit_sub<7,1>(instr) << 11) | (bit_sub<8,4>(instr) << 1) | (bit_sub<25,6>(instr) << 5) | (bit_sub<31,1>(instr) << 12));
  2438. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2439. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  2440. if(this->disass_enabled){
  2441. /* generate console output when executing the command */
  2442. auto mnemonic = fmt::format(
  2443. "{mnemonic:10} {rs1}, {rs2}, {imm:#0x}", fmt::arg("mnemonic", "beq"),
  2444. fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)), fmt::arg("imm", imm));
  2445. std::vector<Value*> args {
  2446. this->core_ptr,
  2447. this->gen_const(64, pc.val),
  2448. this->builder.CreateGlobalStringPtr(mnemonic),
  2449. };
  2450. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2451. }
  2452. Value* cur_pc_val = this->gen_const(32, pc.val);
  2453. pc=pc+4;
  2454. Value* PC_val = this->gen_choose(
  2455. this->builder.CreateICmp(
  2456. ICmpInst::ICMP_EQ,
  2457. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  2458. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0)),
  2459. this->builder.CreateAdd(
  2460. this->gen_ext(
  2461. cur_pc_val,
  2462. 32, true),
  2463. this->gen_const(32U, imm)),
  2464. this->builder.CreateAdd(
  2465. cur_pc_val,
  2466. this->gen_const(32U, 4)),
  2467. 32);
  2468. this->builder.CreateStore(PC_val, get_reg_ptr(traits<ARCH>::NEXT_PC), false);
  2469. Value* is_cont_v = this->builder.CreateICmp(ICmpInst::ICMP_NE, PC_val, this->gen_const(32U, pc.val), "is_cont_v");
  2470. this->builder.CreateStore(this->gen_ext(is_cont_v, 32U, false), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  2471. this->gen_sync(POST_SYNC, 51);
  2472. this->gen_trap_check(this->leave_blk);
  2473. return std::make_tuple(BRANCH, nullptr);
  2474. }
  2475. /* instruction 52: BNE */
  2476. std::tuple<continuation_e, BasicBlock*> __bne(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2477. bb->setName("BNE");
  2478. this->gen_sync(PRE_SYNC, 52);
  2479. int16_t imm = signextend<int16_t,13>((bit_sub<7,1>(instr) << 11) | (bit_sub<8,4>(instr) << 1) | (bit_sub<25,6>(instr) << 5) | (bit_sub<31,1>(instr) << 12));
  2480. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2481. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  2482. if(this->disass_enabled){
  2483. /* generate console output when executing the command */
  2484. auto mnemonic = fmt::format(
  2485. "{mnemonic:10} {rs1}, {rs2}, {imm:#0x}", fmt::arg("mnemonic", "bne"),
  2486. fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)), fmt::arg("imm", imm));
  2487. std::vector<Value*> args {
  2488. this->core_ptr,
  2489. this->gen_const(64, pc.val),
  2490. this->builder.CreateGlobalStringPtr(mnemonic),
  2491. };
  2492. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2493. }
  2494. Value* cur_pc_val = this->gen_const(32, pc.val);
  2495. pc=pc+4;
  2496. Value* PC_val = this->gen_choose(
  2497. this->builder.CreateICmp(
  2498. ICmpInst::ICMP_NE,
  2499. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  2500. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0)),
  2501. this->builder.CreateAdd(
  2502. this->gen_ext(
  2503. cur_pc_val,
  2504. 32, true),
  2505. this->gen_const(32U, imm)),
  2506. this->builder.CreateAdd(
  2507. cur_pc_val,
  2508. this->gen_const(32U, 4)),
  2509. 32);
  2510. this->builder.CreateStore(PC_val, get_reg_ptr(traits<ARCH>::NEXT_PC), false);
  2511. Value* is_cont_v = this->builder.CreateICmp(ICmpInst::ICMP_NE, PC_val, this->gen_const(32U, pc.val), "is_cont_v");
  2512. this->builder.CreateStore(this->gen_ext(is_cont_v, 32U, false), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  2513. this->gen_sync(POST_SYNC, 52);
  2514. this->gen_trap_check(this->leave_blk);
  2515. return std::make_tuple(BRANCH, nullptr);
  2516. }
  2517. /* instruction 53: BLT */
  2518. std::tuple<continuation_e, BasicBlock*> __blt(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2519. bb->setName("BLT");
  2520. this->gen_sync(PRE_SYNC, 53);
  2521. int16_t imm = signextend<int16_t,13>((bit_sub<7,1>(instr) << 11) | (bit_sub<8,4>(instr) << 1) | (bit_sub<25,6>(instr) << 5) | (bit_sub<31,1>(instr) << 12));
  2522. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2523. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  2524. if(this->disass_enabled){
  2525. /* generate console output when executing the command */
  2526. auto mnemonic = fmt::format(
  2527. "{mnemonic:10} {rs1}, {rs2}, {imm:#0x}", fmt::arg("mnemonic", "blt"),
  2528. fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)), fmt::arg("imm", imm));
  2529. std::vector<Value*> args {
  2530. this->core_ptr,
  2531. this->gen_const(64, pc.val),
  2532. this->builder.CreateGlobalStringPtr(mnemonic),
  2533. };
  2534. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2535. }
  2536. Value* cur_pc_val = this->gen_const(32, pc.val);
  2537. pc=pc+4;
  2538. Value* PC_val = this->gen_choose(
  2539. this->builder.CreateICmp(
  2540. ICmpInst::ICMP_SLT,
  2541. this->gen_ext(
  2542. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  2543. 32, true),
  2544. this->gen_ext(
  2545. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  2546. 32, true)),
  2547. this->builder.CreateAdd(
  2548. this->gen_ext(
  2549. cur_pc_val,
  2550. 32, true),
  2551. this->gen_const(32U, imm)),
  2552. this->builder.CreateAdd(
  2553. cur_pc_val,
  2554. this->gen_const(32U, 4)),
  2555. 32);
  2556. this->builder.CreateStore(PC_val, get_reg_ptr(traits<ARCH>::NEXT_PC), false);
  2557. Value* is_cont_v = this->builder.CreateICmp(ICmpInst::ICMP_NE, PC_val, this->gen_const(32U, pc.val), "is_cont_v");
  2558. this->builder.CreateStore(this->gen_ext(is_cont_v, 32U, false), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  2559. this->gen_sync(POST_SYNC, 53);
  2560. this->gen_trap_check(this->leave_blk);
  2561. return std::make_tuple(BRANCH, nullptr);
  2562. }
  2563. /* instruction 54: BGE */
  2564. std::tuple<continuation_e, BasicBlock*> __bge(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2565. bb->setName("BGE");
  2566. this->gen_sync(PRE_SYNC, 54);
  2567. int16_t imm = signextend<int16_t,13>((bit_sub<7,1>(instr) << 11) | (bit_sub<8,4>(instr) << 1) | (bit_sub<25,6>(instr) << 5) | (bit_sub<31,1>(instr) << 12));
  2568. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2569. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  2570. if(this->disass_enabled){
  2571. /* generate console output when executing the command */
  2572. auto mnemonic = fmt::format(
  2573. "{mnemonic:10} {rs1}, {rs2}, {imm:#0x}", fmt::arg("mnemonic", "bge"),
  2574. fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)), fmt::arg("imm", imm));
  2575. std::vector<Value*> args {
  2576. this->core_ptr,
  2577. this->gen_const(64, pc.val),
  2578. this->builder.CreateGlobalStringPtr(mnemonic),
  2579. };
  2580. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2581. }
  2582. Value* cur_pc_val = this->gen_const(32, pc.val);
  2583. pc=pc+4;
  2584. Value* PC_val = this->gen_choose(
  2585. this->builder.CreateICmp(
  2586. ICmpInst::ICMP_SGE,
  2587. this->gen_ext(
  2588. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  2589. 32, true),
  2590. this->gen_ext(
  2591. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  2592. 32, true)),
  2593. this->builder.CreateAdd(
  2594. this->gen_ext(
  2595. cur_pc_val,
  2596. 32, true),
  2597. this->gen_const(32U, imm)),
  2598. this->builder.CreateAdd(
  2599. cur_pc_val,
  2600. this->gen_const(32U, 4)),
  2601. 32);
  2602. this->builder.CreateStore(PC_val, get_reg_ptr(traits<ARCH>::NEXT_PC), false);
  2603. Value* is_cont_v = this->builder.CreateICmp(ICmpInst::ICMP_NE, PC_val, this->gen_const(32U, pc.val), "is_cont_v");
  2604. this->builder.CreateStore(this->gen_ext(is_cont_v, 32U, false), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  2605. this->gen_sync(POST_SYNC, 54);
  2606. this->gen_trap_check(this->leave_blk);
  2607. return std::make_tuple(BRANCH, nullptr);
  2608. }
  2609. /* instruction 55: BLTU */
  2610. std::tuple<continuation_e, BasicBlock*> __bltu(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2611. bb->setName("BLTU");
  2612. this->gen_sync(PRE_SYNC, 55);
  2613. int16_t imm = signextend<int16_t,13>((bit_sub<7,1>(instr) << 11) | (bit_sub<8,4>(instr) << 1) | (bit_sub<25,6>(instr) << 5) | (bit_sub<31,1>(instr) << 12));
  2614. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2615. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  2616. if(this->disass_enabled){
  2617. /* generate console output when executing the command */
  2618. auto mnemonic = fmt::format(
  2619. "{mnemonic:10} {rs1}, {rs2}, {imm:#0x}", fmt::arg("mnemonic", "bltu"),
  2620. fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)), fmt::arg("imm", imm));
  2621. std::vector<Value*> args {
  2622. this->core_ptr,
  2623. this->gen_const(64, pc.val),
  2624. this->builder.CreateGlobalStringPtr(mnemonic),
  2625. };
  2626. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2627. }
  2628. Value* cur_pc_val = this->gen_const(32, pc.val);
  2629. pc=pc+4;
  2630. Value* PC_val = this->gen_choose(
  2631. this->builder.CreateICmp(
  2632. ICmpInst::ICMP_ULT,
  2633. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  2634. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0)),
  2635. this->builder.CreateAdd(
  2636. this->gen_ext(
  2637. cur_pc_val,
  2638. 32, true),
  2639. this->gen_const(32U, imm)),
  2640. this->builder.CreateAdd(
  2641. cur_pc_val,
  2642. this->gen_const(32U, 4)),
  2643. 32);
  2644. this->builder.CreateStore(PC_val, get_reg_ptr(traits<ARCH>::NEXT_PC), false);
  2645. Value* is_cont_v = this->builder.CreateICmp(ICmpInst::ICMP_NE, PC_val, this->gen_const(32U, pc.val), "is_cont_v");
  2646. this->builder.CreateStore(this->gen_ext(is_cont_v, 32U, false), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  2647. this->gen_sync(POST_SYNC, 55);
  2648. this->gen_trap_check(this->leave_blk);
  2649. return std::make_tuple(BRANCH, nullptr);
  2650. }
  2651. /* instruction 56: BGEU */
  2652. std::tuple<continuation_e, BasicBlock*> __bgeu(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2653. bb->setName("BGEU");
  2654. this->gen_sync(PRE_SYNC, 56);
  2655. int16_t imm = signextend<int16_t,13>((bit_sub<7,1>(instr) << 11) | (bit_sub<8,4>(instr) << 1) | (bit_sub<25,6>(instr) << 5) | (bit_sub<31,1>(instr) << 12));
  2656. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2657. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  2658. if(this->disass_enabled){
  2659. /* generate console output when executing the command */
  2660. auto mnemonic = fmt::format(
  2661. "{mnemonic:10} {rs1}, {rs2}, {imm:#0x}", fmt::arg("mnemonic", "bgeu"),
  2662. fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)), fmt::arg("imm", imm));
  2663. std::vector<Value*> args {
  2664. this->core_ptr,
  2665. this->gen_const(64, pc.val),
  2666. this->builder.CreateGlobalStringPtr(mnemonic),
  2667. };
  2668. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2669. }
  2670. Value* cur_pc_val = this->gen_const(32, pc.val);
  2671. pc=pc+4;
  2672. Value* PC_val = this->gen_choose(
  2673. this->builder.CreateICmp(
  2674. ICmpInst::ICMP_UGE,
  2675. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  2676. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0)),
  2677. this->builder.CreateAdd(
  2678. this->gen_ext(
  2679. cur_pc_val,
  2680. 32, true),
  2681. this->gen_const(32U, imm)),
  2682. this->builder.CreateAdd(
  2683. cur_pc_val,
  2684. this->gen_const(32U, 4)),
  2685. 32);
  2686. this->builder.CreateStore(PC_val, get_reg_ptr(traits<ARCH>::NEXT_PC), false);
  2687. Value* is_cont_v = this->builder.CreateICmp(ICmpInst::ICMP_NE, PC_val, this->gen_const(32U, pc.val), "is_cont_v");
  2688. this->builder.CreateStore(this->gen_ext(is_cont_v, 32U, false), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  2689. this->gen_sync(POST_SYNC, 56);
  2690. this->gen_trap_check(this->leave_blk);
  2691. return std::make_tuple(BRANCH, nullptr);
  2692. }
  2693. /* instruction 57: LB */
  2694. std::tuple<continuation_e, BasicBlock*> __lb(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2695. bb->setName("LB");
  2696. this->gen_sync(PRE_SYNC, 57);
  2697. uint8_t rd = ((bit_sub<7,5>(instr)));
  2698. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2699. int16_t imm = signextend<int16_t,12>((bit_sub<20,12>(instr)));
  2700. if(this->disass_enabled){
  2701. /* generate console output when executing the command */
  2702. auto mnemonic = fmt::format(
  2703. "{mnemonic:10} {rd}, {imm}({rs1})", fmt::arg("mnemonic", "lb"),
  2704. fmt::arg("rd", name(rd)), fmt::arg("imm", imm), fmt::arg("rs1", name(rs1)));
  2705. std::vector<Value*> args {
  2706. this->core_ptr,
  2707. this->gen_const(64, pc.val),
  2708. this->builder.CreateGlobalStringPtr(mnemonic),
  2709. };
  2710. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2711. }
  2712. Value* cur_pc_val = this->gen_const(32, pc.val);
  2713. pc=pc+4;
  2714. Value* offs_val = this->builder.CreateAdd(
  2715. this->gen_ext(
  2716. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  2717. 32, true),
  2718. this->gen_const(32U, imm));
  2719. if(rd != 0){
  2720. Value* Xtmp0_val = this->gen_ext(
  2721. this->gen_read_mem(traits<ARCH>::MEM, offs_val, 8/8),
  2722. 32,
  2723. true);
  2724. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2725. }
  2726. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2727. this->gen_sync(POST_SYNC, 57);
  2728. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2729. this->gen_trap_check(bb);
  2730. return std::make_tuple(CONT, bb);
  2731. }
  2732. /* instruction 58: LH */
  2733. std::tuple<continuation_e, BasicBlock*> __lh(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2734. bb->setName("LH");
  2735. this->gen_sync(PRE_SYNC, 58);
  2736. uint8_t rd = ((bit_sub<7,5>(instr)));
  2737. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2738. int16_t imm = signextend<int16_t,12>((bit_sub<20,12>(instr)));
  2739. if(this->disass_enabled){
  2740. /* generate console output when executing the command */
  2741. auto mnemonic = fmt::format(
  2742. "{mnemonic:10} {rd}, {imm}({rs1})", fmt::arg("mnemonic", "lh"),
  2743. fmt::arg("rd", name(rd)), fmt::arg("imm", imm), fmt::arg("rs1", name(rs1)));
  2744. std::vector<Value*> args {
  2745. this->core_ptr,
  2746. this->gen_const(64, pc.val),
  2747. this->builder.CreateGlobalStringPtr(mnemonic),
  2748. };
  2749. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2750. }
  2751. Value* cur_pc_val = this->gen_const(32, pc.val);
  2752. pc=pc+4;
  2753. Value* offs_val = this->builder.CreateAdd(
  2754. this->gen_ext(
  2755. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  2756. 32, true),
  2757. this->gen_const(32U, imm));
  2758. if(rd != 0){
  2759. Value* Xtmp0_val = this->gen_ext(
  2760. this->gen_read_mem(traits<ARCH>::MEM, offs_val, 16/8),
  2761. 32,
  2762. true);
  2763. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2764. }
  2765. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2766. this->gen_sync(POST_SYNC, 58);
  2767. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2768. this->gen_trap_check(bb);
  2769. return std::make_tuple(CONT, bb);
  2770. }
  2771. /* instruction 59: LW */
  2772. std::tuple<continuation_e, BasicBlock*> __lw(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2773. bb->setName("LW");
  2774. this->gen_sync(PRE_SYNC, 59);
  2775. uint8_t rd = ((bit_sub<7,5>(instr)));
  2776. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2777. int16_t imm = signextend<int16_t,12>((bit_sub<20,12>(instr)));
  2778. if(this->disass_enabled){
  2779. /* generate console output when executing the command */
  2780. auto mnemonic = fmt::format(
  2781. "{mnemonic:10} {rd}, {imm}({rs1})", fmt::arg("mnemonic", "lw"),
  2782. fmt::arg("rd", name(rd)), fmt::arg("imm", imm), fmt::arg("rs1", name(rs1)));
  2783. std::vector<Value*> args {
  2784. this->core_ptr,
  2785. this->gen_const(64, pc.val),
  2786. this->builder.CreateGlobalStringPtr(mnemonic),
  2787. };
  2788. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2789. }
  2790. Value* cur_pc_val = this->gen_const(32, pc.val);
  2791. pc=pc+4;
  2792. Value* offs_val = this->builder.CreateAdd(
  2793. this->gen_ext(
  2794. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  2795. 32, true),
  2796. this->gen_const(32U, imm));
  2797. if(rd != 0){
  2798. Value* Xtmp0_val = this->gen_ext(
  2799. this->gen_read_mem(traits<ARCH>::MEM, offs_val, 32/8),
  2800. 32,
  2801. true);
  2802. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2803. }
  2804. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2805. this->gen_sync(POST_SYNC, 59);
  2806. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2807. this->gen_trap_check(bb);
  2808. return std::make_tuple(CONT, bb);
  2809. }
  2810. /* instruction 60: LBU */
  2811. std::tuple<continuation_e, BasicBlock*> __lbu(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2812. bb->setName("LBU");
  2813. this->gen_sync(PRE_SYNC, 60);
  2814. uint8_t rd = ((bit_sub<7,5>(instr)));
  2815. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2816. int16_t imm = signextend<int16_t,12>((bit_sub<20,12>(instr)));
  2817. if(this->disass_enabled){
  2818. /* generate console output when executing the command */
  2819. auto mnemonic = fmt::format(
  2820. "{mnemonic:10} {rd}, {imm}({rs1})", fmt::arg("mnemonic", "lbu"),
  2821. fmt::arg("rd", name(rd)), fmt::arg("imm", imm), fmt::arg("rs1", name(rs1)));
  2822. std::vector<Value*> args {
  2823. this->core_ptr,
  2824. this->gen_const(64, pc.val),
  2825. this->builder.CreateGlobalStringPtr(mnemonic),
  2826. };
  2827. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2828. }
  2829. Value* cur_pc_val = this->gen_const(32, pc.val);
  2830. pc=pc+4;
  2831. Value* offs_val = this->builder.CreateAdd(
  2832. this->gen_ext(
  2833. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  2834. 32, true),
  2835. this->gen_const(32U, imm));
  2836. if(rd != 0){
  2837. Value* Xtmp0_val = this->gen_ext(
  2838. this->gen_read_mem(traits<ARCH>::MEM, offs_val, 8/8),
  2839. 32,
  2840. false);
  2841. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2842. }
  2843. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2844. this->gen_sync(POST_SYNC, 60);
  2845. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2846. this->gen_trap_check(bb);
  2847. return std::make_tuple(CONT, bb);
  2848. }
  2849. /* instruction 61: LHU */
  2850. std::tuple<continuation_e, BasicBlock*> __lhu(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2851. bb->setName("LHU");
  2852. this->gen_sync(PRE_SYNC, 61);
  2853. uint8_t rd = ((bit_sub<7,5>(instr)));
  2854. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2855. int16_t imm = signextend<int16_t,12>((bit_sub<20,12>(instr)));
  2856. if(this->disass_enabled){
  2857. /* generate console output when executing the command */
  2858. auto mnemonic = fmt::format(
  2859. "{mnemonic:10} {rd}, {imm}({rs1})", fmt::arg("mnemonic", "lhu"),
  2860. fmt::arg("rd", name(rd)), fmt::arg("imm", imm), fmt::arg("rs1", name(rs1)));
  2861. std::vector<Value*> args {
  2862. this->core_ptr,
  2863. this->gen_const(64, pc.val),
  2864. this->builder.CreateGlobalStringPtr(mnemonic),
  2865. };
  2866. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2867. }
  2868. Value* cur_pc_val = this->gen_const(32, pc.val);
  2869. pc=pc+4;
  2870. Value* offs_val = this->builder.CreateAdd(
  2871. this->gen_ext(
  2872. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  2873. 32, true),
  2874. this->gen_const(32U, imm));
  2875. if(rd != 0){
  2876. Value* Xtmp0_val = this->gen_ext(
  2877. this->gen_read_mem(traits<ARCH>::MEM, offs_val, 16/8),
  2878. 32,
  2879. false);
  2880. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2881. }
  2882. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2883. this->gen_sync(POST_SYNC, 61);
  2884. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2885. this->gen_trap_check(bb);
  2886. return std::make_tuple(CONT, bb);
  2887. }
  2888. /* instruction 62: SB */
  2889. std::tuple<continuation_e, BasicBlock*> __sb(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2890. bb->setName("SB");
  2891. this->gen_sync(PRE_SYNC, 62);
  2892. int16_t imm = signextend<int16_t,12>((bit_sub<7,5>(instr)) | (bit_sub<25,7>(instr) << 5));
  2893. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2894. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  2895. if(this->disass_enabled){
  2896. /* generate console output when executing the command */
  2897. auto mnemonic = fmt::format(
  2898. "{mnemonic:10} {rs2}, {imm}({rs1})", fmt::arg("mnemonic", "sb"),
  2899. fmt::arg("rs2", name(rs2)), fmt::arg("imm", imm), fmt::arg("rs1", name(rs1)));
  2900. std::vector<Value*> args {
  2901. this->core_ptr,
  2902. this->gen_const(64, pc.val),
  2903. this->builder.CreateGlobalStringPtr(mnemonic),
  2904. };
  2905. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2906. }
  2907. Value* cur_pc_val = this->gen_const(32, pc.val);
  2908. pc=pc+4;
  2909. Value* offs_val = this->builder.CreateAdd(
  2910. this->gen_ext(
  2911. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  2912. 32, true),
  2913. this->gen_const(32U, imm));
  2914. Value* MEMtmp0_val = this->gen_reg_load(rs2 + traits<ARCH>::X0, 0);
  2915. this->gen_write_mem(
  2916. traits<ARCH>::MEM,
  2917. offs_val,
  2918. this->builder.CreateZExtOrTrunc(MEMtmp0_val,this->get_type(8)));
  2919. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2920. this->gen_sync(POST_SYNC, 62);
  2921. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2922. this->gen_trap_check(bb);
  2923. return std::make_tuple(CONT, bb);
  2924. }
  2925. /* instruction 63: SH */
  2926. std::tuple<continuation_e, BasicBlock*> __sh(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2927. bb->setName("SH");
  2928. this->gen_sync(PRE_SYNC, 63);
  2929. int16_t imm = signextend<int16_t,12>((bit_sub<7,5>(instr)) | (bit_sub<25,7>(instr) << 5));
  2930. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2931. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  2932. if(this->disass_enabled){
  2933. /* generate console output when executing the command */
  2934. auto mnemonic = fmt::format(
  2935. "{mnemonic:10} {rs2}, {imm}({rs1})", fmt::arg("mnemonic", "sh"),
  2936. fmt::arg("rs2", name(rs2)), fmt::arg("imm", imm), fmt::arg("rs1", name(rs1)));
  2937. std::vector<Value*> args {
  2938. this->core_ptr,
  2939. this->gen_const(64, pc.val),
  2940. this->builder.CreateGlobalStringPtr(mnemonic),
  2941. };
  2942. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2943. }
  2944. Value* cur_pc_val = this->gen_const(32, pc.val);
  2945. pc=pc+4;
  2946. Value* offs_val = this->builder.CreateAdd(
  2947. this->gen_ext(
  2948. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  2949. 32, true),
  2950. this->gen_const(32U, imm));
  2951. Value* MEMtmp0_val = this->gen_reg_load(rs2 + traits<ARCH>::X0, 0);
  2952. this->gen_write_mem(
  2953. traits<ARCH>::MEM,
  2954. offs_val,
  2955. this->builder.CreateZExtOrTrunc(MEMtmp0_val,this->get_type(16)));
  2956. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2957. this->gen_sync(POST_SYNC, 63);
  2958. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2959. this->gen_trap_check(bb);
  2960. return std::make_tuple(CONT, bb);
  2961. }
  2962. /* instruction 64: SW */
  2963. std::tuple<continuation_e, BasicBlock*> __sw(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2964. bb->setName("SW");
  2965. this->gen_sync(PRE_SYNC, 64);
  2966. int16_t imm = signextend<int16_t,12>((bit_sub<7,5>(instr)) | (bit_sub<25,7>(instr) << 5));
  2967. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2968. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  2969. if(this->disass_enabled){
  2970. /* generate console output when executing the command */
  2971. auto mnemonic = fmt::format(
  2972. "{mnemonic:10} {rs2}, {imm}({rs1})", fmt::arg("mnemonic", "sw"),
  2973. fmt::arg("rs2", name(rs2)), fmt::arg("imm", imm), fmt::arg("rs1", name(rs1)));
  2974. std::vector<Value*> args {
  2975. this->core_ptr,
  2976. this->gen_const(64, pc.val),
  2977. this->builder.CreateGlobalStringPtr(mnemonic),
  2978. };
  2979. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2980. }
  2981. Value* cur_pc_val = this->gen_const(32, pc.val);
  2982. pc=pc+4;
  2983. Value* offs_val = this->builder.CreateAdd(
  2984. this->gen_ext(
  2985. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  2986. 32, true),
  2987. this->gen_const(32U, imm));
  2988. Value* MEMtmp0_val = this->gen_reg_load(rs2 + traits<ARCH>::X0, 0);
  2989. this->gen_write_mem(
  2990. traits<ARCH>::MEM,
  2991. offs_val,
  2992. this->builder.CreateZExtOrTrunc(MEMtmp0_val,this->get_type(32)));
  2993. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2994. this->gen_sync(POST_SYNC, 64);
  2995. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2996. this->gen_trap_check(bb);
  2997. return std::make_tuple(CONT, bb);
  2998. }
  2999. /* instruction 65: ADDI */
  3000. std::tuple<continuation_e, BasicBlock*> __addi(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3001. bb->setName("ADDI");
  3002. this->gen_sync(PRE_SYNC, 65);
  3003. uint8_t rd = ((bit_sub<7,5>(instr)));
  3004. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  3005. int16_t imm = signextend<int16_t,12>((bit_sub<20,12>(instr)));
  3006. if(this->disass_enabled){
  3007. /* generate console output when executing the command */
  3008. auto mnemonic = fmt::format(
  3009. "{mnemonic:10} {rd}, {rs1}, {imm}", fmt::arg("mnemonic", "addi"),
  3010. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("imm", imm));
  3011. std::vector<Value*> args {
  3012. this->core_ptr,
  3013. this->gen_const(64, pc.val),
  3014. this->builder.CreateGlobalStringPtr(mnemonic),
  3015. };
  3016. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3017. }
  3018. Value* cur_pc_val = this->gen_const(32, pc.val);
  3019. pc=pc+4;
  3020. if(rd != 0){
  3021. Value* Xtmp0_val = this->builder.CreateAdd(
  3022. this->gen_ext(
  3023. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  3024. 32, true),
  3025. this->gen_const(32U, imm));
  3026. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  3027. }
  3028. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  3029. this->gen_sync(POST_SYNC, 65);
  3030. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  3031. this->gen_trap_check(bb);
  3032. return std::make_tuple(CONT, bb);
  3033. }
  3034. /* instruction 66: SLTI */
  3035. std::tuple<continuation_e, BasicBlock*> __slti(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3036. bb->setName("SLTI");
  3037. this->gen_sync(PRE_SYNC, 66);
  3038. uint8_t rd = ((bit_sub<7,5>(instr)));
  3039. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  3040. int16_t imm = signextend<int16_t,12>((bit_sub<20,12>(instr)));
  3041. if(this->disass_enabled){
  3042. /* generate console output when executing the command */
  3043. auto mnemonic = fmt::format(
  3044. "{mnemonic:10} {rd}, {rs1}, {imm}", fmt::arg("mnemonic", "slti"),
  3045. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("imm", imm));
  3046. std::vector<Value*> args {
  3047. this->core_ptr,
  3048. this->gen_const(64, pc.val),
  3049. this->builder.CreateGlobalStringPtr(mnemonic),
  3050. };
  3051. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3052. }
  3053. Value* cur_pc_val = this->gen_const(32, pc.val);
  3054. pc=pc+4;
  3055. if(rd != 0){
  3056. Value* Xtmp0_val = this->gen_choose(
  3057. this->builder.CreateICmp(
  3058. ICmpInst::ICMP_SLT,
  3059. this->gen_ext(
  3060. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  3061. 32, true),
  3062. this->gen_const(32U, imm)),
  3063. this->gen_const(32U, 1),
  3064. this->gen_const(32U, 0),
  3065. 32);
  3066. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  3067. }
  3068. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  3069. this->gen_sync(POST_SYNC, 66);
  3070. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  3071. this->gen_trap_check(bb);
  3072. return std::make_tuple(CONT, bb);
  3073. }
  3074. /* instruction 67: SLTIU */
  3075. std::tuple<continuation_e, BasicBlock*> __sltiu(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3076. bb->setName("SLTIU");
  3077. this->gen_sync(PRE_SYNC, 67);
  3078. uint8_t rd = ((bit_sub<7,5>(instr)));
  3079. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  3080. int16_t imm = signextend<int16_t,12>((bit_sub<20,12>(instr)));
  3081. if(this->disass_enabled){
  3082. /* generate console output when executing the command */
  3083. auto mnemonic = fmt::format(
  3084. "{mnemonic:10} {rd}, {rs1}, {imm}", fmt::arg("mnemonic", "sltiu"),
  3085. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("imm", imm));
  3086. std::vector<Value*> args {
  3087. this->core_ptr,
  3088. this->gen_const(64, pc.val),
  3089. this->builder.CreateGlobalStringPtr(mnemonic),
  3090. };
  3091. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3092. }
  3093. Value* cur_pc_val = this->gen_const(32, pc.val);
  3094. pc=pc+4;
  3095. int32_t full_imm_val = imm;
  3096. if(rd != 0){
  3097. Value* Xtmp0_val = this->gen_choose(
  3098. this->builder.CreateICmp(
  3099. ICmpInst::ICMP_ULT,
  3100. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  3101. this->gen_const(32U, full_imm_val)),
  3102. this->gen_const(32U, 1),
  3103. this->gen_const(32U, 0),
  3104. 32);
  3105. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  3106. }
  3107. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  3108. this->gen_sync(POST_SYNC, 67);
  3109. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  3110. this->gen_trap_check(bb);
  3111. return std::make_tuple(CONT, bb);
  3112. }
  3113. /* instruction 68: XORI */
  3114. std::tuple<continuation_e, BasicBlock*> __xori(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3115. bb->setName("XORI");
  3116. this->gen_sync(PRE_SYNC, 68);
  3117. uint8_t rd = ((bit_sub<7,5>(instr)));
  3118. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  3119. int16_t imm = signextend<int16_t,12>((bit_sub<20,12>(instr)));
  3120. if(this->disass_enabled){
  3121. /* generate console output when executing the command */
  3122. auto mnemonic = fmt::format(
  3123. "{mnemonic:10} {rd}, {rs1}, {imm}", fmt::arg("mnemonic", "xori"),
  3124. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("imm", imm));
  3125. std::vector<Value*> args {
  3126. this->core_ptr,
  3127. this->gen_const(64, pc.val),
  3128. this->builder.CreateGlobalStringPtr(mnemonic),
  3129. };
  3130. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3131. }
  3132. Value* cur_pc_val = this->gen_const(32, pc.val);
  3133. pc=pc+4;
  3134. if(rd != 0){
  3135. Value* Xtmp0_val = this->builder.CreateXor(
  3136. this->gen_ext(
  3137. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  3138. 32, true),
  3139. this->gen_const(32U, imm));
  3140. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  3141. }
  3142. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  3143. this->gen_sync(POST_SYNC, 68);
  3144. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  3145. this->gen_trap_check(bb);
  3146. return std::make_tuple(CONT, bb);
  3147. }
  3148. /* instruction 69: ORI */
  3149. std::tuple<continuation_e, BasicBlock*> __ori(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3150. bb->setName("ORI");
  3151. this->gen_sync(PRE_SYNC, 69);
  3152. uint8_t rd = ((bit_sub<7,5>(instr)));
  3153. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  3154. int16_t imm = signextend<int16_t,12>((bit_sub<20,12>(instr)));
  3155. if(this->disass_enabled){
  3156. /* generate console output when executing the command */
  3157. auto mnemonic = fmt::format(
  3158. "{mnemonic:10} {rd}, {rs1}, {imm}", fmt::arg("mnemonic", "ori"),
  3159. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("imm", imm));
  3160. std::vector<Value*> args {
  3161. this->core_ptr,
  3162. this->gen_const(64, pc.val),
  3163. this->builder.CreateGlobalStringPtr(mnemonic),
  3164. };
  3165. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3166. }
  3167. Value* cur_pc_val = this->gen_const(32, pc.val);
  3168. pc=pc+4;
  3169. if(rd != 0){
  3170. Value* Xtmp0_val = this->builder.CreateOr(
  3171. this->gen_ext(
  3172. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  3173. 32, true),
  3174. this->gen_const(32U, imm));
  3175. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  3176. }
  3177. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  3178. this->gen_sync(POST_SYNC, 69);
  3179. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  3180. this->gen_trap_check(bb);
  3181. return std::make_tuple(CONT, bb);
  3182. }
  3183. /* instruction 70: ANDI */
  3184. std::tuple<continuation_e, BasicBlock*> __andi(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3185. bb->setName("ANDI");
  3186. this->gen_sync(PRE_SYNC, 70);
  3187. uint8_t rd = ((bit_sub<7,5>(instr)));
  3188. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  3189. int16_t imm = signextend<int16_t,12>((bit_sub<20,12>(instr)));
  3190. if(this->disass_enabled){
  3191. /* generate console output when executing the command */
  3192. auto mnemonic = fmt::format(
  3193. "{mnemonic:10} {rd}, {rs1}, {imm}", fmt::arg("mnemonic", "andi"),
  3194. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("imm", imm));
  3195. std::vector<Value*> args {
  3196. this->core_ptr,
  3197. this->gen_const(64, pc.val),
  3198. this->builder.CreateGlobalStringPtr(mnemonic),
  3199. };
  3200. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3201. }
  3202. Value* cur_pc_val = this->gen_const(32, pc.val);
  3203. pc=pc+4;
  3204. if(rd != 0){
  3205. Value* Xtmp0_val = this->builder.CreateAnd(
  3206. this->gen_ext(
  3207. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  3208. 32, true),
  3209. this->gen_const(32U, imm));
  3210. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  3211. }
  3212. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  3213. this->gen_sync(POST_SYNC, 70);
  3214. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  3215. this->gen_trap_check(bb);
  3216. return std::make_tuple(CONT, bb);
  3217. }
  3218. /* instruction 71: SLLI */
  3219. std::tuple<continuation_e, BasicBlock*> __slli(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3220. bb->setName("SLLI");
  3221. this->gen_sync(PRE_SYNC, 71);
  3222. uint8_t rd = ((bit_sub<7,5>(instr)));
  3223. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  3224. uint8_t shamt = ((bit_sub<20,5>(instr)));
  3225. if(this->disass_enabled){
  3226. /* generate console output when executing the command */
  3227. auto mnemonic = fmt::format(
  3228. "{mnemonic:10} {rd}, {rs1}, {shamt}", fmt::arg("mnemonic", "slli"),
  3229. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("shamt", shamt));
  3230. std::vector<Value*> args {
  3231. this->core_ptr,
  3232. this->gen_const(64, pc.val),
  3233. this->builder.CreateGlobalStringPtr(mnemonic),
  3234. };
  3235. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3236. }
  3237. Value* cur_pc_val = this->gen_const(32, pc.val);
  3238. pc=pc+4;
  3239. if(shamt > 31){
  3240. this->gen_raise_trap(0, 0);
  3241. } else {
  3242. if(rd != 0){
  3243. Value* Xtmp0_val = this->builder.CreateShl(
  3244. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  3245. this->gen_const(32U, shamt));
  3246. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  3247. }
  3248. }
  3249. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  3250. this->gen_sync(POST_SYNC, 71);
  3251. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  3252. this->gen_trap_check(bb);
  3253. return std::make_tuple(CONT, bb);
  3254. }
  3255. /* instruction 72: SRLI */
  3256. std::tuple<continuation_e, BasicBlock*> __srli(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3257. bb->setName("SRLI");
  3258. this->gen_sync(PRE_SYNC, 72);
  3259. uint8_t rd = ((bit_sub<7,5>(instr)));
  3260. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  3261. uint8_t shamt = ((bit_sub<20,5>(instr)));
  3262. if(this->disass_enabled){
  3263. /* generate console output when executing the command */
  3264. auto mnemonic = fmt::format(
  3265. "{mnemonic:10} {rd}, {rs1}, {shamt}", fmt::arg("mnemonic", "srli"),
  3266. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("shamt", shamt));
  3267. std::vector<Value*> args {
  3268. this->core_ptr,
  3269. this->gen_const(64, pc.val),
  3270. this->builder.CreateGlobalStringPtr(mnemonic),
  3271. };
  3272. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3273. }
  3274. Value* cur_pc_val = this->gen_const(32, pc.val);
  3275. pc=pc+4;
  3276. if(shamt > 31){
  3277. this->gen_raise_trap(0, 0);
  3278. } else {
  3279. if(rd != 0){
  3280. Value* Xtmp0_val = this->builder.CreateLShr(
  3281. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  3282. this->gen_const(32U, shamt));
  3283. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  3284. }
  3285. }
  3286. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  3287. this->gen_sync(POST_SYNC, 72);
  3288. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  3289. this->gen_trap_check(bb);
  3290. return std::make_tuple(CONT, bb);
  3291. }
  3292. /* instruction 73: SRAI */
  3293. std::tuple<continuation_e, BasicBlock*> __srai(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3294. bb->setName("SRAI");
  3295. this->gen_sync(PRE_SYNC, 73);
  3296. uint8_t rd = ((bit_sub<7,5>(instr)));
  3297. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  3298. uint8_t shamt = ((bit_sub<20,5>(instr)));
  3299. if(this->disass_enabled){
  3300. /* generate console output when executing the command */
  3301. auto mnemonic = fmt::format(
  3302. "{mnemonic:10} {rd}, {rs1}, {shamt}", fmt::arg("mnemonic", "srai"),
  3303. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("shamt", shamt));
  3304. std::vector<Value*> args {
  3305. this->core_ptr,
  3306. this->gen_const(64, pc.val),
  3307. this->builder.CreateGlobalStringPtr(mnemonic),
  3308. };
  3309. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3310. }
  3311. Value* cur_pc_val = this->gen_const(32, pc.val);
  3312. pc=pc+4;
  3313. if(shamt > 31){
  3314. this->gen_raise_trap(0, 0);
  3315. } else {
  3316. if(rd != 0){
  3317. Value* Xtmp0_val = this->builder.CreateAShr(
  3318. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  3319. this->gen_const(32U, shamt));
  3320. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  3321. }
  3322. }
  3323. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  3324. this->gen_sync(POST_SYNC, 73);
  3325. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  3326. this->gen_trap_check(bb);
  3327. return std::make_tuple(CONT, bb);
  3328. }
  3329. /* instruction 74: ADD */
  3330. std::tuple<continuation_e, BasicBlock*> __add(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3331. bb->setName("ADD");
  3332. this->gen_sync(PRE_SYNC, 74);
  3333. uint8_t rd = ((bit_sub<7,5>(instr)));
  3334. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  3335. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  3336. if(this->disass_enabled){
  3337. /* generate console output when executing the command */
  3338. auto mnemonic = fmt::format(
  3339. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "add"),
  3340. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  3341. std::vector<Value*> args {
  3342. this->core_ptr,
  3343. this->gen_const(64, pc.val),
  3344. this->builder.CreateGlobalStringPtr(mnemonic),
  3345. };
  3346. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3347. }
  3348. Value* cur_pc_val = this->gen_const(32, pc.val);
  3349. pc=pc+4;
  3350. if(rd != 0){
  3351. Value* Xtmp0_val = this->builder.CreateAdd(
  3352. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  3353. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0));
  3354. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  3355. }
  3356. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  3357. this->gen_sync(POST_SYNC, 74);
  3358. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  3359. this->gen_trap_check(bb);
  3360. return std::make_tuple(CONT, bb);
  3361. }
  3362. /* instruction 75: SUB */
  3363. std::tuple<continuation_e, BasicBlock*> __sub(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3364. bb->setName("SUB");
  3365. this->gen_sync(PRE_SYNC, 75);
  3366. uint8_t rd = ((bit_sub<7,5>(instr)));
  3367. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  3368. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  3369. if(this->disass_enabled){
  3370. /* generate console output when executing the command */
  3371. auto mnemonic = fmt::format(
  3372. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "sub"),
  3373. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  3374. std::vector<Value*> args {
  3375. this->core_ptr,
  3376. this->gen_const(64, pc.val),
  3377. this->builder.CreateGlobalStringPtr(mnemonic),
  3378. };
  3379. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3380. }
  3381. Value* cur_pc_val = this->gen_const(32, pc.val);
  3382. pc=pc+4;
  3383. if(rd != 0){
  3384. Value* Xtmp0_val = this->builder.CreateSub(
  3385. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  3386. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0));
  3387. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  3388. }
  3389. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  3390. this->gen_sync(POST_SYNC, 75);
  3391. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  3392. this->gen_trap_check(bb);
  3393. return std::make_tuple(CONT, bb);
  3394. }
  3395. /* instruction 76: SLL */
  3396. std::tuple<continuation_e, BasicBlock*> __sll(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3397. bb->setName("SLL");
  3398. this->gen_sync(PRE_SYNC, 76);
  3399. uint8_t rd = ((bit_sub<7,5>(instr)));
  3400. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  3401. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  3402. if(this->disass_enabled){
  3403. /* generate console output when executing the command */
  3404. auto mnemonic = fmt::format(
  3405. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "sll"),
  3406. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  3407. std::vector<Value*> args {
  3408. this->core_ptr,
  3409. this->gen_const(64, pc.val),
  3410. this->builder.CreateGlobalStringPtr(mnemonic),
  3411. };
  3412. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3413. }
  3414. Value* cur_pc_val = this->gen_const(32, pc.val);
  3415. pc=pc+4;
  3416. if(rd != 0){
  3417. Value* Xtmp0_val = this->builder.CreateShl(
  3418. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  3419. this->builder.CreateAnd(
  3420. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  3421. this->builder.CreateSub(
  3422. this->gen_const(32U, 32),
  3423. this->gen_const(32U, 1))));
  3424. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  3425. }
  3426. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  3427. this->gen_sync(POST_SYNC, 76);
  3428. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  3429. this->gen_trap_check(bb);
  3430. return std::make_tuple(CONT, bb);
  3431. }
  3432. /* instruction 77: SLT */
  3433. std::tuple<continuation_e, BasicBlock*> __slt(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3434. bb->setName("SLT");
  3435. this->gen_sync(PRE_SYNC, 77);
  3436. uint8_t rd = ((bit_sub<7,5>(instr)));
  3437. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  3438. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  3439. if(this->disass_enabled){
  3440. /* generate console output when executing the command */
  3441. auto mnemonic = fmt::format(
  3442. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "slt"),
  3443. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  3444. std::vector<Value*> args {
  3445. this->core_ptr,
  3446. this->gen_const(64, pc.val),
  3447. this->builder.CreateGlobalStringPtr(mnemonic),
  3448. };
  3449. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3450. }
  3451. Value* cur_pc_val = this->gen_const(32, pc.val);
  3452. pc=pc+4;
  3453. if(rd != 0){
  3454. Value* Xtmp0_val = this->gen_choose(
  3455. this->builder.CreateICmp(
  3456. ICmpInst::ICMP_SLT,
  3457. this->gen_ext(
  3458. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  3459. 32, true),
  3460. this->gen_ext(
  3461. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  3462. 32, true)),
  3463. this->gen_const(32U, 1),
  3464. this->gen_const(32U, 0),
  3465. 32);
  3466. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  3467. }
  3468. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  3469. this->gen_sync(POST_SYNC, 77);
  3470. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  3471. this->gen_trap_check(bb);
  3472. return std::make_tuple(CONT, bb);
  3473. }
  3474. /* instruction 78: SLTU */
  3475. std::tuple<continuation_e, BasicBlock*> __sltu(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3476. bb->setName("SLTU");
  3477. this->gen_sync(PRE_SYNC, 78);
  3478. uint8_t rd = ((bit_sub<7,5>(instr)));
  3479. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  3480. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  3481. if(this->disass_enabled){
  3482. /* generate console output when executing the command */
  3483. auto mnemonic = fmt::format(
  3484. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "sltu"),
  3485. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  3486. std::vector<Value*> args {
  3487. this->core_ptr,
  3488. this->gen_const(64, pc.val),
  3489. this->builder.CreateGlobalStringPtr(mnemonic),
  3490. };
  3491. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3492. }
  3493. Value* cur_pc_val = this->gen_const(32, pc.val);
  3494. pc=pc+4;
  3495. if(rd != 0){
  3496. Value* Xtmp0_val = this->gen_choose(
  3497. this->builder.CreateICmp(
  3498. ICmpInst::ICMP_ULT,
  3499. this->gen_ext(
  3500. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  3501. 32,
  3502. false),
  3503. this->gen_ext(
  3504. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  3505. 32,
  3506. false)),
  3507. this->gen_const(32U, 1),
  3508. this->gen_const(32U, 0),
  3509. 32);
  3510. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  3511. }
  3512. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  3513. this->gen_sync(POST_SYNC, 78);
  3514. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  3515. this->gen_trap_check(bb);
  3516. return std::make_tuple(CONT, bb);
  3517. }
  3518. /* instruction 79: XOR */
  3519. std::tuple<continuation_e, BasicBlock*> __xor(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3520. bb->setName("XOR");
  3521. this->gen_sync(PRE_SYNC, 79);
  3522. uint8_t rd = ((bit_sub<7,5>(instr)));
  3523. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  3524. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  3525. if(this->disass_enabled){
  3526. /* generate console output when executing the command */
  3527. auto mnemonic = fmt::format(
  3528. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "xor"),
  3529. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  3530. std::vector<Value*> args {
  3531. this->core_ptr,
  3532. this->gen_const(64, pc.val),
  3533. this->builder.CreateGlobalStringPtr(mnemonic),
  3534. };
  3535. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3536. }
  3537. Value* cur_pc_val = this->gen_const(32, pc.val);
  3538. pc=pc+4;
  3539. if(rd != 0){
  3540. Value* Xtmp0_val = this->builder.CreateXor(
  3541. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  3542. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0));
  3543. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  3544. }
  3545. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  3546. this->gen_sync(POST_SYNC, 79);
  3547. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  3548. this->gen_trap_check(bb);
  3549. return std::make_tuple(CONT, bb);
  3550. }
  3551. /* instruction 80: SRL */
  3552. std::tuple<continuation_e, BasicBlock*> __srl(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3553. bb->setName("SRL");
  3554. this->gen_sync(PRE_SYNC, 80);
  3555. uint8_t rd = ((bit_sub<7,5>(instr)));
  3556. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  3557. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  3558. if(this->disass_enabled){
  3559. /* generate console output when executing the command */
  3560. auto mnemonic = fmt::format(
  3561. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "srl"),
  3562. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  3563. std::vector<Value*> args {
  3564. this->core_ptr,
  3565. this->gen_const(64, pc.val),
  3566. this->builder.CreateGlobalStringPtr(mnemonic),
  3567. };
  3568. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3569. }
  3570. Value* cur_pc_val = this->gen_const(32, pc.val);
  3571. pc=pc+4;
  3572. if(rd != 0){
  3573. Value* Xtmp0_val = this->builder.CreateLShr(
  3574. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  3575. this->builder.CreateAnd(
  3576. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  3577. this->builder.CreateSub(
  3578. this->gen_const(32U, 32),
  3579. this->gen_const(32U, 1))));
  3580. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  3581. }
  3582. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  3583. this->gen_sync(POST_SYNC, 80);
  3584. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  3585. this->gen_trap_check(bb);
  3586. return std::make_tuple(CONT, bb);
  3587. }
  3588. /* instruction 81: SRA */
  3589. std::tuple<continuation_e, BasicBlock*> __sra(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3590. bb->setName("SRA");
  3591. this->gen_sync(PRE_SYNC, 81);
  3592. uint8_t rd = ((bit_sub<7,5>(instr)));
  3593. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  3594. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  3595. if(this->disass_enabled){
  3596. /* generate console output when executing the command */
  3597. auto mnemonic = fmt::format(
  3598. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "sra"),
  3599. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  3600. std::vector<Value*> args {
  3601. this->core_ptr,
  3602. this->gen_const(64, pc.val),
  3603. this->builder.CreateGlobalStringPtr(mnemonic),
  3604. };
  3605. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3606. }
  3607. Value* cur_pc_val = this->gen_const(32, pc.val);
  3608. pc=pc+4;
  3609. if(rd != 0){
  3610. Value* Xtmp0_val = this->builder.CreateAShr(
  3611. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  3612. this->builder.CreateAnd(
  3613. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  3614. this->builder.CreateSub(
  3615. this->gen_const(32U, 32),
  3616. this->gen_const(32U, 1))));
  3617. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  3618. }
  3619. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  3620. this->gen_sync(POST_SYNC, 81);
  3621. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  3622. this->gen_trap_check(bb);
  3623. return std::make_tuple(CONT, bb);
  3624. }
  3625. /* instruction 82: OR */
  3626. std::tuple<continuation_e, BasicBlock*> __or(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3627. bb->setName("OR");
  3628. this->gen_sync(PRE_SYNC, 82);
  3629. uint8_t rd = ((bit_sub<7,5>(instr)));
  3630. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  3631. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  3632. if(this->disass_enabled){
  3633. /* generate console output when executing the command */
  3634. auto mnemonic = fmt::format(
  3635. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "or"),
  3636. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  3637. std::vector<Value*> args {
  3638. this->core_ptr,
  3639. this->gen_const(64, pc.val),
  3640. this->builder.CreateGlobalStringPtr(mnemonic),
  3641. };
  3642. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3643. }
  3644. Value* cur_pc_val = this->gen_const(32, pc.val);
  3645. pc=pc+4;
  3646. if(rd != 0){
  3647. Value* Xtmp0_val = this->builder.CreateOr(
  3648. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  3649. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0));
  3650. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  3651. }
  3652. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  3653. this->gen_sync(POST_SYNC, 82);
  3654. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  3655. this->gen_trap_check(bb);
  3656. return std::make_tuple(CONT, bb);
  3657. }
  3658. /* instruction 83: AND */
  3659. std::tuple<continuation_e, BasicBlock*> __and(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3660. bb->setName("AND");
  3661. this->gen_sync(PRE_SYNC, 83);
  3662. uint8_t rd = ((bit_sub<7,5>(instr)));
  3663. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  3664. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  3665. if(this->disass_enabled){
  3666. /* generate console output when executing the command */
  3667. auto mnemonic = fmt::format(
  3668. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "and"),
  3669. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  3670. std::vector<Value*> args {
  3671. this->core_ptr,
  3672. this->gen_const(64, pc.val),
  3673. this->builder.CreateGlobalStringPtr(mnemonic),
  3674. };
  3675. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3676. }
  3677. Value* cur_pc_val = this->gen_const(32, pc.val);
  3678. pc=pc+4;
  3679. if(rd != 0){
  3680. Value* Xtmp0_val = this->builder.CreateAnd(
  3681. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  3682. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0));
  3683. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  3684. }
  3685. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  3686. this->gen_sync(POST_SYNC, 83);
  3687. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  3688. this->gen_trap_check(bb);
  3689. return std::make_tuple(CONT, bb);
  3690. }
  3691. /* instruction 84: FENCE */
  3692. std::tuple<continuation_e, BasicBlock*> __fence(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3693. bb->setName("FENCE");
  3694. this->gen_sync(PRE_SYNC, 84);
  3695. uint8_t rd = ((bit_sub<7,5>(instr)));
  3696. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  3697. uint8_t succ = ((bit_sub<20,4>(instr)));
  3698. uint8_t pred = ((bit_sub<24,4>(instr)));
  3699. if(this->disass_enabled){
  3700. /* generate console output when executing the command */
  3701. std::vector<Value*> args {
  3702. this->core_ptr,
  3703. this->gen_const(64, pc.val),
  3704. this->builder.CreateGlobalStringPtr("fence"),
  3705. };
  3706. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3707. }
  3708. Value* cur_pc_val = this->gen_const(32, pc.val);
  3709. pc=pc+4;
  3710. Value* FENCEtmp0_val = this->builder.CreateOr(
  3711. this->builder.CreateShl(
  3712. this->gen_const(32U, pred),
  3713. this->gen_const(32U, 4)),
  3714. this->gen_const(32U, succ));
  3715. this->gen_write_mem(
  3716. traits<ARCH>::FENCE,
  3717. this->gen_const(64U, 0),
  3718. this->builder.CreateZExtOrTrunc(FENCEtmp0_val,this->get_type(32)));
  3719. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  3720. this->gen_sync(POST_SYNC, 84);
  3721. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  3722. this->gen_trap_check(bb);
  3723. return std::make_tuple(CONT, bb);
  3724. }
  3725. /* instruction 85: FENCE_I */
  3726. std::tuple<continuation_e, BasicBlock*> __fence_i(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3727. bb->setName("FENCE_I");
  3728. this->gen_sync(PRE_SYNC, 85);
  3729. uint8_t rd = ((bit_sub<7,5>(instr)));
  3730. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  3731. uint16_t imm = ((bit_sub<20,12>(instr)));
  3732. if(this->disass_enabled){
  3733. /* generate console output when executing the command */
  3734. std::vector<Value*> args {
  3735. this->core_ptr,
  3736. this->gen_const(64, pc.val),
  3737. this->builder.CreateGlobalStringPtr("fence_i"),
  3738. };
  3739. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3740. }
  3741. Value* cur_pc_val = this->gen_const(32, pc.val);
  3742. pc=pc+4;
  3743. Value* FENCEtmp0_val = this->gen_const(32U, imm);
  3744. this->gen_write_mem(
  3745. traits<ARCH>::FENCE,
  3746. this->gen_const(64U, 1),
  3747. this->builder.CreateZExtOrTrunc(FENCEtmp0_val,this->get_type(32)));
  3748. this->builder.CreateStore(this->gen_const(32U, std::numeric_limits<uint32_t>::max()), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  3749. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  3750. this->gen_sync(POST_SYNC, 85);
  3751. this->gen_trap_check(this->leave_blk);
  3752. return std::make_tuple(FLUSH, nullptr);
  3753. }
  3754. /* instruction 86: ECALL */
  3755. std::tuple<continuation_e, BasicBlock*> __ecall(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3756. bb->setName("ECALL");
  3757. this->gen_sync(PRE_SYNC, 86);
  3758. if(this->disass_enabled){
  3759. /* generate console output when executing the command */
  3760. std::vector<Value*> args {
  3761. this->core_ptr,
  3762. this->gen_const(64, pc.val),
  3763. this->builder.CreateGlobalStringPtr("ecall"),
  3764. };
  3765. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3766. }
  3767. Value* cur_pc_val = this->gen_const(32, pc.val);
  3768. pc=pc+4;
  3769. this->gen_raise_trap(0, 11);
  3770. this->gen_sync(POST_SYNC, 86);
  3771. this->gen_trap_check(this->leave_blk);
  3772. return std::make_tuple(BRANCH, nullptr);
  3773. }
  3774. /* instruction 87: EBREAK */
  3775. std::tuple<continuation_e, BasicBlock*> __ebreak(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3776. bb->setName("EBREAK");
  3777. this->gen_sync(PRE_SYNC, 87);
  3778. if(this->disass_enabled){
  3779. /* generate console output when executing the command */
  3780. std::vector<Value*> args {
  3781. this->core_ptr,
  3782. this->gen_const(64, pc.val),
  3783. this->builder.CreateGlobalStringPtr("ebreak"),
  3784. };
  3785. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3786. }
  3787. Value* cur_pc_val = this->gen_const(32, pc.val);
  3788. pc=pc+4;
  3789. this->gen_raise_trap(0, 3);
  3790. this->gen_sync(POST_SYNC, 87);
  3791. this->gen_trap_check(this->leave_blk);
  3792. return std::make_tuple(BRANCH, nullptr);
  3793. }
  3794. /* instruction 88: URET */
  3795. std::tuple<continuation_e, BasicBlock*> __uret(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3796. bb->setName("URET");
  3797. this->gen_sync(PRE_SYNC, 88);
  3798. if(this->disass_enabled){
  3799. /* generate console output when executing the command */
  3800. std::vector<Value*> args {
  3801. this->core_ptr,
  3802. this->gen_const(64, pc.val),
  3803. this->builder.CreateGlobalStringPtr("uret"),
  3804. };
  3805. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3806. }
  3807. Value* cur_pc_val = this->gen_const(32, pc.val);
  3808. pc=pc+4;
  3809. this->gen_leave_trap(0);
  3810. this->gen_sync(POST_SYNC, 88);
  3811. this->gen_trap_check(this->leave_blk);
  3812. return std::make_tuple(BRANCH, nullptr);
  3813. }
  3814. /* instruction 89: SRET */
  3815. std::tuple<continuation_e, BasicBlock*> __sret(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3816. bb->setName("SRET");
  3817. this->gen_sync(PRE_SYNC, 89);
  3818. if(this->disass_enabled){
  3819. /* generate console output when executing the command */
  3820. std::vector<Value*> args {
  3821. this->core_ptr,
  3822. this->gen_const(64, pc.val),
  3823. this->builder.CreateGlobalStringPtr("sret"),
  3824. };
  3825. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3826. }
  3827. Value* cur_pc_val = this->gen_const(32, pc.val);
  3828. pc=pc+4;
  3829. this->gen_leave_trap(1);
  3830. this->gen_sync(POST_SYNC, 89);
  3831. this->gen_trap_check(this->leave_blk);
  3832. return std::make_tuple(BRANCH, nullptr);
  3833. }
  3834. /* instruction 90: MRET */
  3835. std::tuple<continuation_e, BasicBlock*> __mret(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3836. bb->setName("MRET");
  3837. this->gen_sync(PRE_SYNC, 90);
  3838. if(this->disass_enabled){
  3839. /* generate console output when executing the command */
  3840. std::vector<Value*> args {
  3841. this->core_ptr,
  3842. this->gen_const(64, pc.val),
  3843. this->builder.CreateGlobalStringPtr("mret"),
  3844. };
  3845. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3846. }
  3847. Value* cur_pc_val = this->gen_const(32, pc.val);
  3848. pc=pc+4;
  3849. this->gen_leave_trap(3);
  3850. this->gen_sync(POST_SYNC, 90);
  3851. this->gen_trap_check(this->leave_blk);
  3852. return std::make_tuple(BRANCH, nullptr);
  3853. }
  3854. /* instruction 91: WFI */
  3855. std::tuple<continuation_e, BasicBlock*> __wfi(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3856. bb->setName("WFI");
  3857. this->gen_sync(PRE_SYNC, 91);
  3858. if(this->disass_enabled){
  3859. /* generate console output when executing the command */
  3860. std::vector<Value*> args {
  3861. this->core_ptr,
  3862. this->gen_const(64, pc.val),
  3863. this->builder.CreateGlobalStringPtr("wfi"),
  3864. };
  3865. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3866. }
  3867. Value* cur_pc_val = this->gen_const(32, pc.val);
  3868. pc=pc+4;
  3869. this->gen_wait(1);
  3870. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  3871. this->gen_sync(POST_SYNC, 91);
  3872. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  3873. this->gen_trap_check(bb);
  3874. return std::make_tuple(CONT, bb);
  3875. }
  3876. /* instruction 92: SFENCE.VMA */
  3877. std::tuple<continuation_e, BasicBlock*> __sfence_vma(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3878. bb->setName("SFENCE.VMA");
  3879. this->gen_sync(PRE_SYNC, 92);
  3880. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  3881. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  3882. if(this->disass_enabled){
  3883. /* generate console output when executing the command */
  3884. std::vector<Value*> args {
  3885. this->core_ptr,
  3886. this->gen_const(64, pc.val),
  3887. this->builder.CreateGlobalStringPtr("sfence.vma"),
  3888. };
  3889. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3890. }
  3891. Value* cur_pc_val = this->gen_const(32, pc.val);
  3892. pc=pc+4;
  3893. Value* FENCEtmp0_val = this->gen_const(32U, rs1);
  3894. this->gen_write_mem(
  3895. traits<ARCH>::FENCE,
  3896. this->gen_const(64U, 2),
  3897. this->builder.CreateZExtOrTrunc(FENCEtmp0_val,this->get_type(32)));
  3898. Value* FENCEtmp1_val = this->gen_const(32U, rs2);
  3899. this->gen_write_mem(
  3900. traits<ARCH>::FENCE,
  3901. this->gen_const(64U, 3),
  3902. this->builder.CreateZExtOrTrunc(FENCEtmp1_val,this->get_type(32)));
  3903. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  3904. this->gen_sync(POST_SYNC, 92);
  3905. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  3906. this->gen_trap_check(bb);
  3907. return std::make_tuple(CONT, bb);
  3908. }
  3909. /* instruction 93: CSRRW */
  3910. std::tuple<continuation_e, BasicBlock*> __csrrw(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3911. bb->setName("CSRRW");
  3912. this->gen_sync(PRE_SYNC, 93);
  3913. uint8_t rd = ((bit_sub<7,5>(instr)));
  3914. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  3915. uint16_t csr = ((bit_sub<20,12>(instr)));
  3916. if(this->disass_enabled){
  3917. /* generate console output when executing the command */
  3918. auto mnemonic = fmt::format(
  3919. "{mnemonic:10} {rd}, {csr}, {rs1}", fmt::arg("mnemonic", "csrrw"),
  3920. fmt::arg("rd", name(rd)), fmt::arg("csr", csr), fmt::arg("rs1", name(rs1)));
  3921. std::vector<Value*> args {
  3922. this->core_ptr,
  3923. this->gen_const(64, pc.val),
  3924. this->builder.CreateGlobalStringPtr(mnemonic),
  3925. };
  3926. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3927. }
  3928. Value* cur_pc_val = this->gen_const(32, pc.val);
  3929. pc=pc+4;
  3930. Value* rs_val_val = this->gen_reg_load(rs1 + traits<ARCH>::X0, 0);
  3931. if(rd != 0){
  3932. Value* csr_val_val = this->gen_read_mem(traits<ARCH>::CSR, this->gen_const(16U, csr), 32/8);
  3933. Value* CSRtmp0_val = rs_val_val;
  3934. this->gen_write_mem(
  3935. traits<ARCH>::CSR,
  3936. this->gen_const(16U, csr),
  3937. this->builder.CreateZExtOrTrunc(CSRtmp0_val,this->get_type(32)));
  3938. Value* Xtmp1_val = csr_val_val;
  3939. this->builder.CreateStore(Xtmp1_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  3940. } else {
  3941. Value* CSRtmp2_val = rs_val_val;
  3942. this->gen_write_mem(
  3943. traits<ARCH>::CSR,
  3944. this->gen_const(16U, csr),
  3945. this->builder.CreateZExtOrTrunc(CSRtmp2_val,this->get_type(32)));
  3946. }
  3947. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  3948. this->gen_sync(POST_SYNC, 93);
  3949. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  3950. this->gen_trap_check(bb);
  3951. return std::make_tuple(CONT, bb);
  3952. }
  3953. /* instruction 94: CSRRS */
  3954. std::tuple<continuation_e, BasicBlock*> __csrrs(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3955. bb->setName("CSRRS");
  3956. this->gen_sync(PRE_SYNC, 94);
  3957. uint8_t rd = ((bit_sub<7,5>(instr)));
  3958. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  3959. uint16_t csr = ((bit_sub<20,12>(instr)));
  3960. if(this->disass_enabled){
  3961. /* generate console output when executing the command */
  3962. auto mnemonic = fmt::format(
  3963. "{mnemonic:10} {rd}, {csr}, {rs1}", fmt::arg("mnemonic", "csrrs"),
  3964. fmt::arg("rd", name(rd)), fmt::arg("csr", csr), fmt::arg("rs1", name(rs1)));
  3965. std::vector<Value*> args {
  3966. this->core_ptr,
  3967. this->gen_const(64, pc.val),
  3968. this->builder.CreateGlobalStringPtr(mnemonic),
  3969. };
  3970. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  3971. }
  3972. Value* cur_pc_val = this->gen_const(32, pc.val);
  3973. pc=pc+4;
  3974. Value* xrd_val = this->gen_read_mem(traits<ARCH>::CSR, this->gen_const(16U, csr), 32/8);
  3975. Value* xrs1_val = this->gen_reg_load(rs1 + traits<ARCH>::X0, 0);
  3976. if(rd != 0){
  3977. Value* Xtmp0_val = xrd_val;
  3978. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  3979. }
  3980. if(rs1 != 0){
  3981. Value* CSRtmp1_val = this->builder.CreateOr(
  3982. xrd_val,
  3983. xrs1_val);
  3984. this->gen_write_mem(
  3985. traits<ARCH>::CSR,
  3986. this->gen_const(16U, csr),
  3987. this->builder.CreateZExtOrTrunc(CSRtmp1_val,this->get_type(32)));
  3988. }
  3989. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  3990. this->gen_sync(POST_SYNC, 94);
  3991. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  3992. this->gen_trap_check(bb);
  3993. return std::make_tuple(CONT, bb);
  3994. }
  3995. /* instruction 95: CSRRC */
  3996. std::tuple<continuation_e, BasicBlock*> __csrrc(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  3997. bb->setName("CSRRC");
  3998. this->gen_sync(PRE_SYNC, 95);
  3999. uint8_t rd = ((bit_sub<7,5>(instr)));
  4000. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  4001. uint16_t csr = ((bit_sub<20,12>(instr)));
  4002. if(this->disass_enabled){
  4003. /* generate console output when executing the command */
  4004. auto mnemonic = fmt::format(
  4005. "{mnemonic:10} {rd}, {csr}, {rs1}", fmt::arg("mnemonic", "csrrc"),
  4006. fmt::arg("rd", name(rd)), fmt::arg("csr", csr), fmt::arg("rs1", name(rs1)));
  4007. std::vector<Value*> args {
  4008. this->core_ptr,
  4009. this->gen_const(64, pc.val),
  4010. this->builder.CreateGlobalStringPtr(mnemonic),
  4011. };
  4012. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  4013. }
  4014. Value* cur_pc_val = this->gen_const(32, pc.val);
  4015. pc=pc+4;
  4016. Value* xrd_val = this->gen_read_mem(traits<ARCH>::CSR, this->gen_const(16U, csr), 32/8);
  4017. Value* xrs1_val = this->gen_reg_load(rs1 + traits<ARCH>::X0, 0);
  4018. if(rd != 0){
  4019. Value* Xtmp0_val = xrd_val;
  4020. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  4021. }
  4022. if(rs1 != 0){
  4023. Value* CSRtmp1_val = this->builder.CreateAnd(
  4024. xrd_val,
  4025. this->builder.CreateNot(xrs1_val));
  4026. this->gen_write_mem(
  4027. traits<ARCH>::CSR,
  4028. this->gen_const(16U, csr),
  4029. this->builder.CreateZExtOrTrunc(CSRtmp1_val,this->get_type(32)));
  4030. }
  4031. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  4032. this->gen_sync(POST_SYNC, 95);
  4033. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  4034. this->gen_trap_check(bb);
  4035. return std::make_tuple(CONT, bb);
  4036. }
  4037. /* instruction 96: CSRRWI */
  4038. std::tuple<continuation_e, BasicBlock*> __csrrwi(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  4039. bb->setName("CSRRWI");
  4040. this->gen_sync(PRE_SYNC, 96);
  4041. uint8_t rd = ((bit_sub<7,5>(instr)));
  4042. uint8_t zimm = ((bit_sub<15,5>(instr)));
  4043. uint16_t csr = ((bit_sub<20,12>(instr)));
  4044. if(this->disass_enabled){
  4045. /* generate console output when executing the command */
  4046. auto mnemonic = fmt::format(
  4047. "{mnemonic:10} {rd}, {csr}, {zimm:#0x}", fmt::arg("mnemonic", "csrrwi"),
  4048. fmt::arg("rd", name(rd)), fmt::arg("csr", csr), fmt::arg("zimm", zimm));
  4049. std::vector<Value*> args {
  4050. this->core_ptr,
  4051. this->gen_const(64, pc.val),
  4052. this->builder.CreateGlobalStringPtr(mnemonic),
  4053. };
  4054. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  4055. }
  4056. Value* cur_pc_val = this->gen_const(32, pc.val);
  4057. pc=pc+4;
  4058. if(rd != 0){
  4059. Value* Xtmp0_val = this->gen_read_mem(traits<ARCH>::CSR, this->gen_const(16U, csr), 32/8);
  4060. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  4061. }
  4062. Value* CSRtmp1_val = this->gen_ext(
  4063. this->gen_const(32U, zimm),
  4064. 32,
  4065. false);
  4066. this->gen_write_mem(
  4067. traits<ARCH>::CSR,
  4068. this->gen_const(16U, csr),
  4069. this->builder.CreateZExtOrTrunc(CSRtmp1_val,this->get_type(32)));
  4070. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  4071. this->gen_sync(POST_SYNC, 96);
  4072. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  4073. this->gen_trap_check(bb);
  4074. return std::make_tuple(CONT, bb);
  4075. }
  4076. /* instruction 97: CSRRSI */
  4077. std::tuple<continuation_e, BasicBlock*> __csrrsi(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  4078. bb->setName("CSRRSI");
  4079. this->gen_sync(PRE_SYNC, 97);
  4080. uint8_t rd = ((bit_sub<7,5>(instr)));
  4081. uint8_t zimm = ((bit_sub<15,5>(instr)));
  4082. uint16_t csr = ((bit_sub<20,12>(instr)));
  4083. if(this->disass_enabled){
  4084. /* generate console output when executing the command */
  4085. auto mnemonic = fmt::format(
  4086. "{mnemonic:10} {rd}, {csr}, {zimm:#0x}", fmt::arg("mnemonic", "csrrsi"),
  4087. fmt::arg("rd", name(rd)), fmt::arg("csr", csr), fmt::arg("zimm", zimm));
  4088. std::vector<Value*> args {
  4089. this->core_ptr,
  4090. this->gen_const(64, pc.val),
  4091. this->builder.CreateGlobalStringPtr(mnemonic),
  4092. };
  4093. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  4094. }
  4095. Value* cur_pc_val = this->gen_const(32, pc.val);
  4096. pc=pc+4;
  4097. Value* res_val = this->gen_read_mem(traits<ARCH>::CSR, this->gen_const(16U, csr), 32/8);
  4098. if(zimm != 0){
  4099. Value* CSRtmp0_val = this->builder.CreateOr(
  4100. res_val,
  4101. this->gen_ext(
  4102. this->gen_const(32U, zimm),
  4103. 32,
  4104. false));
  4105. this->gen_write_mem(
  4106. traits<ARCH>::CSR,
  4107. this->gen_const(16U, csr),
  4108. this->builder.CreateZExtOrTrunc(CSRtmp0_val,this->get_type(32)));
  4109. }
  4110. if(rd != 0){
  4111. Value* Xtmp1_val = res_val;
  4112. this->builder.CreateStore(Xtmp1_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  4113. }
  4114. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  4115. this->gen_sync(POST_SYNC, 97);
  4116. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  4117. this->gen_trap_check(bb);
  4118. return std::make_tuple(CONT, bb);
  4119. }
  4120. /* instruction 98: CSRRCI */
  4121. std::tuple<continuation_e, BasicBlock*> __csrrci(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  4122. bb->setName("CSRRCI");
  4123. this->gen_sync(PRE_SYNC, 98);
  4124. uint8_t rd = ((bit_sub<7,5>(instr)));
  4125. uint8_t zimm = ((bit_sub<15,5>(instr)));
  4126. uint16_t csr = ((bit_sub<20,12>(instr)));
  4127. if(this->disass_enabled){
  4128. /* generate console output when executing the command */
  4129. auto mnemonic = fmt::format(
  4130. "{mnemonic:10} {rd}, {csr}, {zimm:#0x}", fmt::arg("mnemonic", "csrrci"),
  4131. fmt::arg("rd", name(rd)), fmt::arg("csr", csr), fmt::arg("zimm", zimm));
  4132. std::vector<Value*> args {
  4133. this->core_ptr,
  4134. this->gen_const(64, pc.val),
  4135. this->builder.CreateGlobalStringPtr(mnemonic),
  4136. };
  4137. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  4138. }
  4139. Value* cur_pc_val = this->gen_const(32, pc.val);
  4140. pc=pc+4;
  4141. Value* res_val = this->gen_read_mem(traits<ARCH>::CSR, this->gen_const(16U, csr), 32/8);
  4142. if(rd != 0){
  4143. Value* Xtmp0_val = res_val;
  4144. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  4145. }
  4146. if(zimm != 0){
  4147. Value* CSRtmp1_val = this->builder.CreateAnd(
  4148. res_val,
  4149. this->builder.CreateNot(this->gen_ext(
  4150. this->gen_const(32U, zimm),
  4151. 32,
  4152. false)));
  4153. this->gen_write_mem(
  4154. traits<ARCH>::CSR,
  4155. this->gen_const(16U, csr),
  4156. this->builder.CreateZExtOrTrunc(CSRtmp1_val,this->get_type(32)));
  4157. }
  4158. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  4159. this->gen_sync(POST_SYNC, 98);
  4160. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  4161. this->gen_trap_check(bb);
  4162. return std::make_tuple(CONT, bb);
  4163. }
  4164. /****************************************************************************
  4165. * end opcode definitions
  4166. ****************************************************************************/
  4167. std::tuple<continuation_e, BasicBlock *> illegal_intruction(virt_addr_t &pc, code_word_t instr, BasicBlock *bb) {
  4168. this->gen_sync(iss::PRE_SYNC, instr_descr.size());
  4169. this->builder.CreateStore(this->builder.CreateLoad(get_reg_ptr(traits<ARCH>::NEXT_PC), true),
  4170. get_reg_ptr(traits<ARCH>::PC), true);
  4171. this->builder.CreateStore(
  4172. this->builder.CreateAdd(this->builder.CreateLoad(get_reg_ptr(traits<ARCH>::ICOUNT), true),
  4173. this->gen_const(64U, 1)),
  4174. get_reg_ptr(traits<ARCH>::ICOUNT), true);
  4175. pc = pc + ((instr & 3) == 3 ? 4 : 2);
  4176. this->gen_raise_trap(0, 2); // illegal instruction trap
  4177. this->gen_sync(iss::POST_SYNC, instr_descr.size());
  4178. this->gen_trap_check(this->leave_blk);
  4179. return std::make_tuple(BRANCH, nullptr);
  4180. }
  4181. };
  4182. template <typename CODE_WORD> void debug_fn(CODE_WORD insn) {
  4183. volatile CODE_WORD x = insn;
  4184. insn = 2 * x;
  4185. }
  4186. template <typename ARCH> vm_impl<ARCH>::vm_impl() { this(new ARCH()); }
  4187. template <typename ARCH>
  4188. vm_impl<ARCH>::vm_impl(ARCH &core, unsigned core_id, unsigned cluster_id)
  4189. : vm_base<ARCH>(core, core_id, cluster_id) {
  4190. qlut[0] = lut_00.data();
  4191. qlut[1] = lut_01.data();
  4192. qlut[2] = lut_10.data();
  4193. qlut[3] = lut_11.data();
  4194. for (auto instr : instr_descr) {
  4195. auto quantrant = instr.value & 0x3;
  4196. expand_bit_mask(29, lutmasks[quantrant], instr.value >> 2, instr.mask >> 2, 0, qlut[quantrant], instr.op);
  4197. }
  4198. }
  4199. template <typename ARCH>
  4200. std::tuple<continuation_e, BasicBlock *>
  4201. vm_impl<ARCH>::gen_single_inst_behavior(virt_addr_t &pc, unsigned int &inst_cnt, BasicBlock *this_block) {
  4202. // we fetch at max 4 byte, alignment is 2
  4203. enum {TRAP_ID=1<<16};
  4204. code_word_t insn = 0;
  4205. const typename traits<ARCH>::addr_t upper_bits = ~traits<ARCH>::PGMASK;
  4206. phys_addr_t paddr(pc);
  4207. auto *const data = (uint8_t *)&insn;
  4208. paddr = this->core.v2p(pc);
  4209. if ((pc.val & upper_bits) != ((pc.val + 2) & upper_bits)) { // we may cross a page boundary
  4210. auto res = this->core.read(paddr, 2, data);
  4211. if (res != iss::Ok) throw trap_access(TRAP_ID, pc.val);
  4212. if ((insn & 0x3) == 0x3) { // this is a 32bit instruction
  4213. res = this->core.read(this->core.v2p(pc + 2), 2, data + 2);
  4214. }
  4215. } else {
  4216. auto res = this->core.read(paddr, 4, data);
  4217. if (res != iss::Ok) throw trap_access(TRAP_ID, pc.val);
  4218. }
  4219. if (insn == 0x0000006f || (insn&0xffff)==0xa001) throw simulation_stopped(0); // 'J 0' or 'C.J 0'
  4220. // curr pc on stack
  4221. ++inst_cnt;
  4222. auto lut_val = extract_fields(insn);
  4223. auto f = qlut[insn & 0x3][lut_val];
  4224. if (f == nullptr) {
  4225. f = &this_class::illegal_intruction;
  4226. }
  4227. return (this->*f)(pc, insn, this_block);
  4228. }
  4229. template <typename ARCH> void vm_impl<ARCH>::gen_leave_behavior(BasicBlock *leave_blk) {
  4230. this->builder.SetInsertPoint(leave_blk);
  4231. this->builder.CreateRet(this->builder.CreateLoad(get_reg_ptr(arch::traits<ARCH>::NEXT_PC), false));
  4232. }
  4233. template <typename ARCH> void vm_impl<ARCH>::gen_raise_trap(uint16_t trap_id, uint16_t cause) {
  4234. auto *TRAP_val = this->gen_const(32, 0x80 << 24 | (cause << 16) | trap_id);
  4235. this->builder.CreateStore(TRAP_val, get_reg_ptr(traits<ARCH>::TRAP_STATE), true);
  4236. this->builder.CreateStore(this->gen_const(32U, std::numeric_limits<uint32_t>::max()), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  4237. }
  4238. template <typename ARCH> void vm_impl<ARCH>::gen_leave_trap(unsigned lvl) {
  4239. std::vector<Value *> args{ this->core_ptr, ConstantInt::get(getContext(), APInt(64, lvl)) };
  4240. this->builder.CreateCall(this->mod->getFunction("leave_trap"), args);
  4241. auto *PC_val = this->gen_read_mem(traits<ARCH>::CSR, (lvl << 8) + 0x41, traits<ARCH>::XLEN / 8);
  4242. this->builder.CreateStore(PC_val, get_reg_ptr(traits<ARCH>::NEXT_PC), false);
  4243. this->builder.CreateStore(this->gen_const(32U, std::numeric_limits<uint32_t>::max()), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  4244. }
  4245. template <typename ARCH> void vm_impl<ARCH>::gen_wait(unsigned type) {
  4246. std::vector<Value *> args{ this->core_ptr, ConstantInt::get(getContext(), APInt(64, type)) };
  4247. this->builder.CreateCall(this->mod->getFunction("wait"), args);
  4248. }
  4249. template <typename ARCH> void vm_impl<ARCH>::gen_trap_behavior(BasicBlock *trap_blk) {
  4250. this->builder.SetInsertPoint(trap_blk);
  4251. auto *trap_state_val = this->builder.CreateLoad(get_reg_ptr(traits<ARCH>::TRAP_STATE), true);
  4252. this->builder.CreateStore(this->gen_const(32U, std::numeric_limits<uint32_t>::max()),
  4253. get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  4254. std::vector<Value *> args{this->core_ptr, this->adj_to64(trap_state_val),
  4255. this->adj_to64(this->builder.CreateLoad(get_reg_ptr(traits<ARCH>::PC), false))};
  4256. this->builder.CreateCall(this->mod->getFunction("enter_trap"), args);
  4257. auto *trap_addr_val = this->builder.CreateLoad(get_reg_ptr(traits<ARCH>::NEXT_PC), false);
  4258. this->builder.CreateRet(trap_addr_val);
  4259. }
  4260. template <typename ARCH> inline void vm_impl<ARCH>::gen_trap_check(BasicBlock *bb) {
  4261. auto *v = this->builder.CreateLoad(get_reg_ptr(arch::traits<ARCH>::TRAP_STATE), true);
  4262. this->gen_cond_branch(this->builder.CreateICmp(
  4263. ICmpInst::ICMP_EQ, v,
  4264. ConstantInt::get(getContext(), APInt(v->getType()->getIntegerBitWidth(), 0))),
  4265. bb, this->trap_blk, 1);
  4266. }
  4267. } // namespace rv32imac
  4268. template <>
  4269. std::unique_ptr<vm_if> create<arch::rv32imac>(arch::rv32imac *core, unsigned short port, bool dump) {
  4270. auto ret = new rv32imac::vm_impl<arch::rv32imac>(*core, dump);
  4271. if (port != 0) debugger::server<debugger::gdb_session>::run_server(ret, port);
  4272. return std::unique_ptr<vm_if>(ret);
  4273. }
  4274. } // namespace iss