vm_rv64i.cpp 129KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152
  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/rv64i.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 rv64i {
  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, 64> instr_descr = {{
  157. /* entries are: size, valid value, valid mask, function ptr */
  158. /* instruction LUI */
  159. {32, 0b00000000000000000000000000110111, 0b00000000000000000000000001111111, &this_class::__lui},
  160. /* instruction AUIPC */
  161. {32, 0b00000000000000000000000000010111, 0b00000000000000000000000001111111, &this_class::__auipc},
  162. /* instruction JAL */
  163. {32, 0b00000000000000000000000001101111, 0b00000000000000000000000001111111, &this_class::__jal},
  164. /* instruction JALR */
  165. {32, 0b00000000000000000000000001100111, 0b00000000000000000111000001111111, &this_class::__jalr},
  166. /* instruction BEQ */
  167. {32, 0b00000000000000000000000001100011, 0b00000000000000000111000001111111, &this_class::__beq},
  168. /* instruction BNE */
  169. {32, 0b00000000000000000001000001100011, 0b00000000000000000111000001111111, &this_class::__bne},
  170. /* instruction BLT */
  171. {32, 0b00000000000000000100000001100011, 0b00000000000000000111000001111111, &this_class::__blt},
  172. /* instruction BGE */
  173. {32, 0b00000000000000000101000001100011, 0b00000000000000000111000001111111, &this_class::__bge},
  174. /* instruction BLTU */
  175. {32, 0b00000000000000000110000001100011, 0b00000000000000000111000001111111, &this_class::__bltu},
  176. /* instruction BGEU */
  177. {32, 0b00000000000000000111000001100011, 0b00000000000000000111000001111111, &this_class::__bgeu},
  178. /* instruction LB */
  179. {32, 0b00000000000000000000000000000011, 0b00000000000000000111000001111111, &this_class::__lb},
  180. /* instruction LH */
  181. {32, 0b00000000000000000001000000000011, 0b00000000000000000111000001111111, &this_class::__lh},
  182. /* instruction LW */
  183. {32, 0b00000000000000000010000000000011, 0b00000000000000000111000001111111, &this_class::__lw},
  184. /* instruction LBU */
  185. {32, 0b00000000000000000100000000000011, 0b00000000000000000111000001111111, &this_class::__lbu},
  186. /* instruction LHU */
  187. {32, 0b00000000000000000101000000000011, 0b00000000000000000111000001111111, &this_class::__lhu},
  188. /* instruction SB */
  189. {32, 0b00000000000000000000000000100011, 0b00000000000000000111000001111111, &this_class::__sb},
  190. /* instruction SH */
  191. {32, 0b00000000000000000001000000100011, 0b00000000000000000111000001111111, &this_class::__sh},
  192. /* instruction SW */
  193. {32, 0b00000000000000000010000000100011, 0b00000000000000000111000001111111, &this_class::__sw},
  194. /* instruction ADDI */
  195. {32, 0b00000000000000000000000000010011, 0b00000000000000000111000001111111, &this_class::__addi},
  196. /* instruction SLTI */
  197. {32, 0b00000000000000000010000000010011, 0b00000000000000000111000001111111, &this_class::__slti},
  198. /* instruction SLTIU */
  199. {32, 0b00000000000000000011000000010011, 0b00000000000000000111000001111111, &this_class::__sltiu},
  200. /* instruction XORI */
  201. {32, 0b00000000000000000100000000010011, 0b00000000000000000111000001111111, &this_class::__xori},
  202. /* instruction ORI */
  203. {32, 0b00000000000000000110000000010011, 0b00000000000000000111000001111111, &this_class::__ori},
  204. /* instruction ANDI */
  205. {32, 0b00000000000000000111000000010011, 0b00000000000000000111000001111111, &this_class::__andi},
  206. /* instruction SLLI */
  207. {32, 0b00000000000000000001000000010011, 0b11111100000000000111000001111111, &this_class::__slli},
  208. /* instruction SRLI */
  209. {32, 0b00000000000000000101000000010011, 0b11111100000000000111000001111111, &this_class::__srli},
  210. /* instruction SRAI */
  211. {32, 0b01000000000000000101000000010011, 0b11111100000000000111000001111111, &this_class::__srai},
  212. /* instruction ADD */
  213. {32, 0b00000000000000000000000000110011, 0b11111110000000000111000001111111, &this_class::__add},
  214. /* instruction SUB */
  215. {32, 0b01000000000000000000000000110011, 0b11111110000000000111000001111111, &this_class::__sub},
  216. /* instruction SLL */
  217. {32, 0b00000000000000000001000000110011, 0b11111110000000000111000001111111, &this_class::__sll},
  218. /* instruction SLT */
  219. {32, 0b00000000000000000010000000110011, 0b11111110000000000111000001111111, &this_class::__slt},
  220. /* instruction SLTU */
  221. {32, 0b00000000000000000011000000110011, 0b11111110000000000111000001111111, &this_class::__sltu},
  222. /* instruction XOR */
  223. {32, 0b00000000000000000100000000110011, 0b11111110000000000111000001111111, &this_class::__xor},
  224. /* instruction SRL */
  225. {32, 0b00000000000000000101000000110011, 0b11111110000000000111000001111111, &this_class::__srl},
  226. /* instruction SRA */
  227. {32, 0b01000000000000000101000000110011, 0b11111110000000000111000001111111, &this_class::__sra},
  228. /* instruction OR */
  229. {32, 0b00000000000000000110000000110011, 0b11111110000000000111000001111111, &this_class::__or},
  230. /* instruction AND */
  231. {32, 0b00000000000000000111000000110011, 0b11111110000000000111000001111111, &this_class::__and},
  232. /* instruction FENCE */
  233. {32, 0b00000000000000000000000000001111, 0b11110000000000000111000001111111, &this_class::__fence},
  234. /* instruction FENCE_I */
  235. {32, 0b00000000000000000001000000001111, 0b00000000000000000111000001111111, &this_class::__fence_i},
  236. /* instruction ECALL */
  237. {32, 0b00000000000000000000000001110011, 0b11111111111111111111111111111111, &this_class::__ecall},
  238. /* instruction EBREAK */
  239. {32, 0b00000000000100000000000001110011, 0b11111111111111111111111111111111, &this_class::__ebreak},
  240. /* instruction URET */
  241. {32, 0b00000000001000000000000001110011, 0b11111111111111111111111111111111, &this_class::__uret},
  242. /* instruction SRET */
  243. {32, 0b00010000001000000000000001110011, 0b11111111111111111111111111111111, &this_class::__sret},
  244. /* instruction MRET */
  245. {32, 0b00110000001000000000000001110011, 0b11111111111111111111111111111111, &this_class::__mret},
  246. /* instruction WFI */
  247. {32, 0b00010000010100000000000001110011, 0b11111111111111111111111111111111, &this_class::__wfi},
  248. /* instruction SFENCE.VMA */
  249. {32, 0b00010010000000000000000001110011, 0b11111110000000000111111111111111, &this_class::__sfence_vma},
  250. /* instruction CSRRW */
  251. {32, 0b00000000000000000001000001110011, 0b00000000000000000111000001111111, &this_class::__csrrw},
  252. /* instruction CSRRS */
  253. {32, 0b00000000000000000010000001110011, 0b00000000000000000111000001111111, &this_class::__csrrs},
  254. /* instruction CSRRC */
  255. {32, 0b00000000000000000011000001110011, 0b00000000000000000111000001111111, &this_class::__csrrc},
  256. /* instruction CSRRWI */
  257. {32, 0b00000000000000000101000001110011, 0b00000000000000000111000001111111, &this_class::__csrrwi},
  258. /* instruction CSRRSI */
  259. {32, 0b00000000000000000110000001110011, 0b00000000000000000111000001111111, &this_class::__csrrsi},
  260. /* instruction CSRRCI */
  261. {32, 0b00000000000000000111000001110011, 0b00000000000000000111000001111111, &this_class::__csrrci},
  262. /* instruction LWU */
  263. {32, 0b00000000000000000110000000000011, 0b00000000000000000111000001111111, &this_class::__lwu},
  264. /* instruction LD */
  265. {32, 0b00000000000000000011000000000011, 0b00000000000000000111000001111111, &this_class::__ld},
  266. /* instruction SD */
  267. {32, 0b00000000000000000011000000100011, 0b00000000000000000111000001111111, &this_class::__sd},
  268. /* instruction ADDIW */
  269. {32, 0b00000000000000000000000000011011, 0b00000000000000000111000001111111, &this_class::__addiw},
  270. /* instruction SLLIW */
  271. {32, 0b00000000000000000001000000011011, 0b11111110000000000111000001111111, &this_class::__slliw},
  272. /* instruction SRLIW */
  273. {32, 0b00000000000000000101000000011011, 0b11111110000000000111000001111111, &this_class::__srliw},
  274. /* instruction SRAIW */
  275. {32, 0b01000000000000000101000000011011, 0b11111110000000000111000001111111, &this_class::__sraiw},
  276. /* instruction ADDW */
  277. {32, 0b00000000000000000000000000111011, 0b11111110000000000111000001111111, &this_class::__addw},
  278. /* instruction SUBW */
  279. {32, 0b01000000000000000000000000111011, 0b11111110000000000111000001111111, &this_class::__subw},
  280. /* instruction SLLW */
  281. {32, 0b00000000000000000001000000111011, 0b11111110000000000111000001111111, &this_class::__sllw},
  282. /* instruction SRLW */
  283. {32, 0b00000000000000000101000000111011, 0b11111110000000000111000001111111, &this_class::__srlw},
  284. /* instruction SRAW */
  285. {32, 0b01000000000000000101000000111011, 0b11111110000000000111000001111111, &this_class::__sraw},
  286. }};
  287. /* instruction definitions */
  288. /* instruction 0: LUI */
  289. std::tuple<continuation_e, BasicBlock*> __lui(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  290. bb->setName("LUI");
  291. this->gen_sync(PRE_SYNC, 0);
  292. uint8_t rd = ((bit_sub<7,5>(instr)));
  293. int32_t imm = signextend<int32_t,32>((bit_sub<12,20>(instr) << 12));
  294. if(this->disass_enabled){
  295. /* generate console output when executing the command */
  296. auto mnemonic = fmt::format(
  297. "{mnemonic:10} {rd}, {imm:#05x}", fmt::arg("mnemonic", "lui"),
  298. fmt::arg("rd", name(rd)), fmt::arg("imm", imm));
  299. std::vector<Value*> args {
  300. this->core_ptr,
  301. this->gen_const(64, pc.val),
  302. this->builder.CreateGlobalStringPtr(mnemonic),
  303. };
  304. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  305. }
  306. Value* cur_pc_val = this->gen_const(64, pc.val);
  307. pc=pc+4;
  308. if(rd != 0){
  309. Value* Xtmp0_val = this->gen_const(64U, imm);
  310. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  311. }
  312. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  313. this->gen_sync(POST_SYNC, 0);
  314. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  315. this->gen_trap_check(bb);
  316. return std::make_tuple(CONT, bb);
  317. }
  318. /* instruction 1: AUIPC */
  319. std::tuple<continuation_e, BasicBlock*> __auipc(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  320. bb->setName("AUIPC");
  321. this->gen_sync(PRE_SYNC, 1);
  322. uint8_t rd = ((bit_sub<7,5>(instr)));
  323. int32_t imm = signextend<int32_t,32>((bit_sub<12,20>(instr) << 12));
  324. if(this->disass_enabled){
  325. /* generate console output when executing the command */
  326. auto mnemonic = fmt::format(
  327. "{mnemonic:10} {rd}, {imm:#08x}", fmt::arg("mnemonic", "auipc"),
  328. fmt::arg("rd", name(rd)), fmt::arg("imm", imm));
  329. std::vector<Value*> args {
  330. this->core_ptr,
  331. this->gen_const(64, pc.val),
  332. this->builder.CreateGlobalStringPtr(mnemonic),
  333. };
  334. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  335. }
  336. Value* cur_pc_val = this->gen_const(64, pc.val);
  337. pc=pc+4;
  338. if(rd != 0){
  339. Value* Xtmp0_val = this->builder.CreateAdd(
  340. this->gen_ext(
  341. cur_pc_val,
  342. 64, true),
  343. this->gen_const(64U, imm));
  344. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  345. }
  346. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  347. this->gen_sync(POST_SYNC, 1);
  348. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  349. this->gen_trap_check(bb);
  350. return std::make_tuple(CONT, bb);
  351. }
  352. /* instruction 2: JAL */
  353. std::tuple<continuation_e, BasicBlock*> __jal(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  354. bb->setName("JAL");
  355. this->gen_sync(PRE_SYNC, 2);
  356. uint8_t rd = ((bit_sub<7,5>(instr)));
  357. 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));
  358. if(this->disass_enabled){
  359. /* generate console output when executing the command */
  360. auto mnemonic = fmt::format(
  361. "{mnemonic:10} {rd}, {imm:#0x}", fmt::arg("mnemonic", "jal"),
  362. fmt::arg("rd", name(rd)), fmt::arg("imm", imm));
  363. std::vector<Value*> args {
  364. this->core_ptr,
  365. this->gen_const(64, pc.val),
  366. this->builder.CreateGlobalStringPtr(mnemonic),
  367. };
  368. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  369. }
  370. Value* cur_pc_val = this->gen_const(64, pc.val);
  371. pc=pc+4;
  372. if(rd != 0){
  373. Value* Xtmp0_val = this->builder.CreateAdd(
  374. cur_pc_val,
  375. this->gen_const(64U, 4));
  376. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  377. }
  378. Value* PC_val = this->builder.CreateAdd(
  379. this->gen_ext(
  380. cur_pc_val,
  381. 64, true),
  382. this->gen_const(64U, imm));
  383. this->builder.CreateStore(PC_val, get_reg_ptr(traits<ARCH>::NEXT_PC), false);
  384. Value* is_cont_v = this->builder.CreateICmp(ICmpInst::ICMP_NE, PC_val, this->gen_const(64U, pc.val), "is_cont_v");
  385. this->builder.CreateStore(this->gen_ext(is_cont_v, 32U, false), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  386. this->gen_sync(POST_SYNC, 2);
  387. this->gen_trap_check(this->leave_blk);
  388. return std::make_tuple(BRANCH, nullptr);
  389. }
  390. /* instruction 3: JALR */
  391. std::tuple<continuation_e, BasicBlock*> __jalr(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  392. bb->setName("JALR");
  393. this->gen_sync(PRE_SYNC, 3);
  394. uint8_t rd = ((bit_sub<7,5>(instr)));
  395. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  396. int16_t imm = signextend<int16_t,12>((bit_sub<20,12>(instr)));
  397. if(this->disass_enabled){
  398. /* generate console output when executing the command */
  399. auto mnemonic = fmt::format(
  400. "{mnemonic:10} {rd}, {rs1}, {imm:#0x}", fmt::arg("mnemonic", "jalr"),
  401. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("imm", imm));
  402. std::vector<Value*> args {
  403. this->core_ptr,
  404. this->gen_const(64, pc.val),
  405. this->builder.CreateGlobalStringPtr(mnemonic),
  406. };
  407. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  408. }
  409. Value* cur_pc_val = this->gen_const(64, pc.val);
  410. pc=pc+4;
  411. Value* new_pc_val = this->builder.CreateAdd(
  412. this->gen_ext(
  413. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  414. 64, true),
  415. this->gen_const(64U, imm));
  416. Value* align_val = this->builder.CreateAnd(
  417. new_pc_val,
  418. this->gen_const(64U, 0x2));
  419. {
  420. BasicBlock* bbnext = BasicBlock::Create(this->mod->getContext(), "endif", this->func, this->leave_blk);
  421. BasicBlock* bb_then = BasicBlock::Create(this->mod->getContext(), "thenbr", this->func, bbnext);
  422. BasicBlock* bb_else = BasicBlock::Create(this->mod->getContext(), "elsebr", this->func, bbnext);
  423. // this->builder.SetInsertPoint(bb);
  424. this->gen_cond_branch(this->builder.CreateICmp(
  425. ICmpInst::ICMP_NE,
  426. align_val,
  427. this->gen_const(64U, 0)),
  428. bb_then,
  429. bb_else);
  430. this->builder.SetInsertPoint(bb_then);
  431. {
  432. this->gen_raise_trap(0, 0);
  433. }
  434. this->builder.CreateBr(bbnext);
  435. this->builder.SetInsertPoint(bb_else);
  436. {
  437. if(rd != 0){
  438. Value* Xtmp0_val = this->builder.CreateAdd(
  439. cur_pc_val,
  440. this->gen_const(64U, 4));
  441. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  442. }
  443. Value* PC_val = this->builder.CreateAnd(
  444. new_pc_val,
  445. this->builder.CreateNot(this->gen_const(64U, 0x1)));
  446. this->builder.CreateStore(PC_val, get_reg_ptr(traits<ARCH>::NEXT_PC), false);
  447. this->builder.CreateStore(this->gen_const(32U, std::numeric_limits<uint32_t>::max()), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  448. }
  449. this->builder.CreateBr(bbnext);
  450. bb=bbnext;
  451. }
  452. this->builder.SetInsertPoint(bb);
  453. this->gen_sync(POST_SYNC, 3);
  454. this->gen_trap_check(this->leave_blk);
  455. return std::make_tuple(BRANCH, nullptr);
  456. }
  457. /* instruction 4: BEQ */
  458. std::tuple<continuation_e, BasicBlock*> __beq(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  459. bb->setName("BEQ");
  460. this->gen_sync(PRE_SYNC, 4);
  461. 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));
  462. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  463. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  464. if(this->disass_enabled){
  465. /* generate console output when executing the command */
  466. auto mnemonic = fmt::format(
  467. "{mnemonic:10} {rs1}, {rs2}, {imm:#0x}", fmt::arg("mnemonic", "beq"),
  468. fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)), fmt::arg("imm", imm));
  469. std::vector<Value*> args {
  470. this->core_ptr,
  471. this->gen_const(64, pc.val),
  472. this->builder.CreateGlobalStringPtr(mnemonic),
  473. };
  474. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  475. }
  476. Value* cur_pc_val = this->gen_const(64, pc.val);
  477. pc=pc+4;
  478. Value* PC_val = this->gen_choose(
  479. this->builder.CreateICmp(
  480. ICmpInst::ICMP_EQ,
  481. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  482. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0)),
  483. this->builder.CreateAdd(
  484. this->gen_ext(
  485. cur_pc_val,
  486. 64, true),
  487. this->gen_const(64U, imm)),
  488. this->builder.CreateAdd(
  489. cur_pc_val,
  490. this->gen_const(64U, 4)),
  491. 64);
  492. this->builder.CreateStore(PC_val, get_reg_ptr(traits<ARCH>::NEXT_PC), false);
  493. Value* is_cont_v = this->builder.CreateICmp(ICmpInst::ICMP_NE, PC_val, this->gen_const(64U, pc.val), "is_cont_v");
  494. this->builder.CreateStore(this->gen_ext(is_cont_v, 32U, false), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  495. this->gen_sync(POST_SYNC, 4);
  496. this->gen_trap_check(this->leave_blk);
  497. return std::make_tuple(BRANCH, nullptr);
  498. }
  499. /* instruction 5: BNE */
  500. std::tuple<continuation_e, BasicBlock*> __bne(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  501. bb->setName("BNE");
  502. this->gen_sync(PRE_SYNC, 5);
  503. 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));
  504. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  505. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  506. if(this->disass_enabled){
  507. /* generate console output when executing the command */
  508. auto mnemonic = fmt::format(
  509. "{mnemonic:10} {rs1}, {rs2}, {imm:#0x}", fmt::arg("mnemonic", "bne"),
  510. fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)), fmt::arg("imm", imm));
  511. std::vector<Value*> args {
  512. this->core_ptr,
  513. this->gen_const(64, pc.val),
  514. this->builder.CreateGlobalStringPtr(mnemonic),
  515. };
  516. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  517. }
  518. Value* cur_pc_val = this->gen_const(64, pc.val);
  519. pc=pc+4;
  520. Value* PC_val = this->gen_choose(
  521. this->builder.CreateICmp(
  522. ICmpInst::ICMP_NE,
  523. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  524. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0)),
  525. this->builder.CreateAdd(
  526. this->gen_ext(
  527. cur_pc_val,
  528. 64, true),
  529. this->gen_const(64U, imm)),
  530. this->builder.CreateAdd(
  531. cur_pc_val,
  532. this->gen_const(64U, 4)),
  533. 64);
  534. this->builder.CreateStore(PC_val, get_reg_ptr(traits<ARCH>::NEXT_PC), false);
  535. Value* is_cont_v = this->builder.CreateICmp(ICmpInst::ICMP_NE, PC_val, this->gen_const(64U, pc.val), "is_cont_v");
  536. this->builder.CreateStore(this->gen_ext(is_cont_v, 32U, false), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  537. this->gen_sync(POST_SYNC, 5);
  538. this->gen_trap_check(this->leave_blk);
  539. return std::make_tuple(BRANCH, nullptr);
  540. }
  541. /* instruction 6: BLT */
  542. std::tuple<continuation_e, BasicBlock*> __blt(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  543. bb->setName("BLT");
  544. this->gen_sync(PRE_SYNC, 6);
  545. 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));
  546. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  547. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  548. if(this->disass_enabled){
  549. /* generate console output when executing the command */
  550. auto mnemonic = fmt::format(
  551. "{mnemonic:10} {rs1}, {rs2}, {imm:#0x}", fmt::arg("mnemonic", "blt"),
  552. fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)), fmt::arg("imm", imm));
  553. std::vector<Value*> args {
  554. this->core_ptr,
  555. this->gen_const(64, pc.val),
  556. this->builder.CreateGlobalStringPtr(mnemonic),
  557. };
  558. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  559. }
  560. Value* cur_pc_val = this->gen_const(64, pc.val);
  561. pc=pc+4;
  562. Value* PC_val = this->gen_choose(
  563. this->builder.CreateICmp(
  564. ICmpInst::ICMP_SLT,
  565. this->gen_ext(
  566. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  567. 64, true),
  568. this->gen_ext(
  569. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  570. 64, true)),
  571. this->builder.CreateAdd(
  572. this->gen_ext(
  573. cur_pc_val,
  574. 64, true),
  575. this->gen_const(64U, imm)),
  576. this->builder.CreateAdd(
  577. cur_pc_val,
  578. this->gen_const(64U, 4)),
  579. 64);
  580. this->builder.CreateStore(PC_val, get_reg_ptr(traits<ARCH>::NEXT_PC), false);
  581. Value* is_cont_v = this->builder.CreateICmp(ICmpInst::ICMP_NE, PC_val, this->gen_const(64U, pc.val), "is_cont_v");
  582. this->builder.CreateStore(this->gen_ext(is_cont_v, 32U, false), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  583. this->gen_sync(POST_SYNC, 6);
  584. this->gen_trap_check(this->leave_blk);
  585. return std::make_tuple(BRANCH, nullptr);
  586. }
  587. /* instruction 7: BGE */
  588. std::tuple<continuation_e, BasicBlock*> __bge(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  589. bb->setName("BGE");
  590. this->gen_sync(PRE_SYNC, 7);
  591. 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));
  592. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  593. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  594. if(this->disass_enabled){
  595. /* generate console output when executing the command */
  596. auto mnemonic = fmt::format(
  597. "{mnemonic:10} {rs1}, {rs2}, {imm:#0x}", fmt::arg("mnemonic", "bge"),
  598. fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)), fmt::arg("imm", imm));
  599. std::vector<Value*> args {
  600. this->core_ptr,
  601. this->gen_const(64, pc.val),
  602. this->builder.CreateGlobalStringPtr(mnemonic),
  603. };
  604. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  605. }
  606. Value* cur_pc_val = this->gen_const(64, pc.val);
  607. pc=pc+4;
  608. Value* PC_val = this->gen_choose(
  609. this->builder.CreateICmp(
  610. ICmpInst::ICMP_SGE,
  611. this->gen_ext(
  612. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  613. 64, true),
  614. this->gen_ext(
  615. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  616. 64, true)),
  617. this->builder.CreateAdd(
  618. this->gen_ext(
  619. cur_pc_val,
  620. 64, true),
  621. this->gen_const(64U, imm)),
  622. this->builder.CreateAdd(
  623. cur_pc_val,
  624. this->gen_const(64U, 4)),
  625. 64);
  626. this->builder.CreateStore(PC_val, get_reg_ptr(traits<ARCH>::NEXT_PC), false);
  627. Value* is_cont_v = this->builder.CreateICmp(ICmpInst::ICMP_NE, PC_val, this->gen_const(64U, pc.val), "is_cont_v");
  628. this->builder.CreateStore(this->gen_ext(is_cont_v, 32U, false), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  629. this->gen_sync(POST_SYNC, 7);
  630. this->gen_trap_check(this->leave_blk);
  631. return std::make_tuple(BRANCH, nullptr);
  632. }
  633. /* instruction 8: BLTU */
  634. std::tuple<continuation_e, BasicBlock*> __bltu(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  635. bb->setName("BLTU");
  636. this->gen_sync(PRE_SYNC, 8);
  637. 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));
  638. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  639. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  640. if(this->disass_enabled){
  641. /* generate console output when executing the command */
  642. auto mnemonic = fmt::format(
  643. "{mnemonic:10} {rs1}, {rs2}, {imm:#0x}", fmt::arg("mnemonic", "bltu"),
  644. fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)), fmt::arg("imm", imm));
  645. std::vector<Value*> args {
  646. this->core_ptr,
  647. this->gen_const(64, pc.val),
  648. this->builder.CreateGlobalStringPtr(mnemonic),
  649. };
  650. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  651. }
  652. Value* cur_pc_val = this->gen_const(64, pc.val);
  653. pc=pc+4;
  654. Value* PC_val = this->gen_choose(
  655. this->builder.CreateICmp(
  656. ICmpInst::ICMP_ULT,
  657. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  658. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0)),
  659. this->builder.CreateAdd(
  660. this->gen_ext(
  661. cur_pc_val,
  662. 64, true),
  663. this->gen_const(64U, imm)),
  664. this->builder.CreateAdd(
  665. cur_pc_val,
  666. this->gen_const(64U, 4)),
  667. 64);
  668. this->builder.CreateStore(PC_val, get_reg_ptr(traits<ARCH>::NEXT_PC), false);
  669. Value* is_cont_v = this->builder.CreateICmp(ICmpInst::ICMP_NE, PC_val, this->gen_const(64U, pc.val), "is_cont_v");
  670. this->builder.CreateStore(this->gen_ext(is_cont_v, 32U, false), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  671. this->gen_sync(POST_SYNC, 8);
  672. this->gen_trap_check(this->leave_blk);
  673. return std::make_tuple(BRANCH, nullptr);
  674. }
  675. /* instruction 9: BGEU */
  676. std::tuple<continuation_e, BasicBlock*> __bgeu(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  677. bb->setName("BGEU");
  678. this->gen_sync(PRE_SYNC, 9);
  679. 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));
  680. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  681. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  682. if(this->disass_enabled){
  683. /* generate console output when executing the command */
  684. auto mnemonic = fmt::format(
  685. "{mnemonic:10} {rs1}, {rs2}, {imm:#0x}", fmt::arg("mnemonic", "bgeu"),
  686. fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)), fmt::arg("imm", imm));
  687. std::vector<Value*> args {
  688. this->core_ptr,
  689. this->gen_const(64, pc.val),
  690. this->builder.CreateGlobalStringPtr(mnemonic),
  691. };
  692. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  693. }
  694. Value* cur_pc_val = this->gen_const(64, pc.val);
  695. pc=pc+4;
  696. Value* PC_val = this->gen_choose(
  697. this->builder.CreateICmp(
  698. ICmpInst::ICMP_UGE,
  699. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  700. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0)),
  701. this->builder.CreateAdd(
  702. this->gen_ext(
  703. cur_pc_val,
  704. 64, true),
  705. this->gen_const(64U, imm)),
  706. this->builder.CreateAdd(
  707. cur_pc_val,
  708. this->gen_const(64U, 4)),
  709. 64);
  710. this->builder.CreateStore(PC_val, get_reg_ptr(traits<ARCH>::NEXT_PC), false);
  711. Value* is_cont_v = this->builder.CreateICmp(ICmpInst::ICMP_NE, PC_val, this->gen_const(64U, pc.val), "is_cont_v");
  712. this->builder.CreateStore(this->gen_ext(is_cont_v, 32U, false), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  713. this->gen_sync(POST_SYNC, 9);
  714. this->gen_trap_check(this->leave_blk);
  715. return std::make_tuple(BRANCH, nullptr);
  716. }
  717. /* instruction 10: LB */
  718. std::tuple<continuation_e, BasicBlock*> __lb(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  719. bb->setName("LB");
  720. this->gen_sync(PRE_SYNC, 10);
  721. uint8_t rd = ((bit_sub<7,5>(instr)));
  722. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  723. int16_t imm = signextend<int16_t,12>((bit_sub<20,12>(instr)));
  724. if(this->disass_enabled){
  725. /* generate console output when executing the command */
  726. auto mnemonic = fmt::format(
  727. "{mnemonic:10} {rd}, {imm}({rs1})", fmt::arg("mnemonic", "lb"),
  728. fmt::arg("rd", name(rd)), fmt::arg("imm", imm), fmt::arg("rs1", name(rs1)));
  729. std::vector<Value*> args {
  730. this->core_ptr,
  731. this->gen_const(64, pc.val),
  732. this->builder.CreateGlobalStringPtr(mnemonic),
  733. };
  734. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  735. }
  736. Value* cur_pc_val = this->gen_const(64, pc.val);
  737. pc=pc+4;
  738. Value* offs_val = this->builder.CreateAdd(
  739. this->gen_ext(
  740. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  741. 64, true),
  742. this->gen_const(64U, imm));
  743. if(rd != 0){
  744. Value* Xtmp0_val = this->gen_ext(
  745. this->gen_read_mem(traits<ARCH>::MEM, offs_val, 8/8),
  746. 64,
  747. true);
  748. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  749. }
  750. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  751. this->gen_sync(POST_SYNC, 10);
  752. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  753. this->gen_trap_check(bb);
  754. return std::make_tuple(CONT, bb);
  755. }
  756. /* instruction 11: LH */
  757. std::tuple<continuation_e, BasicBlock*> __lh(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  758. bb->setName("LH");
  759. this->gen_sync(PRE_SYNC, 11);
  760. uint8_t rd = ((bit_sub<7,5>(instr)));
  761. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  762. int16_t imm = signextend<int16_t,12>((bit_sub<20,12>(instr)));
  763. if(this->disass_enabled){
  764. /* generate console output when executing the command */
  765. auto mnemonic = fmt::format(
  766. "{mnemonic:10} {rd}, {imm}({rs1})", fmt::arg("mnemonic", "lh"),
  767. fmt::arg("rd", name(rd)), fmt::arg("imm", imm), fmt::arg("rs1", name(rs1)));
  768. std::vector<Value*> args {
  769. this->core_ptr,
  770. this->gen_const(64, pc.val),
  771. this->builder.CreateGlobalStringPtr(mnemonic),
  772. };
  773. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  774. }
  775. Value* cur_pc_val = this->gen_const(64, pc.val);
  776. pc=pc+4;
  777. Value* offs_val = this->builder.CreateAdd(
  778. this->gen_ext(
  779. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  780. 64, true),
  781. this->gen_const(64U, imm));
  782. if(rd != 0){
  783. Value* Xtmp0_val = this->gen_ext(
  784. this->gen_read_mem(traits<ARCH>::MEM, offs_val, 16/8),
  785. 64,
  786. true);
  787. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  788. }
  789. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  790. this->gen_sync(POST_SYNC, 11);
  791. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  792. this->gen_trap_check(bb);
  793. return std::make_tuple(CONT, bb);
  794. }
  795. /* instruction 12: LW */
  796. std::tuple<continuation_e, BasicBlock*> __lw(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  797. bb->setName("LW");
  798. this->gen_sync(PRE_SYNC, 12);
  799. uint8_t rd = ((bit_sub<7,5>(instr)));
  800. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  801. int16_t imm = signextend<int16_t,12>((bit_sub<20,12>(instr)));
  802. if(this->disass_enabled){
  803. /* generate console output when executing the command */
  804. auto mnemonic = fmt::format(
  805. "{mnemonic:10} {rd}, {imm}({rs1})", fmt::arg("mnemonic", "lw"),
  806. fmt::arg("rd", name(rd)), fmt::arg("imm", imm), fmt::arg("rs1", name(rs1)));
  807. std::vector<Value*> args {
  808. this->core_ptr,
  809. this->gen_const(64, pc.val),
  810. this->builder.CreateGlobalStringPtr(mnemonic),
  811. };
  812. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  813. }
  814. Value* cur_pc_val = this->gen_const(64, pc.val);
  815. pc=pc+4;
  816. Value* offs_val = this->builder.CreateAdd(
  817. this->gen_ext(
  818. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  819. 64, true),
  820. this->gen_const(64U, imm));
  821. if(rd != 0){
  822. Value* Xtmp0_val = this->gen_ext(
  823. this->gen_read_mem(traits<ARCH>::MEM, offs_val, 32/8),
  824. 64,
  825. true);
  826. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  827. }
  828. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  829. this->gen_sync(POST_SYNC, 12);
  830. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  831. this->gen_trap_check(bb);
  832. return std::make_tuple(CONT, bb);
  833. }
  834. /* instruction 13: LBU */
  835. std::tuple<continuation_e, BasicBlock*> __lbu(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  836. bb->setName("LBU");
  837. this->gen_sync(PRE_SYNC, 13);
  838. uint8_t rd = ((bit_sub<7,5>(instr)));
  839. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  840. int16_t imm = signextend<int16_t,12>((bit_sub<20,12>(instr)));
  841. if(this->disass_enabled){
  842. /* generate console output when executing the command */
  843. auto mnemonic = fmt::format(
  844. "{mnemonic:10} {rd}, {imm}({rs1})", fmt::arg("mnemonic", "lbu"),
  845. fmt::arg("rd", name(rd)), fmt::arg("imm", imm), fmt::arg("rs1", name(rs1)));
  846. std::vector<Value*> args {
  847. this->core_ptr,
  848. this->gen_const(64, pc.val),
  849. this->builder.CreateGlobalStringPtr(mnemonic),
  850. };
  851. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  852. }
  853. Value* cur_pc_val = this->gen_const(64, pc.val);
  854. pc=pc+4;
  855. Value* offs_val = this->builder.CreateAdd(
  856. this->gen_ext(
  857. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  858. 64, true),
  859. this->gen_const(64U, imm));
  860. if(rd != 0){
  861. Value* Xtmp0_val = this->gen_ext(
  862. this->gen_read_mem(traits<ARCH>::MEM, offs_val, 8/8),
  863. 64,
  864. false);
  865. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  866. }
  867. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  868. this->gen_sync(POST_SYNC, 13);
  869. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  870. this->gen_trap_check(bb);
  871. return std::make_tuple(CONT, bb);
  872. }
  873. /* instruction 14: LHU */
  874. std::tuple<continuation_e, BasicBlock*> __lhu(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  875. bb->setName("LHU");
  876. this->gen_sync(PRE_SYNC, 14);
  877. uint8_t rd = ((bit_sub<7,5>(instr)));
  878. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  879. int16_t imm = signextend<int16_t,12>((bit_sub<20,12>(instr)));
  880. if(this->disass_enabled){
  881. /* generate console output when executing the command */
  882. auto mnemonic = fmt::format(
  883. "{mnemonic:10} {rd}, {imm}({rs1})", fmt::arg("mnemonic", "lhu"),
  884. fmt::arg("rd", name(rd)), fmt::arg("imm", imm), fmt::arg("rs1", name(rs1)));
  885. std::vector<Value*> args {
  886. this->core_ptr,
  887. this->gen_const(64, pc.val),
  888. this->builder.CreateGlobalStringPtr(mnemonic),
  889. };
  890. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  891. }
  892. Value* cur_pc_val = this->gen_const(64, pc.val);
  893. pc=pc+4;
  894. Value* offs_val = this->builder.CreateAdd(
  895. this->gen_ext(
  896. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  897. 64, true),
  898. this->gen_const(64U, imm));
  899. if(rd != 0){
  900. Value* Xtmp0_val = this->gen_ext(
  901. this->gen_read_mem(traits<ARCH>::MEM, offs_val, 16/8),
  902. 64,
  903. false);
  904. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  905. }
  906. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  907. this->gen_sync(POST_SYNC, 14);
  908. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  909. this->gen_trap_check(bb);
  910. return std::make_tuple(CONT, bb);
  911. }
  912. /* instruction 15: SB */
  913. std::tuple<continuation_e, BasicBlock*> __sb(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  914. bb->setName("SB");
  915. this->gen_sync(PRE_SYNC, 15);
  916. int16_t imm = signextend<int16_t,12>((bit_sub<7,5>(instr)) | (bit_sub<25,7>(instr) << 5));
  917. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  918. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  919. if(this->disass_enabled){
  920. /* generate console output when executing the command */
  921. auto mnemonic = fmt::format(
  922. "{mnemonic:10} {rs2}, {imm}({rs1})", fmt::arg("mnemonic", "sb"),
  923. fmt::arg("rs2", name(rs2)), fmt::arg("imm", imm), fmt::arg("rs1", name(rs1)));
  924. std::vector<Value*> args {
  925. this->core_ptr,
  926. this->gen_const(64, pc.val),
  927. this->builder.CreateGlobalStringPtr(mnemonic),
  928. };
  929. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  930. }
  931. Value* cur_pc_val = this->gen_const(64, pc.val);
  932. pc=pc+4;
  933. Value* offs_val = this->builder.CreateAdd(
  934. this->gen_ext(
  935. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  936. 64, true),
  937. this->gen_const(64U, imm));
  938. Value* MEMtmp0_val = this->gen_reg_load(rs2 + traits<ARCH>::X0, 0);
  939. this->gen_write_mem(
  940. traits<ARCH>::MEM,
  941. offs_val,
  942. this->builder.CreateZExtOrTrunc(MEMtmp0_val,this->get_type(8)));
  943. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  944. this->gen_sync(POST_SYNC, 15);
  945. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  946. this->gen_trap_check(bb);
  947. return std::make_tuple(CONT, bb);
  948. }
  949. /* instruction 16: SH */
  950. std::tuple<continuation_e, BasicBlock*> __sh(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  951. bb->setName("SH");
  952. this->gen_sync(PRE_SYNC, 16);
  953. int16_t imm = signextend<int16_t,12>((bit_sub<7,5>(instr)) | (bit_sub<25,7>(instr) << 5));
  954. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  955. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  956. if(this->disass_enabled){
  957. /* generate console output when executing the command */
  958. auto mnemonic = fmt::format(
  959. "{mnemonic:10} {rs2}, {imm}({rs1})", fmt::arg("mnemonic", "sh"),
  960. fmt::arg("rs2", name(rs2)), fmt::arg("imm", imm), fmt::arg("rs1", name(rs1)));
  961. std::vector<Value*> args {
  962. this->core_ptr,
  963. this->gen_const(64, pc.val),
  964. this->builder.CreateGlobalStringPtr(mnemonic),
  965. };
  966. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  967. }
  968. Value* cur_pc_val = this->gen_const(64, pc.val);
  969. pc=pc+4;
  970. Value* offs_val = this->builder.CreateAdd(
  971. this->gen_ext(
  972. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  973. 64, true),
  974. this->gen_const(64U, imm));
  975. Value* MEMtmp0_val = this->gen_reg_load(rs2 + traits<ARCH>::X0, 0);
  976. this->gen_write_mem(
  977. traits<ARCH>::MEM,
  978. offs_val,
  979. this->builder.CreateZExtOrTrunc(MEMtmp0_val,this->get_type(16)));
  980. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  981. this->gen_sync(POST_SYNC, 16);
  982. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  983. this->gen_trap_check(bb);
  984. return std::make_tuple(CONT, bb);
  985. }
  986. /* instruction 17: SW */
  987. std::tuple<continuation_e, BasicBlock*> __sw(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  988. bb->setName("SW");
  989. this->gen_sync(PRE_SYNC, 17);
  990. int16_t imm = signextend<int16_t,12>((bit_sub<7,5>(instr)) | (bit_sub<25,7>(instr) << 5));
  991. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  992. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  993. if(this->disass_enabled){
  994. /* generate console output when executing the command */
  995. auto mnemonic = fmt::format(
  996. "{mnemonic:10} {rs2}, {imm}({rs1})", fmt::arg("mnemonic", "sw"),
  997. fmt::arg("rs2", name(rs2)), fmt::arg("imm", imm), fmt::arg("rs1", name(rs1)));
  998. std::vector<Value*> args {
  999. this->core_ptr,
  1000. this->gen_const(64, pc.val),
  1001. this->builder.CreateGlobalStringPtr(mnemonic),
  1002. };
  1003. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1004. }
  1005. Value* cur_pc_val = this->gen_const(64, pc.val);
  1006. pc=pc+4;
  1007. Value* offs_val = this->builder.CreateAdd(
  1008. this->gen_ext(
  1009. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  1010. 64, true),
  1011. this->gen_const(64U, imm));
  1012. Value* MEMtmp0_val = this->gen_reg_load(rs2 + traits<ARCH>::X0, 0);
  1013. this->gen_write_mem(
  1014. traits<ARCH>::MEM,
  1015. offs_val,
  1016. this->builder.CreateZExtOrTrunc(MEMtmp0_val,this->get_type(32)));
  1017. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1018. this->gen_sync(POST_SYNC, 17);
  1019. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1020. this->gen_trap_check(bb);
  1021. return std::make_tuple(CONT, bb);
  1022. }
  1023. /* instruction 18: ADDI */
  1024. std::tuple<continuation_e, BasicBlock*> __addi(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1025. bb->setName("ADDI");
  1026. this->gen_sync(PRE_SYNC, 18);
  1027. uint8_t rd = ((bit_sub<7,5>(instr)));
  1028. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1029. int16_t imm = signextend<int16_t,12>((bit_sub<20,12>(instr)));
  1030. if(this->disass_enabled){
  1031. /* generate console output when executing the command */
  1032. auto mnemonic = fmt::format(
  1033. "{mnemonic:10} {rd}, {rs1}, {imm}", fmt::arg("mnemonic", "addi"),
  1034. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("imm", imm));
  1035. std::vector<Value*> args {
  1036. this->core_ptr,
  1037. this->gen_const(64, pc.val),
  1038. this->builder.CreateGlobalStringPtr(mnemonic),
  1039. };
  1040. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1041. }
  1042. Value* cur_pc_val = this->gen_const(64, pc.val);
  1043. pc=pc+4;
  1044. if(rd != 0){
  1045. Value* Xtmp0_val = this->builder.CreateAdd(
  1046. this->gen_ext(
  1047. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  1048. 64, true),
  1049. this->gen_const(64U, imm));
  1050. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1051. }
  1052. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1053. this->gen_sync(POST_SYNC, 18);
  1054. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1055. this->gen_trap_check(bb);
  1056. return std::make_tuple(CONT, bb);
  1057. }
  1058. /* instruction 19: SLTI */
  1059. std::tuple<continuation_e, BasicBlock*> __slti(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1060. bb->setName("SLTI");
  1061. this->gen_sync(PRE_SYNC, 19);
  1062. uint8_t rd = ((bit_sub<7,5>(instr)));
  1063. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1064. int16_t imm = signextend<int16_t,12>((bit_sub<20,12>(instr)));
  1065. if(this->disass_enabled){
  1066. /* generate console output when executing the command */
  1067. auto mnemonic = fmt::format(
  1068. "{mnemonic:10} {rd}, {rs1}, {imm}", fmt::arg("mnemonic", "slti"),
  1069. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("imm", imm));
  1070. std::vector<Value*> args {
  1071. this->core_ptr,
  1072. this->gen_const(64, pc.val),
  1073. this->builder.CreateGlobalStringPtr(mnemonic),
  1074. };
  1075. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1076. }
  1077. Value* cur_pc_val = this->gen_const(64, pc.val);
  1078. pc=pc+4;
  1079. if(rd != 0){
  1080. Value* Xtmp0_val = this->gen_choose(
  1081. this->builder.CreateICmp(
  1082. ICmpInst::ICMP_SLT,
  1083. this->gen_ext(
  1084. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  1085. 64, true),
  1086. this->gen_const(64U, imm)),
  1087. this->gen_const(64U, 1),
  1088. this->gen_const(64U, 0),
  1089. 64);
  1090. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1091. }
  1092. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1093. this->gen_sync(POST_SYNC, 19);
  1094. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1095. this->gen_trap_check(bb);
  1096. return std::make_tuple(CONT, bb);
  1097. }
  1098. /* instruction 20: SLTIU */
  1099. std::tuple<continuation_e, BasicBlock*> __sltiu(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1100. bb->setName("SLTIU");
  1101. this->gen_sync(PRE_SYNC, 20);
  1102. uint8_t rd = ((bit_sub<7,5>(instr)));
  1103. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1104. int16_t imm = signextend<int16_t,12>((bit_sub<20,12>(instr)));
  1105. if(this->disass_enabled){
  1106. /* generate console output when executing the command */
  1107. auto mnemonic = fmt::format(
  1108. "{mnemonic:10} {rd}, {rs1}, {imm}", fmt::arg("mnemonic", "sltiu"),
  1109. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("imm", imm));
  1110. std::vector<Value*> args {
  1111. this->core_ptr,
  1112. this->gen_const(64, pc.val),
  1113. this->builder.CreateGlobalStringPtr(mnemonic),
  1114. };
  1115. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1116. }
  1117. Value* cur_pc_val = this->gen_const(64, pc.val);
  1118. pc=pc+4;
  1119. int64_t full_imm_val = imm;
  1120. if(rd != 0){
  1121. Value* Xtmp0_val = this->gen_choose(
  1122. this->builder.CreateICmp(
  1123. ICmpInst::ICMP_ULT,
  1124. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  1125. this->gen_const(64U, full_imm_val)),
  1126. this->gen_const(64U, 1),
  1127. this->gen_const(64U, 0),
  1128. 64);
  1129. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1130. }
  1131. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1132. this->gen_sync(POST_SYNC, 20);
  1133. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1134. this->gen_trap_check(bb);
  1135. return std::make_tuple(CONT, bb);
  1136. }
  1137. /* instruction 21: XORI */
  1138. std::tuple<continuation_e, BasicBlock*> __xori(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1139. bb->setName("XORI");
  1140. this->gen_sync(PRE_SYNC, 21);
  1141. uint8_t rd = ((bit_sub<7,5>(instr)));
  1142. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1143. int16_t imm = signextend<int16_t,12>((bit_sub<20,12>(instr)));
  1144. if(this->disass_enabled){
  1145. /* generate console output when executing the command */
  1146. auto mnemonic = fmt::format(
  1147. "{mnemonic:10} {rd}, {rs1}, {imm}", fmt::arg("mnemonic", "xori"),
  1148. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("imm", imm));
  1149. std::vector<Value*> args {
  1150. this->core_ptr,
  1151. this->gen_const(64, pc.val),
  1152. this->builder.CreateGlobalStringPtr(mnemonic),
  1153. };
  1154. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1155. }
  1156. Value* cur_pc_val = this->gen_const(64, pc.val);
  1157. pc=pc+4;
  1158. if(rd != 0){
  1159. Value* Xtmp0_val = this->builder.CreateXor(
  1160. this->gen_ext(
  1161. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  1162. 64, true),
  1163. this->gen_const(64U, imm));
  1164. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1165. }
  1166. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1167. this->gen_sync(POST_SYNC, 21);
  1168. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1169. this->gen_trap_check(bb);
  1170. return std::make_tuple(CONT, bb);
  1171. }
  1172. /* instruction 22: ORI */
  1173. std::tuple<continuation_e, BasicBlock*> __ori(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1174. bb->setName("ORI");
  1175. this->gen_sync(PRE_SYNC, 22);
  1176. uint8_t rd = ((bit_sub<7,5>(instr)));
  1177. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1178. int16_t imm = signextend<int16_t,12>((bit_sub<20,12>(instr)));
  1179. if(this->disass_enabled){
  1180. /* generate console output when executing the command */
  1181. auto mnemonic = fmt::format(
  1182. "{mnemonic:10} {rd}, {rs1}, {imm}", fmt::arg("mnemonic", "ori"),
  1183. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("imm", imm));
  1184. std::vector<Value*> args {
  1185. this->core_ptr,
  1186. this->gen_const(64, pc.val),
  1187. this->builder.CreateGlobalStringPtr(mnemonic),
  1188. };
  1189. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1190. }
  1191. Value* cur_pc_val = this->gen_const(64, pc.val);
  1192. pc=pc+4;
  1193. if(rd != 0){
  1194. Value* Xtmp0_val = this->builder.CreateOr(
  1195. this->gen_ext(
  1196. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  1197. 64, true),
  1198. this->gen_const(64U, imm));
  1199. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1200. }
  1201. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1202. this->gen_sync(POST_SYNC, 22);
  1203. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1204. this->gen_trap_check(bb);
  1205. return std::make_tuple(CONT, bb);
  1206. }
  1207. /* instruction 23: ANDI */
  1208. std::tuple<continuation_e, BasicBlock*> __andi(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1209. bb->setName("ANDI");
  1210. this->gen_sync(PRE_SYNC, 23);
  1211. uint8_t rd = ((bit_sub<7,5>(instr)));
  1212. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1213. int16_t imm = signextend<int16_t,12>((bit_sub<20,12>(instr)));
  1214. if(this->disass_enabled){
  1215. /* generate console output when executing the command */
  1216. auto mnemonic = fmt::format(
  1217. "{mnemonic:10} {rd}, {rs1}, {imm}", fmt::arg("mnemonic", "andi"),
  1218. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("imm", imm));
  1219. std::vector<Value*> args {
  1220. this->core_ptr,
  1221. this->gen_const(64, pc.val),
  1222. this->builder.CreateGlobalStringPtr(mnemonic),
  1223. };
  1224. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1225. }
  1226. Value* cur_pc_val = this->gen_const(64, pc.val);
  1227. pc=pc+4;
  1228. if(rd != 0){
  1229. Value* Xtmp0_val = this->builder.CreateAnd(
  1230. this->gen_ext(
  1231. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  1232. 64, true),
  1233. this->gen_const(64U, imm));
  1234. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1235. }
  1236. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1237. this->gen_sync(POST_SYNC, 23);
  1238. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1239. this->gen_trap_check(bb);
  1240. return std::make_tuple(CONT, bb);
  1241. }
  1242. /* instruction 24: SLLI */
  1243. std::tuple<continuation_e, BasicBlock*> __slli(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1244. bb->setName("SLLI");
  1245. this->gen_sync(PRE_SYNC, 24);
  1246. uint8_t rd = ((bit_sub<7,5>(instr)));
  1247. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1248. uint8_t shamt = ((bit_sub<20,6>(instr)));
  1249. if(this->disass_enabled){
  1250. /* generate console output when executing the command */
  1251. auto mnemonic = fmt::format(
  1252. "{mnemonic:10} {rd}, {rs1}, {shamt}", fmt::arg("mnemonic", "slli"),
  1253. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("shamt", shamt));
  1254. std::vector<Value*> args {
  1255. this->core_ptr,
  1256. this->gen_const(64, pc.val),
  1257. this->builder.CreateGlobalStringPtr(mnemonic),
  1258. };
  1259. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1260. }
  1261. Value* cur_pc_val = this->gen_const(64, pc.val);
  1262. pc=pc+4;
  1263. if(rd != 0){
  1264. Value* Xtmp0_val = this->builder.CreateShl(
  1265. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  1266. this->gen_const(64U, shamt));
  1267. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1268. }
  1269. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1270. this->gen_sync(POST_SYNC, 24);
  1271. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1272. this->gen_trap_check(bb);
  1273. return std::make_tuple(CONT, bb);
  1274. }
  1275. /* instruction 25: SRLI */
  1276. std::tuple<continuation_e, BasicBlock*> __srli(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1277. bb->setName("SRLI");
  1278. this->gen_sync(PRE_SYNC, 25);
  1279. uint8_t rd = ((bit_sub<7,5>(instr)));
  1280. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1281. uint8_t shamt = ((bit_sub<20,6>(instr)));
  1282. if(this->disass_enabled){
  1283. /* generate console output when executing the command */
  1284. auto mnemonic = fmt::format(
  1285. "{mnemonic:10} {rd}, {rs1}, {shamt}", fmt::arg("mnemonic", "srli"),
  1286. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("shamt", shamt));
  1287. std::vector<Value*> args {
  1288. this->core_ptr,
  1289. this->gen_const(64, pc.val),
  1290. this->builder.CreateGlobalStringPtr(mnemonic),
  1291. };
  1292. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1293. }
  1294. Value* cur_pc_val = this->gen_const(64, pc.val);
  1295. pc=pc+4;
  1296. if(rd != 0){
  1297. Value* Xtmp0_val = this->builder.CreateLShr(
  1298. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  1299. this->gen_const(64U, shamt));
  1300. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1301. }
  1302. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1303. this->gen_sync(POST_SYNC, 25);
  1304. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1305. this->gen_trap_check(bb);
  1306. return std::make_tuple(CONT, bb);
  1307. }
  1308. /* instruction 26: SRAI */
  1309. std::tuple<continuation_e, BasicBlock*> __srai(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1310. bb->setName("SRAI");
  1311. this->gen_sync(PRE_SYNC, 26);
  1312. uint8_t rd = ((bit_sub<7,5>(instr)));
  1313. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1314. uint8_t shamt = ((bit_sub<20,6>(instr)));
  1315. if(this->disass_enabled){
  1316. /* generate console output when executing the command */
  1317. auto mnemonic = fmt::format(
  1318. "{mnemonic:10} {rd}, {rs1}, {shamt}", fmt::arg("mnemonic", "srai"),
  1319. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("shamt", shamt));
  1320. std::vector<Value*> args {
  1321. this->core_ptr,
  1322. this->gen_const(64, pc.val),
  1323. this->builder.CreateGlobalStringPtr(mnemonic),
  1324. };
  1325. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1326. }
  1327. Value* cur_pc_val = this->gen_const(64, pc.val);
  1328. pc=pc+4;
  1329. if(rd != 0){
  1330. Value* Xtmp0_val = this->builder.CreateAShr(
  1331. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  1332. this->gen_const(64U, shamt));
  1333. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1334. }
  1335. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1336. this->gen_sync(POST_SYNC, 26);
  1337. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1338. this->gen_trap_check(bb);
  1339. return std::make_tuple(CONT, bb);
  1340. }
  1341. /* instruction 27: ADD */
  1342. std::tuple<continuation_e, BasicBlock*> __add(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1343. bb->setName("ADD");
  1344. this->gen_sync(PRE_SYNC, 27);
  1345. uint8_t rd = ((bit_sub<7,5>(instr)));
  1346. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1347. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  1348. if(this->disass_enabled){
  1349. /* generate console output when executing the command */
  1350. auto mnemonic = fmt::format(
  1351. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "add"),
  1352. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  1353. std::vector<Value*> args {
  1354. this->core_ptr,
  1355. this->gen_const(64, pc.val),
  1356. this->builder.CreateGlobalStringPtr(mnemonic),
  1357. };
  1358. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1359. }
  1360. Value* cur_pc_val = this->gen_const(64, pc.val);
  1361. pc=pc+4;
  1362. if(rd != 0){
  1363. Value* Xtmp0_val = this->builder.CreateAdd(
  1364. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  1365. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0));
  1366. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1367. }
  1368. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1369. this->gen_sync(POST_SYNC, 27);
  1370. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1371. this->gen_trap_check(bb);
  1372. return std::make_tuple(CONT, bb);
  1373. }
  1374. /* instruction 28: SUB */
  1375. std::tuple<continuation_e, BasicBlock*> __sub(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1376. bb->setName("SUB");
  1377. this->gen_sync(PRE_SYNC, 28);
  1378. uint8_t rd = ((bit_sub<7,5>(instr)));
  1379. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1380. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  1381. if(this->disass_enabled){
  1382. /* generate console output when executing the command */
  1383. auto mnemonic = fmt::format(
  1384. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "sub"),
  1385. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  1386. std::vector<Value*> args {
  1387. this->core_ptr,
  1388. this->gen_const(64, pc.val),
  1389. this->builder.CreateGlobalStringPtr(mnemonic),
  1390. };
  1391. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1392. }
  1393. Value* cur_pc_val = this->gen_const(64, pc.val);
  1394. pc=pc+4;
  1395. if(rd != 0){
  1396. Value* Xtmp0_val = this->builder.CreateSub(
  1397. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  1398. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0));
  1399. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1400. }
  1401. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1402. this->gen_sync(POST_SYNC, 28);
  1403. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1404. this->gen_trap_check(bb);
  1405. return std::make_tuple(CONT, bb);
  1406. }
  1407. /* instruction 29: SLL */
  1408. std::tuple<continuation_e, BasicBlock*> __sll(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1409. bb->setName("SLL");
  1410. this->gen_sync(PRE_SYNC, 29);
  1411. uint8_t rd = ((bit_sub<7,5>(instr)));
  1412. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1413. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  1414. if(this->disass_enabled){
  1415. /* generate console output when executing the command */
  1416. auto mnemonic = fmt::format(
  1417. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "sll"),
  1418. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  1419. std::vector<Value*> args {
  1420. this->core_ptr,
  1421. this->gen_const(64, pc.val),
  1422. this->builder.CreateGlobalStringPtr(mnemonic),
  1423. };
  1424. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1425. }
  1426. Value* cur_pc_val = this->gen_const(64, pc.val);
  1427. pc=pc+4;
  1428. if(rd != 0){
  1429. Value* Xtmp0_val = this->builder.CreateShl(
  1430. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  1431. this->builder.CreateAnd(
  1432. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  1433. this->builder.CreateSub(
  1434. this->gen_const(64U, 64),
  1435. this->gen_const(64U, 1))));
  1436. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1437. }
  1438. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1439. this->gen_sync(POST_SYNC, 29);
  1440. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1441. this->gen_trap_check(bb);
  1442. return std::make_tuple(CONT, bb);
  1443. }
  1444. /* instruction 30: SLT */
  1445. std::tuple<continuation_e, BasicBlock*> __slt(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1446. bb->setName("SLT");
  1447. this->gen_sync(PRE_SYNC, 30);
  1448. uint8_t rd = ((bit_sub<7,5>(instr)));
  1449. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1450. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  1451. if(this->disass_enabled){
  1452. /* generate console output when executing the command */
  1453. auto mnemonic = fmt::format(
  1454. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "slt"),
  1455. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  1456. std::vector<Value*> args {
  1457. this->core_ptr,
  1458. this->gen_const(64, pc.val),
  1459. this->builder.CreateGlobalStringPtr(mnemonic),
  1460. };
  1461. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1462. }
  1463. Value* cur_pc_val = this->gen_const(64, pc.val);
  1464. pc=pc+4;
  1465. if(rd != 0){
  1466. Value* Xtmp0_val = this->gen_choose(
  1467. this->builder.CreateICmp(
  1468. ICmpInst::ICMP_SLT,
  1469. this->gen_ext(
  1470. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  1471. 64, true),
  1472. this->gen_ext(
  1473. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  1474. 64, true)),
  1475. this->gen_const(64U, 1),
  1476. this->gen_const(64U, 0),
  1477. 64);
  1478. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1479. }
  1480. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1481. this->gen_sync(POST_SYNC, 30);
  1482. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1483. this->gen_trap_check(bb);
  1484. return std::make_tuple(CONT, bb);
  1485. }
  1486. /* instruction 31: SLTU */
  1487. std::tuple<continuation_e, BasicBlock*> __sltu(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1488. bb->setName("SLTU");
  1489. this->gen_sync(PRE_SYNC, 31);
  1490. uint8_t rd = ((bit_sub<7,5>(instr)));
  1491. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1492. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  1493. if(this->disass_enabled){
  1494. /* generate console output when executing the command */
  1495. auto mnemonic = fmt::format(
  1496. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "sltu"),
  1497. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  1498. std::vector<Value*> args {
  1499. this->core_ptr,
  1500. this->gen_const(64, pc.val),
  1501. this->builder.CreateGlobalStringPtr(mnemonic),
  1502. };
  1503. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1504. }
  1505. Value* cur_pc_val = this->gen_const(64, pc.val);
  1506. pc=pc+4;
  1507. if(rd != 0){
  1508. Value* Xtmp0_val = this->gen_choose(
  1509. this->builder.CreateICmp(
  1510. ICmpInst::ICMP_ULT,
  1511. this->gen_ext(
  1512. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  1513. 64,
  1514. false),
  1515. this->gen_ext(
  1516. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  1517. 64,
  1518. false)),
  1519. this->gen_const(64U, 1),
  1520. this->gen_const(64U, 0),
  1521. 64);
  1522. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1523. }
  1524. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1525. this->gen_sync(POST_SYNC, 31);
  1526. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1527. this->gen_trap_check(bb);
  1528. return std::make_tuple(CONT, bb);
  1529. }
  1530. /* instruction 32: XOR */
  1531. std::tuple<continuation_e, BasicBlock*> __xor(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1532. bb->setName("XOR");
  1533. this->gen_sync(PRE_SYNC, 32);
  1534. uint8_t rd = ((bit_sub<7,5>(instr)));
  1535. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1536. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  1537. if(this->disass_enabled){
  1538. /* generate console output when executing the command */
  1539. auto mnemonic = fmt::format(
  1540. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "xor"),
  1541. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  1542. std::vector<Value*> args {
  1543. this->core_ptr,
  1544. this->gen_const(64, pc.val),
  1545. this->builder.CreateGlobalStringPtr(mnemonic),
  1546. };
  1547. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1548. }
  1549. Value* cur_pc_val = this->gen_const(64, pc.val);
  1550. pc=pc+4;
  1551. if(rd != 0){
  1552. Value* Xtmp0_val = this->builder.CreateXor(
  1553. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  1554. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0));
  1555. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1556. }
  1557. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1558. this->gen_sync(POST_SYNC, 32);
  1559. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1560. this->gen_trap_check(bb);
  1561. return std::make_tuple(CONT, bb);
  1562. }
  1563. /* instruction 33: SRL */
  1564. std::tuple<continuation_e, BasicBlock*> __srl(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1565. bb->setName("SRL");
  1566. this->gen_sync(PRE_SYNC, 33);
  1567. uint8_t rd = ((bit_sub<7,5>(instr)));
  1568. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1569. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  1570. if(this->disass_enabled){
  1571. /* generate console output when executing the command */
  1572. auto mnemonic = fmt::format(
  1573. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "srl"),
  1574. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  1575. std::vector<Value*> args {
  1576. this->core_ptr,
  1577. this->gen_const(64, pc.val),
  1578. this->builder.CreateGlobalStringPtr(mnemonic),
  1579. };
  1580. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1581. }
  1582. Value* cur_pc_val = this->gen_const(64, pc.val);
  1583. pc=pc+4;
  1584. if(rd != 0){
  1585. Value* Xtmp0_val = this->builder.CreateLShr(
  1586. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  1587. this->builder.CreateAnd(
  1588. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  1589. this->builder.CreateSub(
  1590. this->gen_const(64U, 64),
  1591. this->gen_const(64U, 1))));
  1592. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1593. }
  1594. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1595. this->gen_sync(POST_SYNC, 33);
  1596. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1597. this->gen_trap_check(bb);
  1598. return std::make_tuple(CONT, bb);
  1599. }
  1600. /* instruction 34: SRA */
  1601. std::tuple<continuation_e, BasicBlock*> __sra(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1602. bb->setName("SRA");
  1603. this->gen_sync(PRE_SYNC, 34);
  1604. uint8_t rd = ((bit_sub<7,5>(instr)));
  1605. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1606. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  1607. if(this->disass_enabled){
  1608. /* generate console output when executing the command */
  1609. auto mnemonic = fmt::format(
  1610. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "sra"),
  1611. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  1612. std::vector<Value*> args {
  1613. this->core_ptr,
  1614. this->gen_const(64, pc.val),
  1615. this->builder.CreateGlobalStringPtr(mnemonic),
  1616. };
  1617. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1618. }
  1619. Value* cur_pc_val = this->gen_const(64, pc.val);
  1620. pc=pc+4;
  1621. if(rd != 0){
  1622. Value* Xtmp0_val = this->builder.CreateAShr(
  1623. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  1624. this->builder.CreateAnd(
  1625. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  1626. this->builder.CreateSub(
  1627. this->gen_const(64U, 64),
  1628. this->gen_const(64U, 1))));
  1629. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1630. }
  1631. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1632. this->gen_sync(POST_SYNC, 34);
  1633. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1634. this->gen_trap_check(bb);
  1635. return std::make_tuple(CONT, bb);
  1636. }
  1637. /* instruction 35: OR */
  1638. std::tuple<continuation_e, BasicBlock*> __or(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1639. bb->setName("OR");
  1640. this->gen_sync(PRE_SYNC, 35);
  1641. uint8_t rd = ((bit_sub<7,5>(instr)));
  1642. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1643. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  1644. if(this->disass_enabled){
  1645. /* generate console output when executing the command */
  1646. auto mnemonic = fmt::format(
  1647. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "or"),
  1648. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  1649. std::vector<Value*> args {
  1650. this->core_ptr,
  1651. this->gen_const(64, pc.val),
  1652. this->builder.CreateGlobalStringPtr(mnemonic),
  1653. };
  1654. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1655. }
  1656. Value* cur_pc_val = this->gen_const(64, pc.val);
  1657. pc=pc+4;
  1658. if(rd != 0){
  1659. Value* Xtmp0_val = this->builder.CreateOr(
  1660. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  1661. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0));
  1662. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1663. }
  1664. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1665. this->gen_sync(POST_SYNC, 35);
  1666. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1667. this->gen_trap_check(bb);
  1668. return std::make_tuple(CONT, bb);
  1669. }
  1670. /* instruction 36: AND */
  1671. std::tuple<continuation_e, BasicBlock*> __and(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1672. bb->setName("AND");
  1673. this->gen_sync(PRE_SYNC, 36);
  1674. uint8_t rd = ((bit_sub<7,5>(instr)));
  1675. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1676. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  1677. if(this->disass_enabled){
  1678. /* generate console output when executing the command */
  1679. auto mnemonic = fmt::format(
  1680. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "and"),
  1681. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  1682. std::vector<Value*> args {
  1683. this->core_ptr,
  1684. this->gen_const(64, pc.val),
  1685. this->builder.CreateGlobalStringPtr(mnemonic),
  1686. };
  1687. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1688. }
  1689. Value* cur_pc_val = this->gen_const(64, pc.val);
  1690. pc=pc+4;
  1691. if(rd != 0){
  1692. Value* Xtmp0_val = this->builder.CreateAnd(
  1693. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  1694. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0));
  1695. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1696. }
  1697. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1698. this->gen_sync(POST_SYNC, 36);
  1699. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1700. this->gen_trap_check(bb);
  1701. return std::make_tuple(CONT, bb);
  1702. }
  1703. /* instruction 37: FENCE */
  1704. std::tuple<continuation_e, BasicBlock*> __fence(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1705. bb->setName("FENCE");
  1706. this->gen_sync(PRE_SYNC, 37);
  1707. uint8_t rd = ((bit_sub<7,5>(instr)));
  1708. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1709. uint8_t succ = ((bit_sub<20,4>(instr)));
  1710. uint8_t pred = ((bit_sub<24,4>(instr)));
  1711. if(this->disass_enabled){
  1712. /* generate console output when executing the command */
  1713. std::vector<Value*> args {
  1714. this->core_ptr,
  1715. this->gen_const(64, pc.val),
  1716. this->builder.CreateGlobalStringPtr("fence"),
  1717. };
  1718. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1719. }
  1720. Value* cur_pc_val = this->gen_const(64, pc.val);
  1721. pc=pc+4;
  1722. Value* FENCEtmp0_val = this->builder.CreateOr(
  1723. this->builder.CreateShl(
  1724. this->gen_const(64U, pred),
  1725. this->gen_const(64U, 4)),
  1726. this->gen_const(64U, succ));
  1727. this->gen_write_mem(
  1728. traits<ARCH>::FENCE,
  1729. this->gen_const(64U, 0),
  1730. this->builder.CreateZExtOrTrunc(FENCEtmp0_val,this->get_type(64)));
  1731. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1732. this->gen_sync(POST_SYNC, 37);
  1733. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1734. this->gen_trap_check(bb);
  1735. return std::make_tuple(CONT, bb);
  1736. }
  1737. /* instruction 38: FENCE_I */
  1738. std::tuple<continuation_e, BasicBlock*> __fence_i(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1739. bb->setName("FENCE_I");
  1740. this->gen_sync(PRE_SYNC, 38);
  1741. uint8_t rd = ((bit_sub<7,5>(instr)));
  1742. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1743. uint16_t imm = ((bit_sub<20,12>(instr)));
  1744. if(this->disass_enabled){
  1745. /* generate console output when executing the command */
  1746. std::vector<Value*> args {
  1747. this->core_ptr,
  1748. this->gen_const(64, pc.val),
  1749. this->builder.CreateGlobalStringPtr("fence_i"),
  1750. };
  1751. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1752. }
  1753. Value* cur_pc_val = this->gen_const(64, pc.val);
  1754. pc=pc+4;
  1755. Value* FENCEtmp0_val = this->gen_const(64U, imm);
  1756. this->gen_write_mem(
  1757. traits<ARCH>::FENCE,
  1758. this->gen_const(64U, 1),
  1759. this->builder.CreateZExtOrTrunc(FENCEtmp0_val,this->get_type(64)));
  1760. this->builder.CreateStore(this->gen_const(32U, std::numeric_limits<uint32_t>::max()), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  1761. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1762. this->gen_sync(POST_SYNC, 38);
  1763. this->gen_trap_check(this->leave_blk);
  1764. return std::make_tuple(FLUSH, nullptr);
  1765. }
  1766. /* instruction 39: ECALL */
  1767. std::tuple<continuation_e, BasicBlock*> __ecall(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1768. bb->setName("ECALL");
  1769. this->gen_sync(PRE_SYNC, 39);
  1770. if(this->disass_enabled){
  1771. /* generate console output when executing the command */
  1772. std::vector<Value*> args {
  1773. this->core_ptr,
  1774. this->gen_const(64, pc.val),
  1775. this->builder.CreateGlobalStringPtr("ecall"),
  1776. };
  1777. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1778. }
  1779. Value* cur_pc_val = this->gen_const(64, pc.val);
  1780. pc=pc+4;
  1781. this->gen_raise_trap(0, 11);
  1782. this->gen_sync(POST_SYNC, 39);
  1783. this->gen_trap_check(this->leave_blk);
  1784. return std::make_tuple(BRANCH, nullptr);
  1785. }
  1786. /* instruction 40: EBREAK */
  1787. std::tuple<continuation_e, BasicBlock*> __ebreak(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1788. bb->setName("EBREAK");
  1789. this->gen_sync(PRE_SYNC, 40);
  1790. if(this->disass_enabled){
  1791. /* generate console output when executing the command */
  1792. std::vector<Value*> args {
  1793. this->core_ptr,
  1794. this->gen_const(64, pc.val),
  1795. this->builder.CreateGlobalStringPtr("ebreak"),
  1796. };
  1797. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1798. }
  1799. Value* cur_pc_val = this->gen_const(64, pc.val);
  1800. pc=pc+4;
  1801. this->gen_raise_trap(0, 3);
  1802. this->gen_sync(POST_SYNC, 40);
  1803. this->gen_trap_check(this->leave_blk);
  1804. return std::make_tuple(BRANCH, nullptr);
  1805. }
  1806. /* instruction 41: URET */
  1807. std::tuple<continuation_e, BasicBlock*> __uret(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1808. bb->setName("URET");
  1809. this->gen_sync(PRE_SYNC, 41);
  1810. if(this->disass_enabled){
  1811. /* generate console output when executing the command */
  1812. std::vector<Value*> args {
  1813. this->core_ptr,
  1814. this->gen_const(64, pc.val),
  1815. this->builder.CreateGlobalStringPtr("uret"),
  1816. };
  1817. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1818. }
  1819. Value* cur_pc_val = this->gen_const(64, pc.val);
  1820. pc=pc+4;
  1821. this->gen_leave_trap(0);
  1822. this->gen_sync(POST_SYNC, 41);
  1823. this->gen_trap_check(this->leave_blk);
  1824. return std::make_tuple(BRANCH, nullptr);
  1825. }
  1826. /* instruction 42: SRET */
  1827. std::tuple<continuation_e, BasicBlock*> __sret(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1828. bb->setName("SRET");
  1829. this->gen_sync(PRE_SYNC, 42);
  1830. if(this->disass_enabled){
  1831. /* generate console output when executing the command */
  1832. std::vector<Value*> args {
  1833. this->core_ptr,
  1834. this->gen_const(64, pc.val),
  1835. this->builder.CreateGlobalStringPtr("sret"),
  1836. };
  1837. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1838. }
  1839. Value* cur_pc_val = this->gen_const(64, pc.val);
  1840. pc=pc+4;
  1841. this->gen_leave_trap(1);
  1842. this->gen_sync(POST_SYNC, 42);
  1843. this->gen_trap_check(this->leave_blk);
  1844. return std::make_tuple(BRANCH, nullptr);
  1845. }
  1846. /* instruction 43: MRET */
  1847. std::tuple<continuation_e, BasicBlock*> __mret(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1848. bb->setName("MRET");
  1849. this->gen_sync(PRE_SYNC, 43);
  1850. if(this->disass_enabled){
  1851. /* generate console output when executing the command */
  1852. std::vector<Value*> args {
  1853. this->core_ptr,
  1854. this->gen_const(64, pc.val),
  1855. this->builder.CreateGlobalStringPtr("mret"),
  1856. };
  1857. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1858. }
  1859. Value* cur_pc_val = this->gen_const(64, pc.val);
  1860. pc=pc+4;
  1861. this->gen_leave_trap(3);
  1862. this->gen_sync(POST_SYNC, 43);
  1863. this->gen_trap_check(this->leave_blk);
  1864. return std::make_tuple(BRANCH, nullptr);
  1865. }
  1866. /* instruction 44: WFI */
  1867. std::tuple<continuation_e, BasicBlock*> __wfi(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1868. bb->setName("WFI");
  1869. this->gen_sync(PRE_SYNC, 44);
  1870. if(this->disass_enabled){
  1871. /* generate console output when executing the command */
  1872. std::vector<Value*> args {
  1873. this->core_ptr,
  1874. this->gen_const(64, pc.val),
  1875. this->builder.CreateGlobalStringPtr("wfi"),
  1876. };
  1877. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1878. }
  1879. Value* cur_pc_val = this->gen_const(64, pc.val);
  1880. pc=pc+4;
  1881. this->gen_wait(1);
  1882. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1883. this->gen_sync(POST_SYNC, 44);
  1884. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1885. this->gen_trap_check(bb);
  1886. return std::make_tuple(CONT, bb);
  1887. }
  1888. /* instruction 45: SFENCE.VMA */
  1889. std::tuple<continuation_e, BasicBlock*> __sfence_vma(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1890. bb->setName("SFENCE.VMA");
  1891. this->gen_sync(PRE_SYNC, 45);
  1892. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1893. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  1894. if(this->disass_enabled){
  1895. /* generate console output when executing the command */
  1896. std::vector<Value*> args {
  1897. this->core_ptr,
  1898. this->gen_const(64, pc.val),
  1899. this->builder.CreateGlobalStringPtr("sfence.vma"),
  1900. };
  1901. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1902. }
  1903. Value* cur_pc_val = this->gen_const(64, pc.val);
  1904. pc=pc+4;
  1905. Value* FENCEtmp0_val = this->gen_const(64U, rs1);
  1906. this->gen_write_mem(
  1907. traits<ARCH>::FENCE,
  1908. this->gen_const(64U, 2),
  1909. this->builder.CreateZExtOrTrunc(FENCEtmp0_val,this->get_type(64)));
  1910. Value* FENCEtmp1_val = this->gen_const(64U, rs2);
  1911. this->gen_write_mem(
  1912. traits<ARCH>::FENCE,
  1913. this->gen_const(64U, 3),
  1914. this->builder.CreateZExtOrTrunc(FENCEtmp1_val,this->get_type(64)));
  1915. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1916. this->gen_sync(POST_SYNC, 45);
  1917. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1918. this->gen_trap_check(bb);
  1919. return std::make_tuple(CONT, bb);
  1920. }
  1921. /* instruction 46: CSRRW */
  1922. std::tuple<continuation_e, BasicBlock*> __csrrw(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1923. bb->setName("CSRRW");
  1924. this->gen_sync(PRE_SYNC, 46);
  1925. uint8_t rd = ((bit_sub<7,5>(instr)));
  1926. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1927. uint16_t csr = ((bit_sub<20,12>(instr)));
  1928. if(this->disass_enabled){
  1929. /* generate console output when executing the command */
  1930. auto mnemonic = fmt::format(
  1931. "{mnemonic:10} {rd}, {csr}, {rs1}", fmt::arg("mnemonic", "csrrw"),
  1932. fmt::arg("rd", name(rd)), fmt::arg("csr", csr), fmt::arg("rs1", name(rs1)));
  1933. std::vector<Value*> args {
  1934. this->core_ptr,
  1935. this->gen_const(64, pc.val),
  1936. this->builder.CreateGlobalStringPtr(mnemonic),
  1937. };
  1938. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1939. }
  1940. Value* cur_pc_val = this->gen_const(64, pc.val);
  1941. pc=pc+4;
  1942. Value* rs_val_val = this->gen_reg_load(rs1 + traits<ARCH>::X0, 0);
  1943. if(rd != 0){
  1944. Value* csr_val_val = this->gen_read_mem(traits<ARCH>::CSR, this->gen_const(16U, csr), 64/8);
  1945. Value* CSRtmp0_val = rs_val_val;
  1946. this->gen_write_mem(
  1947. traits<ARCH>::CSR,
  1948. this->gen_const(16U, csr),
  1949. this->builder.CreateZExtOrTrunc(CSRtmp0_val,this->get_type(64)));
  1950. Value* Xtmp1_val = csr_val_val;
  1951. this->builder.CreateStore(Xtmp1_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1952. } else {
  1953. Value* CSRtmp2_val = rs_val_val;
  1954. this->gen_write_mem(
  1955. traits<ARCH>::CSR,
  1956. this->gen_const(16U, csr),
  1957. this->builder.CreateZExtOrTrunc(CSRtmp2_val,this->get_type(64)));
  1958. }
  1959. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  1960. this->gen_sync(POST_SYNC, 46);
  1961. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  1962. this->gen_trap_check(bb);
  1963. return std::make_tuple(CONT, bb);
  1964. }
  1965. /* instruction 47: CSRRS */
  1966. std::tuple<continuation_e, BasicBlock*> __csrrs(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  1967. bb->setName("CSRRS");
  1968. this->gen_sync(PRE_SYNC, 47);
  1969. uint8_t rd = ((bit_sub<7,5>(instr)));
  1970. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  1971. uint16_t csr = ((bit_sub<20,12>(instr)));
  1972. if(this->disass_enabled){
  1973. /* generate console output when executing the command */
  1974. auto mnemonic = fmt::format(
  1975. "{mnemonic:10} {rd}, {csr}, {rs1}", fmt::arg("mnemonic", "csrrs"),
  1976. fmt::arg("rd", name(rd)), fmt::arg("csr", csr), fmt::arg("rs1", name(rs1)));
  1977. std::vector<Value*> args {
  1978. this->core_ptr,
  1979. this->gen_const(64, pc.val),
  1980. this->builder.CreateGlobalStringPtr(mnemonic),
  1981. };
  1982. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  1983. }
  1984. Value* cur_pc_val = this->gen_const(64, pc.val);
  1985. pc=pc+4;
  1986. Value* xrd_val = this->gen_read_mem(traits<ARCH>::CSR, this->gen_const(16U, csr), 64/8);
  1987. Value* xrs1_val = this->gen_reg_load(rs1 + traits<ARCH>::X0, 0);
  1988. if(rd != 0){
  1989. Value* Xtmp0_val = xrd_val;
  1990. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  1991. }
  1992. if(rs1 != 0){
  1993. Value* CSRtmp1_val = this->builder.CreateOr(
  1994. xrd_val,
  1995. xrs1_val);
  1996. this->gen_write_mem(
  1997. traits<ARCH>::CSR,
  1998. this->gen_const(16U, csr),
  1999. this->builder.CreateZExtOrTrunc(CSRtmp1_val,this->get_type(64)));
  2000. }
  2001. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2002. this->gen_sync(POST_SYNC, 47);
  2003. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2004. this->gen_trap_check(bb);
  2005. return std::make_tuple(CONT, bb);
  2006. }
  2007. /* instruction 48: CSRRC */
  2008. std::tuple<continuation_e, BasicBlock*> __csrrc(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2009. bb->setName("CSRRC");
  2010. this->gen_sync(PRE_SYNC, 48);
  2011. uint8_t rd = ((bit_sub<7,5>(instr)));
  2012. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2013. uint16_t csr = ((bit_sub<20,12>(instr)));
  2014. if(this->disass_enabled){
  2015. /* generate console output when executing the command */
  2016. auto mnemonic = fmt::format(
  2017. "{mnemonic:10} {rd}, {csr}, {rs1}", fmt::arg("mnemonic", "csrrc"),
  2018. fmt::arg("rd", name(rd)), fmt::arg("csr", csr), fmt::arg("rs1", name(rs1)));
  2019. std::vector<Value*> args {
  2020. this->core_ptr,
  2021. this->gen_const(64, pc.val),
  2022. this->builder.CreateGlobalStringPtr(mnemonic),
  2023. };
  2024. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2025. }
  2026. Value* cur_pc_val = this->gen_const(64, pc.val);
  2027. pc=pc+4;
  2028. Value* xrd_val = this->gen_read_mem(traits<ARCH>::CSR, this->gen_const(16U, csr), 64/8);
  2029. Value* xrs1_val = this->gen_reg_load(rs1 + traits<ARCH>::X0, 0);
  2030. if(rd != 0){
  2031. Value* Xtmp0_val = xrd_val;
  2032. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2033. }
  2034. if(rs1 != 0){
  2035. Value* CSRtmp1_val = this->builder.CreateAnd(
  2036. xrd_val,
  2037. this->builder.CreateNot(xrs1_val));
  2038. this->gen_write_mem(
  2039. traits<ARCH>::CSR,
  2040. this->gen_const(16U, csr),
  2041. this->builder.CreateZExtOrTrunc(CSRtmp1_val,this->get_type(64)));
  2042. }
  2043. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2044. this->gen_sync(POST_SYNC, 48);
  2045. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2046. this->gen_trap_check(bb);
  2047. return std::make_tuple(CONT, bb);
  2048. }
  2049. /* instruction 49: CSRRWI */
  2050. std::tuple<continuation_e, BasicBlock*> __csrrwi(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2051. bb->setName("CSRRWI");
  2052. this->gen_sync(PRE_SYNC, 49);
  2053. uint8_t rd = ((bit_sub<7,5>(instr)));
  2054. uint8_t zimm = ((bit_sub<15,5>(instr)));
  2055. uint16_t csr = ((bit_sub<20,12>(instr)));
  2056. if(this->disass_enabled){
  2057. /* generate console output when executing the command */
  2058. auto mnemonic = fmt::format(
  2059. "{mnemonic:10} {rd}, {csr}, {zimm:#0x}", fmt::arg("mnemonic", "csrrwi"),
  2060. fmt::arg("rd", name(rd)), fmt::arg("csr", csr), fmt::arg("zimm", zimm));
  2061. std::vector<Value*> args {
  2062. this->core_ptr,
  2063. this->gen_const(64, pc.val),
  2064. this->builder.CreateGlobalStringPtr(mnemonic),
  2065. };
  2066. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2067. }
  2068. Value* cur_pc_val = this->gen_const(64, pc.val);
  2069. pc=pc+4;
  2070. if(rd != 0){
  2071. Value* Xtmp0_val = this->gen_read_mem(traits<ARCH>::CSR, this->gen_const(16U, csr), 64/8);
  2072. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2073. }
  2074. Value* CSRtmp1_val = this->gen_ext(
  2075. this->gen_const(64U, zimm),
  2076. 64,
  2077. false);
  2078. this->gen_write_mem(
  2079. traits<ARCH>::CSR,
  2080. this->gen_const(16U, csr),
  2081. this->builder.CreateZExtOrTrunc(CSRtmp1_val,this->get_type(64)));
  2082. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2083. this->gen_sync(POST_SYNC, 49);
  2084. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2085. this->gen_trap_check(bb);
  2086. return std::make_tuple(CONT, bb);
  2087. }
  2088. /* instruction 50: CSRRSI */
  2089. std::tuple<continuation_e, BasicBlock*> __csrrsi(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2090. bb->setName("CSRRSI");
  2091. this->gen_sync(PRE_SYNC, 50);
  2092. uint8_t rd = ((bit_sub<7,5>(instr)));
  2093. uint8_t zimm = ((bit_sub<15,5>(instr)));
  2094. uint16_t csr = ((bit_sub<20,12>(instr)));
  2095. if(this->disass_enabled){
  2096. /* generate console output when executing the command */
  2097. auto mnemonic = fmt::format(
  2098. "{mnemonic:10} {rd}, {csr}, {zimm:#0x}", fmt::arg("mnemonic", "csrrsi"),
  2099. fmt::arg("rd", name(rd)), fmt::arg("csr", csr), fmt::arg("zimm", zimm));
  2100. std::vector<Value*> args {
  2101. this->core_ptr,
  2102. this->gen_const(64, pc.val),
  2103. this->builder.CreateGlobalStringPtr(mnemonic),
  2104. };
  2105. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2106. }
  2107. Value* cur_pc_val = this->gen_const(64, pc.val);
  2108. pc=pc+4;
  2109. Value* res_val = this->gen_read_mem(traits<ARCH>::CSR, this->gen_const(16U, csr), 64/8);
  2110. if(zimm != 0){
  2111. Value* CSRtmp0_val = this->builder.CreateOr(
  2112. res_val,
  2113. this->gen_ext(
  2114. this->gen_const(64U, zimm),
  2115. 64,
  2116. false));
  2117. this->gen_write_mem(
  2118. traits<ARCH>::CSR,
  2119. this->gen_const(16U, csr),
  2120. this->builder.CreateZExtOrTrunc(CSRtmp0_val,this->get_type(64)));
  2121. }
  2122. if(rd != 0){
  2123. Value* Xtmp1_val = res_val;
  2124. this->builder.CreateStore(Xtmp1_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2125. }
  2126. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2127. this->gen_sync(POST_SYNC, 50);
  2128. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2129. this->gen_trap_check(bb);
  2130. return std::make_tuple(CONT, bb);
  2131. }
  2132. /* instruction 51: CSRRCI */
  2133. std::tuple<continuation_e, BasicBlock*> __csrrci(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2134. bb->setName("CSRRCI");
  2135. this->gen_sync(PRE_SYNC, 51);
  2136. uint8_t rd = ((bit_sub<7,5>(instr)));
  2137. uint8_t zimm = ((bit_sub<15,5>(instr)));
  2138. uint16_t csr = ((bit_sub<20,12>(instr)));
  2139. if(this->disass_enabled){
  2140. /* generate console output when executing the command */
  2141. auto mnemonic = fmt::format(
  2142. "{mnemonic:10} {rd}, {csr}, {zimm:#0x}", fmt::arg("mnemonic", "csrrci"),
  2143. fmt::arg("rd", name(rd)), fmt::arg("csr", csr), fmt::arg("zimm", zimm));
  2144. std::vector<Value*> args {
  2145. this->core_ptr,
  2146. this->gen_const(64, pc.val),
  2147. this->builder.CreateGlobalStringPtr(mnemonic),
  2148. };
  2149. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2150. }
  2151. Value* cur_pc_val = this->gen_const(64, pc.val);
  2152. pc=pc+4;
  2153. Value* res_val = this->gen_read_mem(traits<ARCH>::CSR, this->gen_const(16U, csr), 64/8);
  2154. if(rd != 0){
  2155. Value* Xtmp0_val = res_val;
  2156. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2157. }
  2158. if(zimm != 0){
  2159. Value* CSRtmp1_val = this->builder.CreateAnd(
  2160. res_val,
  2161. this->builder.CreateNot(this->gen_ext(
  2162. this->gen_const(64U, zimm),
  2163. 64,
  2164. false)));
  2165. this->gen_write_mem(
  2166. traits<ARCH>::CSR,
  2167. this->gen_const(16U, csr),
  2168. this->builder.CreateZExtOrTrunc(CSRtmp1_val,this->get_type(64)));
  2169. }
  2170. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2171. this->gen_sync(POST_SYNC, 51);
  2172. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2173. this->gen_trap_check(bb);
  2174. return std::make_tuple(CONT, bb);
  2175. }
  2176. /* instruction 52: LWU */
  2177. std::tuple<continuation_e, BasicBlock*> __lwu(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2178. bb->setName("LWU");
  2179. this->gen_sync(PRE_SYNC, 52);
  2180. uint8_t rd = ((bit_sub<7,5>(instr)));
  2181. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2182. int16_t imm = signextend<int16_t,12>((bit_sub<20,12>(instr)));
  2183. if(this->disass_enabled){
  2184. /* generate console output when executing the command */
  2185. auto mnemonic = fmt::format(
  2186. "{mnemonic:10} {rd}, {imm}({rs1})", fmt::arg("mnemonic", "lwu"),
  2187. fmt::arg("rd", name(rd)), fmt::arg("imm", imm), fmt::arg("rs1", name(rs1)));
  2188. std::vector<Value*> args {
  2189. this->core_ptr,
  2190. this->gen_const(64, pc.val),
  2191. this->builder.CreateGlobalStringPtr(mnemonic),
  2192. };
  2193. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2194. }
  2195. Value* cur_pc_val = this->gen_const(64, pc.val);
  2196. pc=pc+4;
  2197. Value* offs_val = this->builder.CreateAdd(
  2198. this->gen_ext(
  2199. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  2200. 64, true),
  2201. this->gen_const(64U, imm));
  2202. if(rd != 0){
  2203. Value* Xtmp0_val = this->gen_ext(
  2204. this->gen_read_mem(traits<ARCH>::MEM, offs_val, 32/8),
  2205. 64,
  2206. false);
  2207. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2208. }
  2209. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2210. this->gen_sync(POST_SYNC, 52);
  2211. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2212. this->gen_trap_check(bb);
  2213. return std::make_tuple(CONT, bb);
  2214. }
  2215. /* instruction 53: LD */
  2216. std::tuple<continuation_e, BasicBlock*> __ld(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2217. bb->setName("LD");
  2218. this->gen_sync(PRE_SYNC, 53);
  2219. uint8_t rd = ((bit_sub<7,5>(instr)));
  2220. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2221. int16_t imm = signextend<int16_t,12>((bit_sub<20,12>(instr)));
  2222. if(this->disass_enabled){
  2223. /* generate console output when executing the command */
  2224. auto mnemonic = fmt::format(
  2225. "{mnemonic:10} {rd}, {imm}({rs1})", fmt::arg("mnemonic", "ld"),
  2226. fmt::arg("rd", name(rd)), fmt::arg("imm", imm), fmt::arg("rs1", name(rs1)));
  2227. std::vector<Value*> args {
  2228. this->core_ptr,
  2229. this->gen_const(64, pc.val),
  2230. this->builder.CreateGlobalStringPtr(mnemonic),
  2231. };
  2232. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2233. }
  2234. Value* cur_pc_val = this->gen_const(64, pc.val);
  2235. pc=pc+4;
  2236. Value* offs_val = this->builder.CreateAdd(
  2237. this->gen_ext(
  2238. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  2239. 64, true),
  2240. this->gen_const(64U, imm));
  2241. if(rd != 0){
  2242. Value* Xtmp0_val = this->gen_ext(
  2243. this->gen_read_mem(traits<ARCH>::MEM, offs_val, 64/8),
  2244. 64,
  2245. true);
  2246. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2247. }
  2248. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2249. this->gen_sync(POST_SYNC, 53);
  2250. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2251. this->gen_trap_check(bb);
  2252. return std::make_tuple(CONT, bb);
  2253. }
  2254. /* instruction 54: SD */
  2255. std::tuple<continuation_e, BasicBlock*> __sd(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2256. bb->setName("SD");
  2257. this->gen_sync(PRE_SYNC, 54);
  2258. int16_t imm = signextend<int16_t,12>((bit_sub<7,5>(instr)) | (bit_sub<25,7>(instr) << 5));
  2259. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2260. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  2261. if(this->disass_enabled){
  2262. /* generate console output when executing the command */
  2263. auto mnemonic = fmt::format(
  2264. "{mnemonic:10} {rs2}, {imm}({rs1})", fmt::arg("mnemonic", "sd"),
  2265. fmt::arg("rs2", name(rs2)), fmt::arg("imm", imm), fmt::arg("rs1", name(rs1)));
  2266. std::vector<Value*> args {
  2267. this->core_ptr,
  2268. this->gen_const(64, pc.val),
  2269. this->builder.CreateGlobalStringPtr(mnemonic),
  2270. };
  2271. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2272. }
  2273. Value* cur_pc_val = this->gen_const(64, pc.val);
  2274. pc=pc+4;
  2275. Value* offs_val = this->builder.CreateAdd(
  2276. this->gen_ext(
  2277. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  2278. 64, true),
  2279. this->gen_const(64U, imm));
  2280. Value* MEMtmp0_val = this->gen_reg_load(rs2 + traits<ARCH>::X0, 0);
  2281. this->gen_write_mem(
  2282. traits<ARCH>::MEM,
  2283. offs_val,
  2284. this->builder.CreateZExtOrTrunc(MEMtmp0_val,this->get_type(64)));
  2285. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2286. this->gen_sync(POST_SYNC, 54);
  2287. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2288. this->gen_trap_check(bb);
  2289. return std::make_tuple(CONT, bb);
  2290. }
  2291. /* instruction 55: ADDIW */
  2292. std::tuple<continuation_e, BasicBlock*> __addiw(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2293. bb->setName("ADDIW");
  2294. this->gen_sync(PRE_SYNC, 55);
  2295. uint8_t rd = ((bit_sub<7,5>(instr)));
  2296. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2297. int16_t imm = signextend<int16_t,12>((bit_sub<20,12>(instr)));
  2298. if(this->disass_enabled){
  2299. /* generate console output when executing the command */
  2300. auto mnemonic = fmt::format(
  2301. "{mnemonic:10} {rd}, {rs1}, {imm}", fmt::arg("mnemonic", "addiw"),
  2302. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("imm", imm));
  2303. std::vector<Value*> args {
  2304. this->core_ptr,
  2305. this->gen_const(64, pc.val),
  2306. this->builder.CreateGlobalStringPtr(mnemonic),
  2307. };
  2308. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2309. }
  2310. Value* cur_pc_val = this->gen_const(64, pc.val);
  2311. pc=pc+4;
  2312. if(rd != 0){
  2313. Value* res_val = this->builder.CreateAdd(
  2314. this->gen_ext(
  2315. this->builder.CreateTrunc(
  2316. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  2317. this-> get_type(32)
  2318. ),
  2319. 32, true),
  2320. this->gen_const(32U, imm));
  2321. Value* Xtmp0_val = this->gen_ext(
  2322. res_val,
  2323. 64,
  2324. true);
  2325. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2326. }
  2327. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2328. this->gen_sync(POST_SYNC, 55);
  2329. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2330. this->gen_trap_check(bb);
  2331. return std::make_tuple(CONT, bb);
  2332. }
  2333. /* instruction 56: SLLIW */
  2334. std::tuple<continuation_e, BasicBlock*> __slliw(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2335. bb->setName("SLLIW");
  2336. this->gen_sync(PRE_SYNC, 56);
  2337. uint8_t rd = ((bit_sub<7,5>(instr)));
  2338. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2339. uint8_t shamt = ((bit_sub<20,5>(instr)));
  2340. if(this->disass_enabled){
  2341. /* generate console output when executing the command */
  2342. auto mnemonic = fmt::format(
  2343. "{mnemonic:10} {rd}, {rs1}, {shamt}", fmt::arg("mnemonic", "slliw"),
  2344. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("shamt", shamt));
  2345. std::vector<Value*> args {
  2346. this->core_ptr,
  2347. this->gen_const(64, pc.val),
  2348. this->builder.CreateGlobalStringPtr(mnemonic),
  2349. };
  2350. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2351. }
  2352. Value* cur_pc_val = this->gen_const(64, pc.val);
  2353. pc=pc+4;
  2354. if(rd != 0){
  2355. Value* sh_val_val = this->builder.CreateShl(
  2356. this->builder.CreateTrunc(
  2357. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  2358. this-> get_type(32)
  2359. ),
  2360. this->gen_const(32U, shamt));
  2361. Value* Xtmp0_val = this->gen_ext(
  2362. sh_val_val,
  2363. 64,
  2364. true);
  2365. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2366. }
  2367. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2368. this->gen_sync(POST_SYNC, 56);
  2369. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2370. this->gen_trap_check(bb);
  2371. return std::make_tuple(CONT, bb);
  2372. }
  2373. /* instruction 57: SRLIW */
  2374. std::tuple<continuation_e, BasicBlock*> __srliw(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2375. bb->setName("SRLIW");
  2376. this->gen_sync(PRE_SYNC, 57);
  2377. uint8_t rd = ((bit_sub<7,5>(instr)));
  2378. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2379. uint8_t shamt = ((bit_sub<20,5>(instr)));
  2380. if(this->disass_enabled){
  2381. /* generate console output when executing the command */
  2382. auto mnemonic = fmt::format(
  2383. "{mnemonic:10} {rd}, {rs1}, {shamt}", fmt::arg("mnemonic", "srliw"),
  2384. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("shamt", shamt));
  2385. std::vector<Value*> args {
  2386. this->core_ptr,
  2387. this->gen_const(64, pc.val),
  2388. this->builder.CreateGlobalStringPtr(mnemonic),
  2389. };
  2390. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2391. }
  2392. Value* cur_pc_val = this->gen_const(64, pc.val);
  2393. pc=pc+4;
  2394. if(rd != 0){
  2395. Value* sh_val_val = this->builder.CreateLShr(
  2396. this->builder.CreateTrunc(
  2397. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  2398. this-> get_type(32)
  2399. ),
  2400. this->gen_const(32U, shamt));
  2401. Value* Xtmp0_val = this->gen_ext(
  2402. sh_val_val,
  2403. 64,
  2404. true);
  2405. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2406. }
  2407. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2408. this->gen_sync(POST_SYNC, 57);
  2409. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2410. this->gen_trap_check(bb);
  2411. return std::make_tuple(CONT, bb);
  2412. }
  2413. /* instruction 58: SRAIW */
  2414. std::tuple<continuation_e, BasicBlock*> __sraiw(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2415. bb->setName("SRAIW");
  2416. this->gen_sync(PRE_SYNC, 58);
  2417. uint8_t rd = ((bit_sub<7,5>(instr)));
  2418. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2419. uint8_t shamt = ((bit_sub<20,5>(instr)));
  2420. if(this->disass_enabled){
  2421. /* generate console output when executing the command */
  2422. auto mnemonic = fmt::format(
  2423. "{mnemonic:10} {rd}, {rs1}, {shamt}", fmt::arg("mnemonic", "sraiw"),
  2424. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("shamt", shamt));
  2425. std::vector<Value*> args {
  2426. this->core_ptr,
  2427. this->gen_const(64, pc.val),
  2428. this->builder.CreateGlobalStringPtr(mnemonic),
  2429. };
  2430. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2431. }
  2432. Value* cur_pc_val = this->gen_const(64, pc.val);
  2433. pc=pc+4;
  2434. if(rd != 0){
  2435. Value* sh_val_val = this->builder.CreateAShr(
  2436. this->builder.CreateTrunc(
  2437. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  2438. this-> get_type(32)
  2439. ),
  2440. this->gen_const(32U, shamt));
  2441. Value* Xtmp0_val = this->gen_ext(
  2442. sh_val_val,
  2443. 64,
  2444. true);
  2445. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2446. }
  2447. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2448. this->gen_sync(POST_SYNC, 58);
  2449. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2450. this->gen_trap_check(bb);
  2451. return std::make_tuple(CONT, bb);
  2452. }
  2453. /* instruction 59: ADDW */
  2454. std::tuple<continuation_e, BasicBlock*> __addw(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2455. bb->setName("ADDW");
  2456. this->gen_sync(PRE_SYNC, 59);
  2457. uint8_t rd = ((bit_sub<7,5>(instr)));
  2458. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2459. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  2460. if(this->disass_enabled){
  2461. /* generate console output when executing the command */
  2462. std::vector<Value*> args {
  2463. this->core_ptr,
  2464. this->gen_const(64, pc.val),
  2465. this->builder.CreateGlobalStringPtr("addw"),
  2466. };
  2467. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2468. }
  2469. Value* cur_pc_val = this->gen_const(64, pc.val);
  2470. pc=pc+4;
  2471. if(rd != 0){
  2472. Value* res_val = this->builder.CreateAdd(
  2473. this->builder.CreateTrunc(
  2474. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  2475. this-> get_type(32)
  2476. ),
  2477. this->builder.CreateTrunc(
  2478. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  2479. this-> get_type(32)
  2480. ));
  2481. Value* Xtmp0_val = this->gen_ext(
  2482. res_val,
  2483. 64,
  2484. true);
  2485. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2486. }
  2487. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2488. this->gen_sync(POST_SYNC, 59);
  2489. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2490. this->gen_trap_check(bb);
  2491. return std::make_tuple(CONT, bb);
  2492. }
  2493. /* instruction 60: SUBW */
  2494. std::tuple<continuation_e, BasicBlock*> __subw(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2495. bb->setName("SUBW");
  2496. this->gen_sync(PRE_SYNC, 60);
  2497. uint8_t rd = ((bit_sub<7,5>(instr)));
  2498. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2499. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  2500. if(this->disass_enabled){
  2501. /* generate console output when executing the command */
  2502. std::vector<Value*> args {
  2503. this->core_ptr,
  2504. this->gen_const(64, pc.val),
  2505. this->builder.CreateGlobalStringPtr("subw"),
  2506. };
  2507. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2508. }
  2509. Value* cur_pc_val = this->gen_const(64, pc.val);
  2510. pc=pc+4;
  2511. if(rd != 0){
  2512. Value* res_val = this->builder.CreateSub(
  2513. this->builder.CreateTrunc(
  2514. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  2515. this-> get_type(32)
  2516. ),
  2517. this->builder.CreateTrunc(
  2518. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  2519. this-> get_type(32)
  2520. ));
  2521. Value* Xtmp0_val = this->gen_ext(
  2522. res_val,
  2523. 64,
  2524. true);
  2525. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2526. }
  2527. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2528. this->gen_sync(POST_SYNC, 60);
  2529. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2530. this->gen_trap_check(bb);
  2531. return std::make_tuple(CONT, bb);
  2532. }
  2533. /* instruction 61: SLLW */
  2534. std::tuple<continuation_e, BasicBlock*> __sllw(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2535. bb->setName("SLLW");
  2536. this->gen_sync(PRE_SYNC, 61);
  2537. uint8_t rd = ((bit_sub<7,5>(instr)));
  2538. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2539. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  2540. if(this->disass_enabled){
  2541. /* generate console output when executing the command */
  2542. auto mnemonic = fmt::format(
  2543. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "sllw"),
  2544. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  2545. std::vector<Value*> args {
  2546. this->core_ptr,
  2547. this->gen_const(64, pc.val),
  2548. this->builder.CreateGlobalStringPtr(mnemonic),
  2549. };
  2550. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2551. }
  2552. Value* cur_pc_val = this->gen_const(64, pc.val);
  2553. pc=pc+4;
  2554. if(rd != 0){
  2555. uint32_t mask_val = 0x1f;
  2556. Value* count_val = this->builder.CreateAnd(
  2557. this->builder.CreateTrunc(
  2558. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  2559. this-> get_type(32)
  2560. ),
  2561. this->gen_const(32U, mask_val));
  2562. Value* sh_val_val = this->builder.CreateShl(
  2563. this->builder.CreateTrunc(
  2564. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  2565. this-> get_type(32)
  2566. ),
  2567. count_val);
  2568. Value* Xtmp0_val = this->gen_ext(
  2569. sh_val_val,
  2570. 64,
  2571. true);
  2572. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2573. }
  2574. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2575. this->gen_sync(POST_SYNC, 61);
  2576. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2577. this->gen_trap_check(bb);
  2578. return std::make_tuple(CONT, bb);
  2579. }
  2580. /* instruction 62: SRLW */
  2581. std::tuple<continuation_e, BasicBlock*> __srlw(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2582. bb->setName("SRLW");
  2583. this->gen_sync(PRE_SYNC, 62);
  2584. uint8_t rd = ((bit_sub<7,5>(instr)));
  2585. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2586. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  2587. if(this->disass_enabled){
  2588. /* generate console output when executing the command */
  2589. auto mnemonic = fmt::format(
  2590. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "srlw"),
  2591. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  2592. std::vector<Value*> args {
  2593. this->core_ptr,
  2594. this->gen_const(64, pc.val),
  2595. this->builder.CreateGlobalStringPtr(mnemonic),
  2596. };
  2597. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2598. }
  2599. Value* cur_pc_val = this->gen_const(64, pc.val);
  2600. pc=pc+4;
  2601. if(rd != 0){
  2602. uint32_t mask_val = 0x1f;
  2603. Value* count_val = this->builder.CreateAnd(
  2604. this->builder.CreateTrunc(
  2605. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  2606. this-> get_type(32)
  2607. ),
  2608. this->gen_const(32U, mask_val));
  2609. Value* sh_val_val = this->builder.CreateLShr(
  2610. this->builder.CreateTrunc(
  2611. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  2612. this-> get_type(32)
  2613. ),
  2614. count_val);
  2615. Value* Xtmp0_val = this->gen_ext(
  2616. sh_val_val,
  2617. 64,
  2618. true);
  2619. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2620. }
  2621. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2622. this->gen_sync(POST_SYNC, 62);
  2623. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2624. this->gen_trap_check(bb);
  2625. return std::make_tuple(CONT, bb);
  2626. }
  2627. /* instruction 63: SRAW */
  2628. std::tuple<continuation_e, BasicBlock*> __sraw(virt_addr_t& pc, code_word_t instr, BasicBlock* bb){
  2629. bb->setName("SRAW");
  2630. this->gen_sync(PRE_SYNC, 63);
  2631. uint8_t rd = ((bit_sub<7,5>(instr)));
  2632. uint8_t rs1 = ((bit_sub<15,5>(instr)));
  2633. uint8_t rs2 = ((bit_sub<20,5>(instr)));
  2634. if(this->disass_enabled){
  2635. /* generate console output when executing the command */
  2636. auto mnemonic = fmt::format(
  2637. "{mnemonic:10} {rd}, {rs1}, {rs2}", fmt::arg("mnemonic", "sraw"),
  2638. fmt::arg("rd", name(rd)), fmt::arg("rs1", name(rs1)), fmt::arg("rs2", name(rs2)));
  2639. std::vector<Value*> args {
  2640. this->core_ptr,
  2641. this->gen_const(64, pc.val),
  2642. this->builder.CreateGlobalStringPtr(mnemonic),
  2643. };
  2644. this->builder.CreateCall(this->mod->getFunction("print_disass"), args);
  2645. }
  2646. Value* cur_pc_val = this->gen_const(64, pc.val);
  2647. pc=pc+4;
  2648. if(rd != 0){
  2649. uint32_t mask_val = 0x1f;
  2650. Value* count_val = this->builder.CreateAnd(
  2651. this->builder.CreateTrunc(
  2652. this->gen_reg_load(rs2 + traits<ARCH>::X0, 0),
  2653. this-> get_type(32)
  2654. ),
  2655. this->gen_const(32U, mask_val));
  2656. Value* sh_val_val = this->builder.CreateAShr(
  2657. this->builder.CreateTrunc(
  2658. this->gen_reg_load(rs1 + traits<ARCH>::X0, 0),
  2659. this-> get_type(32)
  2660. ),
  2661. count_val);
  2662. Value* Xtmp0_val = this->gen_ext(
  2663. sh_val_val,
  2664. 64,
  2665. true);
  2666. this->builder.CreateStore(Xtmp0_val, get_reg_ptr(rd + traits<ARCH>::X0), false);
  2667. }
  2668. this->gen_set_pc(pc, traits<ARCH>::NEXT_PC);
  2669. this->gen_sync(POST_SYNC, 63);
  2670. bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); /* create next BasicBlock in chain */
  2671. this->gen_trap_check(bb);
  2672. return std::make_tuple(CONT, bb);
  2673. }
  2674. /****************************************************************************
  2675. * end opcode definitions
  2676. ****************************************************************************/
  2677. std::tuple<continuation_e, BasicBlock *> illegal_intruction(virt_addr_t &pc, code_word_t instr, BasicBlock *bb) {
  2678. this->gen_sync(iss::PRE_SYNC, instr_descr.size());
  2679. this->builder.CreateStore(this->builder.CreateLoad(get_reg_ptr(traits<ARCH>::NEXT_PC), true),
  2680. get_reg_ptr(traits<ARCH>::PC), true);
  2681. this->builder.CreateStore(
  2682. this->builder.CreateAdd(this->builder.CreateLoad(get_reg_ptr(traits<ARCH>::ICOUNT), true),
  2683. this->gen_const(64U, 1)),
  2684. get_reg_ptr(traits<ARCH>::ICOUNT), true);
  2685. pc = pc + ((instr & 3) == 3 ? 4 : 2);
  2686. this->gen_raise_trap(0, 2); // illegal instruction trap
  2687. this->gen_sync(iss::POST_SYNC, instr_descr.size());
  2688. this->gen_trap_check(this->leave_blk);
  2689. return std::make_tuple(BRANCH, nullptr);
  2690. }
  2691. };
  2692. template <typename CODE_WORD> void debug_fn(CODE_WORD insn) {
  2693. volatile CODE_WORD x = insn;
  2694. insn = 2 * x;
  2695. }
  2696. template <typename ARCH> vm_impl<ARCH>::vm_impl() { this(new ARCH()); }
  2697. template <typename ARCH>
  2698. vm_impl<ARCH>::vm_impl(ARCH &core, unsigned core_id, unsigned cluster_id)
  2699. : vm_base<ARCH>(core, core_id, cluster_id) {
  2700. qlut[0] = lut_00.data();
  2701. qlut[1] = lut_01.data();
  2702. qlut[2] = lut_10.data();
  2703. qlut[3] = lut_11.data();
  2704. for (auto instr : instr_descr) {
  2705. auto quantrant = instr.value & 0x3;
  2706. expand_bit_mask(29, lutmasks[quantrant], instr.value >> 2, instr.mask >> 2, 0, qlut[quantrant], instr.op);
  2707. }
  2708. }
  2709. template <typename ARCH>
  2710. std::tuple<continuation_e, BasicBlock *>
  2711. vm_impl<ARCH>::gen_single_inst_behavior(virt_addr_t &pc, unsigned int &inst_cnt, BasicBlock *this_block) {
  2712. // we fetch at max 4 byte, alignment is 2
  2713. enum {TRAP_ID=1<<16};
  2714. code_word_t insn = 0;
  2715. const typename traits<ARCH>::addr_t upper_bits = ~traits<ARCH>::PGMASK;
  2716. phys_addr_t paddr(pc);
  2717. auto *const data = (uint8_t *)&insn;
  2718. paddr = this->core.v2p(pc);
  2719. if ((pc.val & upper_bits) != ((pc.val + 2) & upper_bits)) { // we may cross a page boundary
  2720. auto res = this->core.read(paddr, 2, data);
  2721. if (res != iss::Ok) throw trap_access(TRAP_ID, pc.val);
  2722. if ((insn & 0x3) == 0x3) { // this is a 32bit instruction
  2723. res = this->core.read(this->core.v2p(pc + 2), 2, data + 2);
  2724. }
  2725. } else {
  2726. auto res = this->core.read(paddr, 4, data);
  2727. if (res != iss::Ok) throw trap_access(TRAP_ID, pc.val);
  2728. }
  2729. if (insn == 0x0000006f || (insn&0xffff)==0xa001) throw simulation_stopped(0); // 'J 0' or 'C.J 0'
  2730. // curr pc on stack
  2731. ++inst_cnt;
  2732. auto lut_val = extract_fields(insn);
  2733. auto f = qlut[insn & 0x3][lut_val];
  2734. if (f == nullptr) {
  2735. f = &this_class::illegal_intruction;
  2736. }
  2737. return (this->*f)(pc, insn, this_block);
  2738. }
  2739. template <typename ARCH> void vm_impl<ARCH>::gen_leave_behavior(BasicBlock *leave_blk) {
  2740. this->builder.SetInsertPoint(leave_blk);
  2741. this->builder.CreateRet(this->builder.CreateLoad(get_reg_ptr(arch::traits<ARCH>::NEXT_PC), false));
  2742. }
  2743. template <typename ARCH> void vm_impl<ARCH>::gen_raise_trap(uint16_t trap_id, uint16_t cause) {
  2744. auto *TRAP_val = this->gen_const(32, 0x80 << 24 | (cause << 16) | trap_id);
  2745. this->builder.CreateStore(TRAP_val, get_reg_ptr(traits<ARCH>::TRAP_STATE), true);
  2746. this->builder.CreateStore(this->gen_const(32U, std::numeric_limits<uint32_t>::max()), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  2747. }
  2748. template <typename ARCH> void vm_impl<ARCH>::gen_leave_trap(unsigned lvl) {
  2749. std::vector<Value *> args{ this->core_ptr, ConstantInt::get(getContext(), APInt(64, lvl)) };
  2750. this->builder.CreateCall(this->mod->getFunction("leave_trap"), args);
  2751. auto *PC_val = this->gen_read_mem(traits<ARCH>::CSR, (lvl << 8) + 0x41, traits<ARCH>::XLEN / 8);
  2752. this->builder.CreateStore(PC_val, get_reg_ptr(traits<ARCH>::NEXT_PC), false);
  2753. this->builder.CreateStore(this->gen_const(32U, std::numeric_limits<uint32_t>::max()), get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  2754. }
  2755. template <typename ARCH> void vm_impl<ARCH>::gen_wait(unsigned type) {
  2756. std::vector<Value *> args{ this->core_ptr, ConstantInt::get(getContext(), APInt(64, type)) };
  2757. this->builder.CreateCall(this->mod->getFunction("wait"), args);
  2758. }
  2759. template <typename ARCH> void vm_impl<ARCH>::gen_trap_behavior(BasicBlock *trap_blk) {
  2760. this->builder.SetInsertPoint(trap_blk);
  2761. auto *trap_state_val = this->builder.CreateLoad(get_reg_ptr(traits<ARCH>::TRAP_STATE), true);
  2762. this->builder.CreateStore(this->gen_const(32U, std::numeric_limits<uint32_t>::max()),
  2763. get_reg_ptr(traits<ARCH>::LAST_BRANCH), false);
  2764. std::vector<Value *> args{this->core_ptr, this->adj_to64(trap_state_val),
  2765. this->adj_to64(this->builder.CreateLoad(get_reg_ptr(traits<ARCH>::PC), false))};
  2766. this->builder.CreateCall(this->mod->getFunction("enter_trap"), args);
  2767. auto *trap_addr_val = this->builder.CreateLoad(get_reg_ptr(traits<ARCH>::NEXT_PC), false);
  2768. this->builder.CreateRet(trap_addr_val);
  2769. }
  2770. template <typename ARCH> inline void vm_impl<ARCH>::gen_trap_check(BasicBlock *bb) {
  2771. auto *v = this->builder.CreateLoad(get_reg_ptr(arch::traits<ARCH>::TRAP_STATE), true);
  2772. this->gen_cond_branch(this->builder.CreateICmp(
  2773. ICmpInst::ICMP_EQ, v,
  2774. ConstantInt::get(getContext(), APInt(v->getType()->getIntegerBitWidth(), 0))),
  2775. bb, this->trap_blk, 1);
  2776. }
  2777. } // namespace rv64i
  2778. template <>
  2779. std::unique_ptr<vm_if> create<arch::rv64i>(arch::rv64i *core, unsigned short port, bool dump) {
  2780. auto ret = new rv64i::vm_impl<arch::rv64i>(*core, dump);
  2781. if (port != 0) debugger::server<debugger::gdb_session>::run_server(ret, port);
  2782. return std::unique_ptr<vm_if>(ret);
  2783. }
  2784. } // namespace iss