2
0

tcg.c 143 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651
  1. /*
  2. * Tiny Code Generator for QEMU
  3. *
  4. * Copyright (c) 2008 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. /* define it to use liveness analysis (better code) */
  25. #define USE_TCG_OPTIMIZATIONS
  26. #include "qemu/osdep.h"
  27. /* Define to jump the ELF file used to communicate with GDB. */
  28. #undef DEBUG_JIT
  29. #include "qemu/error-report.h"
  30. #include "qemu/cutils.h"
  31. #include "qemu/host-utils.h"
  32. #include "qemu/qemu-print.h"
  33. #include "qemu/timer.h"
  34. /* Note: the long term plan is to reduce the dependencies on the QEMU
  35. CPU definitions. Currently they are used for qemu_ld/st
  36. instructions */
  37. #define NO_CPU_IO_DEFS
  38. #include "cpu.h"
  39. #include "exec/exec-all.h"
  40. #if !defined(CONFIG_USER_ONLY)
  41. #include "hw/boards.h"
  42. #endif
  43. #include "tcg/tcg-op.h"
  44. #if UINTPTR_MAX == UINT32_MAX
  45. # define ELF_CLASS ELFCLASS32
  46. #else
  47. # define ELF_CLASS ELFCLASS64
  48. #endif
  49. #ifdef HOST_WORDS_BIGENDIAN
  50. # define ELF_DATA ELFDATA2MSB
  51. #else
  52. # define ELF_DATA ELFDATA2LSB
  53. #endif
  54. #include "elf.h"
  55. #include "exec/log.h"
  56. #include "sysemu/sysemu.h"
  57. /* Forward declarations for functions declared in tcg-target.inc.c and
  58. used here. */
  59. static void tcg_target_init(TCGContext *s);
  60. static const TCGTargetOpDef *tcg_target_op_def(TCGOpcode);
  61. static void tcg_target_qemu_prologue(TCGContext *s);
  62. static bool patch_reloc(TCGContext *s, tcg_insn_unit *code_ptr, int type,
  63. intptr_t value, intptr_t addend);
  64. /* The CIE and FDE header definitions will be common to all hosts. */
  65. typedef struct {
  66. uint32_t len __attribute__((aligned((sizeof(void *)))));
  67. uint32_t id;
  68. uint8_t version;
  69. char augmentation[1];
  70. uint8_t code_align;
  71. uint8_t data_align;
  72. uint8_t return_column;
  73. } DebugFrameCIE;
  74. typedef struct QEMU_PACKED {
  75. uint32_t len __attribute__((aligned((sizeof(void *)))));
  76. uint32_t cie_offset;
  77. uintptr_t func_start;
  78. uintptr_t func_len;
  79. } DebugFrameFDEHeader;
  80. typedef struct QEMU_PACKED {
  81. DebugFrameCIE cie;
  82. DebugFrameFDEHeader fde;
  83. } DebugFrameHeader;
  84. static void tcg_register_jit_int(void *buf, size_t size,
  85. const void *debug_frame,
  86. size_t debug_frame_size)
  87. __attribute__((unused));
  88. /* Forward declarations for functions declared and used in tcg-target.inc.c. */
  89. static const char *target_parse_constraint(TCGArgConstraint *ct,
  90. const char *ct_str, TCGType type);
  91. static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1,
  92. intptr_t arg2);
  93. static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg);
  94. static void tcg_out_movi(TCGContext *s, TCGType type,
  95. TCGReg ret, tcg_target_long arg);
  96. static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,
  97. const int *const_args);
  98. #if TCG_TARGET_MAYBE_vec
  99. static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
  100. TCGReg dst, TCGReg src);
  101. static bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece,
  102. TCGReg dst, TCGReg base, intptr_t offset);
  103. static void tcg_out_dupi_vec(TCGContext *s, TCGType type,
  104. TCGReg dst, tcg_target_long arg);
  105. static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, unsigned vecl,
  106. unsigned vece, const TCGArg *args,
  107. const int *const_args);
  108. #else
  109. static inline bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
  110. TCGReg dst, TCGReg src)
  111. {
  112. g_assert_not_reached();
  113. }
  114. static inline bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece,
  115. TCGReg dst, TCGReg base, intptr_t offset)
  116. {
  117. g_assert_not_reached();
  118. }
  119. static inline void tcg_out_dupi_vec(TCGContext *s, TCGType type,
  120. TCGReg dst, tcg_target_long arg)
  121. {
  122. g_assert_not_reached();
  123. }
  124. static inline void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, unsigned vecl,
  125. unsigned vece, const TCGArg *args,
  126. const int *const_args)
  127. {
  128. g_assert_not_reached();
  129. }
  130. #endif
  131. static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg, TCGReg arg1,
  132. intptr_t arg2);
  133. static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
  134. TCGReg base, intptr_t ofs);
  135. static void tcg_out_call(TCGContext *s, const tcg_insn_unit *target);
  136. static int tcg_target_const_match(tcg_target_long val, TCGType type,
  137. const TCGArgConstraint *arg_ct);
  138. #ifdef TCG_TARGET_NEED_LDST_LABELS
  139. static int tcg_out_ldst_finalize(TCGContext *s);
  140. #endif
  141. #define TCG_HIGHWATER 1024
  142. static TCGContext **tcg_ctxs;
  143. static unsigned int n_tcg_ctxs;
  144. TCGv_env cpu_env = 0;
  145. struct tcg_region_tree {
  146. QemuMutex lock;
  147. GTree *tree;
  148. /* padding to avoid false sharing is computed at run-time */
  149. };
  150. /*
  151. * We divide code_gen_buffer into equally-sized "regions" that TCG threads
  152. * dynamically allocate from as demand dictates. Given appropriate region
  153. * sizing, this minimizes flushes even when some TCG threads generate a lot
  154. * more code than others.
  155. */
  156. struct tcg_region_state {
  157. QemuMutex lock;
  158. /* fields set at init time */
  159. void *start;
  160. void *start_aligned;
  161. void *end;
  162. size_t n;
  163. size_t size; /* size of one region */
  164. size_t stride; /* .size + guard size */
  165. /* fields protected by the lock */
  166. size_t current; /* current region index */
  167. size_t agg_size_full; /* aggregate size of full regions */
  168. };
  169. static struct tcg_region_state region;
  170. /*
  171. * This is an array of struct tcg_region_tree's, with padding.
  172. * We use void * to simplify the computation of region_trees[i]; each
  173. * struct is found every tree_size bytes.
  174. */
  175. static void *region_trees;
  176. static size_t tree_size;
  177. static TCGRegSet tcg_target_available_regs[TCG_TYPE_COUNT];
  178. static TCGRegSet tcg_target_call_clobber_regs;
  179. #if TCG_TARGET_INSN_UNIT_SIZE == 1
  180. static __attribute__((unused)) inline void tcg_out8(TCGContext *s, uint8_t v)
  181. {
  182. *TCG_CODE_PTR_RW(s, s->code_ptr) = v;
  183. s->code_ptr++;
  184. }
  185. static __attribute__((unused)) inline void tcg_patch8(TCGContext *s,
  186. tcg_insn_unit *p,
  187. uint8_t v)
  188. {
  189. *TCG_CODE_PTR_RW(s, p) = v;
  190. }
  191. #endif
  192. #if TCG_TARGET_INSN_UNIT_SIZE <= 2
  193. static __attribute__((unused)) inline void tcg_out16(TCGContext *s, uint16_t v)
  194. {
  195. if (TCG_TARGET_INSN_UNIT_SIZE == 2) {
  196. *TCG_CODE_PTR_RW(s, s->code_ptr) = v;
  197. s->code_ptr++;
  198. } else {
  199. tcg_insn_unit *p = s->code_ptr;
  200. memcpy(TCG_CODE_PTR_RW(s, p), &v, sizeof(v));
  201. s->code_ptr = p + (2 / TCG_TARGET_INSN_UNIT_SIZE);
  202. }
  203. }
  204. static __attribute__((unused)) inline void tcg_patch16(TCGContext *s,
  205. tcg_insn_unit *p,
  206. uint16_t v)
  207. {
  208. if (TCG_TARGET_INSN_UNIT_SIZE == 2) {
  209. *TCG_CODE_PTR_RW(s, p) = v;
  210. } else {
  211. memcpy(TCG_CODE_PTR_RW(s, p), &v, sizeof(v));
  212. }
  213. }
  214. #endif
  215. #if TCG_TARGET_INSN_UNIT_SIZE <= 4
  216. static __attribute__((unused)) inline void tcg_out32(TCGContext *s, uint32_t v)
  217. {
  218. if (TCG_TARGET_INSN_UNIT_SIZE == 4) {
  219. *TCG_CODE_PTR_RW(s, s->code_ptr) = v;
  220. s->code_ptr++;
  221. } else {
  222. tcg_insn_unit *p = s->code_ptr;
  223. memcpy(TCG_CODE_PTR_RW(s, p), &v, sizeof(v));
  224. s->code_ptr = p + (4 / TCG_TARGET_INSN_UNIT_SIZE);
  225. }
  226. }
  227. static __attribute__((unused)) inline void tcg_patch32(TCGContext *s,
  228. tcg_insn_unit *p,
  229. uint32_t v)
  230. {
  231. if (TCG_TARGET_INSN_UNIT_SIZE == 4) {
  232. *p = v;
  233. } else {
  234. memcpy(TCG_CODE_PTR_RW(s, p), &v, sizeof(v));
  235. }
  236. }
  237. #endif
  238. #if TCG_TARGET_INSN_UNIT_SIZE <= 8
  239. static __attribute__((unused)) inline void tcg_out64(TCGContext *s, uint64_t v)
  240. {
  241. if (TCG_TARGET_INSN_UNIT_SIZE == 8) {
  242. *TCG_CODE_PTR_RW(s, s->code_ptr) = v;
  243. s->code_ptr++;
  244. } else {
  245. tcg_insn_unit *p = s->code_ptr;
  246. memcpy(TCG_CODE_PTR_RW(s, p), &v, sizeof(v));
  247. s->code_ptr = p + (8 / TCG_TARGET_INSN_UNIT_SIZE);
  248. }
  249. }
  250. static __attribute__((unused)) inline void tcg_patch64(TCGContext *s,
  251. tcg_insn_unit *p,
  252. uint64_t v)
  253. {
  254. if (TCG_TARGET_INSN_UNIT_SIZE == 8) {
  255. *p = v;
  256. } else {
  257. memcpy(TCG_CODE_PTR_RW(s, p), &v, sizeof(v));
  258. }
  259. }
  260. #endif
  261. /* label relocation processing */
  262. static void tcg_out_reloc(TCGContext *s, tcg_insn_unit *code_ptr, int type,
  263. TCGLabel *l, intptr_t addend)
  264. {
  265. TCGRelocation *r = tcg_malloc(sizeof(TCGRelocation));
  266. r->type = type;
  267. r->ptr = code_ptr;
  268. r->addend = addend;
  269. QSIMPLEQ_INSERT_TAIL(&l->relocs, r, next);
  270. }
  271. static void tcg_out_label(TCGContext *s, TCGLabel *l, const tcg_insn_unit *ptr)
  272. {
  273. tcg_debug_assert(!l->has_value);
  274. l->has_value = 1;
  275. l->u.value_ptr = ptr;
  276. }
  277. TCGLabel *gen_new_label(void)
  278. {
  279. TCGContext *s = tcg_ctx;
  280. TCGLabel *l = tcg_malloc(sizeof(TCGLabel));
  281. memset(l, 0, sizeof(TCGLabel));
  282. l->id = s->nb_labels++;
  283. QSIMPLEQ_INIT(&l->relocs);
  284. QSIMPLEQ_INSERT_TAIL(&s->labels, l, next);
  285. return l;
  286. }
  287. static bool tcg_resolve_relocs(TCGContext *s)
  288. {
  289. TCGLabel *l;
  290. QSIMPLEQ_FOREACH(l, &s->labels, next) {
  291. TCGRelocation *r;
  292. uintptr_t value = l->u.value;
  293. QSIMPLEQ_FOREACH(r, &l->relocs, next) {
  294. if (!patch_reloc(s, r->ptr, r->type, value, r->addend)) {
  295. return false;
  296. }
  297. }
  298. }
  299. return true;
  300. }
  301. static void set_jmp_reset_offset(TCGContext *s, int which)
  302. {
  303. size_t off = tcg_current_code_size(s);
  304. s->tb_jmp_reset_offset[which] = off;
  305. /* Make sure that we didn't overflow the stored offset. */
  306. assert(s->tb_jmp_reset_offset[which] == off);
  307. }
  308. #include "tcg-target.inc.c"
  309. /* compare a pointer @ptr and a tb_tc @s */
  310. static int ptr_cmp_tb_tc(const void *ptr, const struct tb_tc *s)
  311. {
  312. if (ptr >= s->ptr + s->size) {
  313. return 1;
  314. } else if (ptr < s->ptr) {
  315. return -1;
  316. }
  317. return 0;
  318. }
  319. static gint tb_tc_cmp(gconstpointer ap, gconstpointer bp)
  320. {
  321. const struct tb_tc *a = ap;
  322. const struct tb_tc *b = bp;
  323. /*
  324. * When both sizes are set, we know this isn't a lookup.
  325. * This is the most likely case: every TB must be inserted; lookups
  326. * are a lot less frequent.
  327. */
  328. if (likely(a->size && b->size)) {
  329. if (a->ptr > b->ptr) {
  330. return 1;
  331. } else if (a->ptr < b->ptr) {
  332. return -1;
  333. }
  334. /* a->ptr == b->ptr should happen only on deletions */
  335. g_assert(a->size == b->size);
  336. return 0;
  337. }
  338. /*
  339. * All lookups have either .size field set to 0.
  340. * From the glib sources we see that @ap is always the lookup key. However
  341. * the docs provide no guarantee, so we just mark this case as likely.
  342. */
  343. if (likely(a->size == 0)) {
  344. return ptr_cmp_tb_tc(a->ptr, b);
  345. }
  346. return ptr_cmp_tb_tc(b->ptr, a);
  347. }
  348. static void tcg_region_trees_init(void)
  349. {
  350. size_t i;
  351. tree_size = ROUND_UP(sizeof(struct tcg_region_tree), qemu_dcache_linesize);
  352. region_trees = qemu_memalign(qemu_dcache_linesize, region.n * tree_size);
  353. for (i = 0; i < region.n; i++) {
  354. struct tcg_region_tree *rt = region_trees + i * tree_size;
  355. qemu_mutex_init(&rt->lock);
  356. rt->tree = g_tree_new(tb_tc_cmp);
  357. }
  358. }
  359. static struct tcg_region_tree *tc_ptr_to_region_tree(void *p)
  360. {
  361. size_t region_idx;
  362. if (p < region.start_aligned) {
  363. region_idx = 0;
  364. } else {
  365. ptrdiff_t offset = p - region.start_aligned;
  366. if (offset > region.stride * (region.n - 1)) {
  367. region_idx = region.n - 1;
  368. } else {
  369. region_idx = offset / region.stride;
  370. }
  371. }
  372. return region_trees + region_idx * tree_size;
  373. }
  374. void tcg_tb_insert(TranslationBlock *tb)
  375. {
  376. struct tcg_region_tree *rt = tc_ptr_to_region_tree(tb->tc.ptr);
  377. qemu_mutex_lock(&rt->lock);
  378. g_tree_insert(rt->tree, &tb->tc, tb);
  379. qemu_mutex_unlock(&rt->lock);
  380. }
  381. void tcg_tb_remove(TranslationBlock *tb)
  382. {
  383. struct tcg_region_tree *rt = tc_ptr_to_region_tree(tb->tc.ptr);
  384. qemu_mutex_lock(&rt->lock);
  385. g_tree_remove(rt->tree, &tb->tc);
  386. qemu_mutex_unlock(&rt->lock);
  387. }
  388. /*
  389. * Find the TB 'tb' such that
  390. * tb->tc.ptr <= tc_ptr < tb->tc.ptr + tb->tc.size
  391. * Return NULL if not found.
  392. */
  393. TranslationBlock *tcg_tb_lookup(uintptr_t tc_ptr)
  394. {
  395. struct tcg_region_tree *rt = tc_ptr_to_region_tree((void *)tc_ptr);
  396. TranslationBlock *tb;
  397. struct tb_tc s = { .ptr = (void *)tc_ptr };
  398. qemu_mutex_lock(&rt->lock);
  399. tb = g_tree_lookup(rt->tree, &s);
  400. qemu_mutex_unlock(&rt->lock);
  401. return tb;
  402. }
  403. static void tcg_region_tree_lock_all(void)
  404. {
  405. size_t i;
  406. for (i = 0; i < region.n; i++) {
  407. struct tcg_region_tree *rt = region_trees + i * tree_size;
  408. qemu_mutex_lock(&rt->lock);
  409. }
  410. }
  411. static void tcg_region_tree_unlock_all(void)
  412. {
  413. size_t i;
  414. for (i = 0; i < region.n; i++) {
  415. struct tcg_region_tree *rt = region_trees + i * tree_size;
  416. qemu_mutex_unlock(&rt->lock);
  417. }
  418. }
  419. void tcg_tb_foreach(GTraverseFunc func, gpointer user_data)
  420. {
  421. size_t i;
  422. tcg_region_tree_lock_all();
  423. for (i = 0; i < region.n; i++) {
  424. struct tcg_region_tree *rt = region_trees + i * tree_size;
  425. g_tree_foreach(rt->tree, func, user_data);
  426. }
  427. tcg_region_tree_unlock_all();
  428. }
  429. size_t tcg_nb_tbs(void)
  430. {
  431. size_t nb_tbs = 0;
  432. size_t i;
  433. tcg_region_tree_lock_all();
  434. for (i = 0; i < region.n; i++) {
  435. struct tcg_region_tree *rt = region_trees + i * tree_size;
  436. nb_tbs += g_tree_nnodes(rt->tree);
  437. }
  438. tcg_region_tree_unlock_all();
  439. return nb_tbs;
  440. }
  441. static gboolean tcg_region_tree_traverse(gpointer k, gpointer v, gpointer data)
  442. {
  443. TranslationBlock *tb = v;
  444. tb_destroy(tb);
  445. return FALSE;
  446. }
  447. static void tcg_region_tree_reset_all(void)
  448. {
  449. size_t i;
  450. tcg_region_tree_lock_all();
  451. for (i = 0; i < region.n; i++) {
  452. struct tcg_region_tree *rt = region_trees + i * tree_size;
  453. g_tree_foreach(rt->tree, tcg_region_tree_traverse, NULL);
  454. /* Increment the refcount first so that destroy acts as a reset */
  455. g_tree_ref(rt->tree);
  456. g_tree_destroy(rt->tree);
  457. }
  458. tcg_region_tree_unlock_all();
  459. }
  460. static void tcg_region_bounds(size_t curr_region, void **pstart, void **pend)
  461. {
  462. void *start, *end;
  463. start = region.start_aligned + curr_region * region.stride;
  464. end = start + region.size;
  465. if (curr_region == 0) {
  466. start = region.start;
  467. }
  468. if (curr_region == region.n - 1) {
  469. end = region.end;
  470. }
  471. *pstart = start;
  472. *pend = end;
  473. }
  474. static void tcg_region_assign(TCGContext *s, size_t curr_region)
  475. {
  476. void *start, *end;
  477. tcg_region_bounds(curr_region, &start, &end);
  478. s->code_gen_buffer = start;
  479. s->code_gen_ptr = start;
  480. s->code_gen_buffer_size = end - start;
  481. s->code_gen_highwater = end - TCG_HIGHWATER;
  482. }
  483. static bool tcg_region_alloc__locked(TCGContext *s)
  484. {
  485. if (region.current == region.n) {
  486. return true;
  487. }
  488. tcg_region_assign(s, region.current);
  489. region.current++;
  490. return false;
  491. }
  492. /*
  493. * Request a new region once the one in use has filled up.
  494. * Returns true on error.
  495. */
  496. static bool tcg_region_alloc(TCGContext *s)
  497. {
  498. bool err;
  499. /* read the region size now; alloc__locked will overwrite it on success */
  500. size_t size_full = s->code_gen_buffer_size;
  501. qemu_mutex_lock(&region.lock);
  502. err = tcg_region_alloc__locked(s);
  503. if (!err) {
  504. region.agg_size_full += size_full - TCG_HIGHWATER;
  505. }
  506. qemu_mutex_unlock(&region.lock);
  507. return err;
  508. }
  509. /*
  510. * Perform a context's first region allocation.
  511. * This function does _not_ increment region.agg_size_full.
  512. */
  513. static inline bool tcg_region_initial_alloc__locked(TCGContext *s)
  514. {
  515. return tcg_region_alloc__locked(s);
  516. }
  517. /* Call from a safe-work context */
  518. void tcg_region_reset_all(void)
  519. {
  520. unsigned int n_ctxs = atomic_read(&n_tcg_ctxs);
  521. unsigned int i;
  522. qemu_mutex_lock(&region.lock);
  523. region.current = 0;
  524. region.agg_size_full = 0;
  525. for (i = 0; i < n_ctxs; i++) {
  526. TCGContext *s = atomic_read(&tcg_ctxs[i]);
  527. bool err = tcg_region_initial_alloc__locked(s);
  528. g_assert(!err);
  529. }
  530. qemu_mutex_unlock(&region.lock);
  531. tcg_region_tree_reset_all();
  532. }
  533. #ifdef CONFIG_USER_ONLY
  534. static size_t tcg_n_regions(void)
  535. {
  536. return 1;
  537. }
  538. #else
  539. /*
  540. * It is likely that some vCPUs will translate more code than others, so we
  541. * first try to set more regions than max_cpus, with those regions being of
  542. * reasonable size. If that's not possible we make do by evenly dividing
  543. * the code_gen_buffer among the vCPUs.
  544. */
  545. static size_t tcg_n_regions(void)
  546. {
  547. size_t i;
  548. /* Use a single region if all we have is one vCPU thread */
  549. #if !defined(CONFIG_USER_ONLY)
  550. MachineState *ms = MACHINE(qdev_get_machine());
  551. unsigned int max_cpus = ms->smp.max_cpus;
  552. #endif
  553. if (max_cpus == 1 || !qemu_tcg_mttcg_enabled()) {
  554. return 1;
  555. }
  556. /* Try to have more regions than max_cpus, with each region being >= 2 MB */
  557. for (i = 8; i > 0; i--) {
  558. size_t regions_per_thread = i;
  559. size_t region_size;
  560. region_size = tcg_init_ctx.code_gen_buffer_size;
  561. region_size /= max_cpus * regions_per_thread;
  562. if (region_size >= 2 * 1024u * 1024) {
  563. return max_cpus * regions_per_thread;
  564. }
  565. }
  566. /* If we can't, then just allocate one region per vCPU thread */
  567. return max_cpus;
  568. }
  569. #endif
  570. /*
  571. * Initializes region partitioning.
  572. *
  573. * Called at init time from the parent thread (i.e. the one calling
  574. * tcg_context_init), after the target's TCG globals have been set.
  575. *
  576. * Region partitioning works by splitting code_gen_buffer into separate regions,
  577. * and then assigning regions to TCG threads so that the threads can translate
  578. * code in parallel without synchronization.
  579. *
  580. * In softmmu the number of TCG threads is bounded by max_cpus, so we use at
  581. * least max_cpus regions in MTTCG. In !MTTCG we use a single region.
  582. * Note that the TCG options from the command-line (i.e. -accel accel=tcg,[...])
  583. * must have been parsed before calling this function, since it calls
  584. * qemu_tcg_mttcg_enabled().
  585. *
  586. * In user-mode we use a single region. Having multiple regions in user-mode
  587. * is not supported, because the number of vCPU threads (recall that each thread
  588. * spawned by the guest corresponds to a vCPU thread) is only bounded by the
  589. * OS, and usually this number is huge (tens of thousands is not uncommon).
  590. * Thus, given this large bound on the number of vCPU threads and the fact
  591. * that code_gen_buffer is allocated at compile-time, we cannot guarantee
  592. * that the availability of at least one region per vCPU thread.
  593. *
  594. * However, this user-mode limitation is unlikely to be a significant problem
  595. * in practice. Multi-threaded guests share most if not all of their translated
  596. * code, which makes parallel code generation less appealing than in softmmu.
  597. */
  598. void tcg_region_init(void)
  599. {
  600. void *buf = tcg_init_ctx.code_gen_buffer;
  601. void *aligned;
  602. size_t size = tcg_init_ctx.code_gen_buffer_size;
  603. size_t page_size = qemu_real_host_page_size;
  604. size_t region_size;
  605. size_t n_regions;
  606. size_t i;
  607. n_regions = tcg_n_regions();
  608. /* The first region will be 'aligned - buf' bytes larger than the others */
  609. aligned = QEMU_ALIGN_PTR_UP(buf, page_size);
  610. g_assert(aligned < tcg_init_ctx.code_gen_buffer + size);
  611. /*
  612. * Make region_size a multiple of page_size, using aligned as the start.
  613. * As a result of this we might end up with a few extra pages at the end of
  614. * the buffer; we will assign those to the last region.
  615. */
  616. region_size = (size - (aligned - buf)) / n_regions;
  617. region_size = QEMU_ALIGN_DOWN(region_size, page_size);
  618. /* A region must have at least 2 pages; one code, one guard */
  619. g_assert(region_size >= 2 * page_size);
  620. /* init the region struct */
  621. qemu_mutex_init(&region.lock);
  622. region.n = n_regions;
  623. region.size = region_size - page_size;
  624. region.stride = region_size;
  625. region.start = buf;
  626. region.start_aligned = aligned;
  627. /* page-align the end, since its last page will be a guard page */
  628. region.end = QEMU_ALIGN_PTR_DOWN(buf + size, page_size);
  629. /* account for that last guard page */
  630. region.end -= page_size;
  631. /* set guard pages */
  632. for (i = 0; i < region.n; i++) {
  633. void *start, *end;
  634. int rc;
  635. tcg_region_bounds(i, &start, &end);
  636. rc = qemu_mprotect_none(end, page_size);
  637. g_assert(!rc);
  638. }
  639. tcg_region_trees_init();
  640. /* In user-mode we support only one ctx, so do the initial allocation now */
  641. #ifdef CONFIG_USER_ONLY
  642. {
  643. bool err = tcg_region_initial_alloc__locked(tcg_ctx);
  644. g_assert(!err);
  645. }
  646. #endif
  647. }
  648. static void alloc_tcg_plugin_context(TCGContext *s)
  649. {
  650. #ifdef CONFIG_PLUGIN
  651. s->plugin_tb = g_new0(struct qemu_plugin_tb, 1);
  652. s->plugin_tb->insns =
  653. g_ptr_array_new_with_free_func(qemu_plugin_insn_cleanup_fn);
  654. #endif
  655. }
  656. /*
  657. * All TCG threads except the parent (i.e. the one that called tcg_context_init
  658. * and registered the target's TCG globals) must register with this function
  659. * before initiating translation.
  660. *
  661. * In user-mode we just point tcg_ctx to tcg_init_ctx. See the documentation
  662. * of tcg_region_init() for the reasoning behind this.
  663. *
  664. * In softmmu each caller registers its context in tcg_ctxs[]. Note that in
  665. * softmmu tcg_ctxs[] does not track tcg_ctx_init, since the initial context
  666. * is not used anymore for translation once this function is called.
  667. *
  668. * Not tracking tcg_init_ctx in tcg_ctxs[] in softmmu keeps code that iterates
  669. * over the array (e.g. tcg_code_size() the same for both softmmu and user-mode.
  670. */
  671. #ifdef CONFIG_USER_ONLY
  672. void tcg_register_thread(void)
  673. {
  674. tcg_ctx = &tcg_init_ctx;
  675. tb_exec_unlock();
  676. }
  677. #else
  678. void tcg_register_thread(void)
  679. {
  680. MachineState *ms = MACHINE(qdev_get_machine());
  681. TCGContext *s = g_malloc(sizeof(*s));
  682. unsigned int i, n;
  683. bool err;
  684. *s = tcg_init_ctx;
  685. /* Relink mem_base. */
  686. for (i = 0, n = tcg_init_ctx.nb_globals; i < n; ++i) {
  687. if (tcg_init_ctx.temps[i].mem_base) {
  688. ptrdiff_t b = tcg_init_ctx.temps[i].mem_base - tcg_init_ctx.temps;
  689. tcg_debug_assert(b >= 0 && b < n);
  690. s->temps[i].mem_base = &s->temps[b];
  691. }
  692. }
  693. /* Claim an entry in tcg_ctxs */
  694. n = atomic_fetch_inc(&n_tcg_ctxs);
  695. g_assert(n < ms->smp.max_cpus);
  696. atomic_set(&tcg_ctxs[n], s);
  697. if (n > 0) {
  698. alloc_tcg_plugin_context(s);
  699. }
  700. tcg_ctx = s;
  701. qemu_mutex_lock(&region.lock);
  702. err = tcg_region_initial_alloc__locked(tcg_ctx);
  703. g_assert(!err);
  704. qemu_mutex_unlock(&region.lock);
  705. tb_exec_unlock();
  706. }
  707. #endif /* !CONFIG_USER_ONLY */
  708. /*
  709. * Returns the size (in bytes) of all translated code (i.e. from all regions)
  710. * currently in the cache.
  711. * See also: tcg_code_capacity()
  712. * Do not confuse with tcg_current_code_size(); that one applies to a single
  713. * TCG context.
  714. */
  715. size_t tcg_code_size(void)
  716. {
  717. unsigned int n_ctxs = atomic_read(&n_tcg_ctxs);
  718. unsigned int i;
  719. size_t total;
  720. qemu_mutex_lock(&region.lock);
  721. total = region.agg_size_full;
  722. for (i = 0; i < n_ctxs; i++) {
  723. const TCGContext *s = atomic_read(&tcg_ctxs[i]);
  724. size_t size;
  725. size = atomic_read(&s->code_gen_ptr) - s->code_gen_buffer;
  726. g_assert(size <= s->code_gen_buffer_size);
  727. total += size;
  728. }
  729. qemu_mutex_unlock(&region.lock);
  730. return total;
  731. }
  732. /*
  733. * Returns the code capacity (in bytes) of the entire cache, i.e. including all
  734. * regions.
  735. * See also: tcg_code_size()
  736. */
  737. size_t tcg_code_capacity(void)
  738. {
  739. size_t guard_size, capacity;
  740. /* no need for synchronization; these variables are set at init time */
  741. guard_size = region.stride - region.size;
  742. capacity = region.end + guard_size - region.start;
  743. capacity -= region.n * (guard_size + TCG_HIGHWATER);
  744. return capacity;
  745. }
  746. size_t tcg_tb_phys_invalidate_count(void)
  747. {
  748. unsigned int n_ctxs = atomic_read(&n_tcg_ctxs);
  749. unsigned int i;
  750. size_t total = 0;
  751. for (i = 0; i < n_ctxs; i++) {
  752. const TCGContext *s = atomic_read(&tcg_ctxs[i]);
  753. total += atomic_read(&s->tb_phys_invalidate_count);
  754. }
  755. return total;
  756. }
  757. /* pool based memory allocation */
  758. void *tcg_malloc_internal(TCGContext *s, int size)
  759. {
  760. TCGPool *p;
  761. int pool_size;
  762. if (size > TCG_POOL_CHUNK_SIZE) {
  763. /* big malloc: insert a new pool (XXX: could optimize) */
  764. p = g_malloc(sizeof(TCGPool) + size);
  765. p->size = size;
  766. p->next = s->pool_first_large;
  767. s->pool_first_large = p;
  768. return p->data;
  769. } else {
  770. p = s->pool_current;
  771. if (!p) {
  772. p = s->pool_first;
  773. if (!p)
  774. goto new_pool;
  775. } else {
  776. if (!p->next) {
  777. new_pool:
  778. pool_size = TCG_POOL_CHUNK_SIZE;
  779. p = g_malloc(sizeof(TCGPool) + pool_size);
  780. p->size = pool_size;
  781. p->next = NULL;
  782. if (s->pool_current)
  783. s->pool_current->next = p;
  784. else
  785. s->pool_first = p;
  786. } else {
  787. p = p->next;
  788. }
  789. }
  790. }
  791. s->pool_current = p;
  792. s->pool_cur = p->data + size;
  793. s->pool_end = p->data + p->size;
  794. return p->data;
  795. }
  796. void tcg_pool_reset(TCGContext *s)
  797. {
  798. TCGPool *p, *t;
  799. for (p = s->pool_first_large; p; p = t) {
  800. t = p->next;
  801. g_free(p);
  802. }
  803. s->pool_first_large = NULL;
  804. s->pool_cur = s->pool_end = NULL;
  805. s->pool_current = NULL;
  806. }
  807. typedef struct TCGHelperInfo {
  808. void *func;
  809. const char *name;
  810. unsigned flags;
  811. unsigned sizemask;
  812. } TCGHelperInfo;
  813. #include "exec/helper-proto.h"
  814. static const TCGHelperInfo all_helpers[] = {
  815. #include "exec/helper-tcg.h"
  816. };
  817. static GHashTable *helper_table;
  818. static int indirect_reg_alloc_order[ARRAY_SIZE(tcg_target_reg_alloc_order)];
  819. static void process_op_defs(TCGContext *s);
  820. static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type,
  821. TCGReg reg, const char *name);
  822. void tcg_context_init(TCGContext *s)
  823. {
  824. int op, total_args, n, i;
  825. TCGOpDef *def;
  826. TCGArgConstraint *args_ct;
  827. int *sorted_args;
  828. TCGTemp *ts;
  829. memset(s, 0, sizeof(*s));
  830. s->nb_globals = 0;
  831. /* Count total number of arguments and allocate the corresponding
  832. space */
  833. total_args = 0;
  834. for(op = 0; op < NB_OPS; op++) {
  835. def = &tcg_op_defs[op];
  836. n = def->nb_iargs + def->nb_oargs;
  837. total_args += n;
  838. }
  839. args_ct = g_malloc(sizeof(TCGArgConstraint) * total_args);
  840. sorted_args = g_malloc(sizeof(int) * total_args);
  841. for(op = 0; op < NB_OPS; op++) {
  842. def = &tcg_op_defs[op];
  843. def->args_ct = args_ct;
  844. def->sorted_args = sorted_args;
  845. n = def->nb_iargs + def->nb_oargs;
  846. sorted_args += n;
  847. args_ct += n;
  848. }
  849. /* Register helpers. */
  850. /* Use g_direct_hash/equal for direct pointer comparisons on func. */
  851. helper_table = g_hash_table_new(NULL, NULL);
  852. for (i = 0; i < ARRAY_SIZE(all_helpers); ++i) {
  853. g_hash_table_insert(helper_table, (gpointer)all_helpers[i].func,
  854. (gpointer)&all_helpers[i]);
  855. }
  856. tcg_target_init(s);
  857. process_op_defs(s);
  858. /* Reverse the order of the saved registers, assuming they're all at
  859. the start of tcg_target_reg_alloc_order. */
  860. for (n = 0; n < ARRAY_SIZE(tcg_target_reg_alloc_order); ++n) {
  861. int r = tcg_target_reg_alloc_order[n];
  862. if (tcg_regset_test_reg(tcg_target_call_clobber_regs, r)) {
  863. break;
  864. }
  865. }
  866. for (i = 0; i < n; ++i) {
  867. indirect_reg_alloc_order[i] = tcg_target_reg_alloc_order[n - 1 - i];
  868. }
  869. for (; i < ARRAY_SIZE(tcg_target_reg_alloc_order); ++i) {
  870. indirect_reg_alloc_order[i] = tcg_target_reg_alloc_order[i];
  871. }
  872. alloc_tcg_plugin_context(s);
  873. tcg_ctx = s;
  874. /*
  875. * In user-mode we simply share the init context among threads, since we
  876. * use a single region. See the documentation tcg_region_init() for the
  877. * reasoning behind this.
  878. * In softmmu we will have at most max_cpus TCG threads.
  879. */
  880. #ifdef CONFIG_USER_ONLY
  881. tcg_ctxs = &tcg_ctx;
  882. n_tcg_ctxs = 1;
  883. #else
  884. MachineState *ms = MACHINE(qdev_get_machine());
  885. unsigned int max_cpus = ms->smp.max_cpus;
  886. tcg_ctxs = g_new(TCGContext *, max_cpus);
  887. #endif
  888. tcg_debug_assert(!tcg_regset_test_reg(s->reserved_regs, TCG_AREG0));
  889. ts = tcg_global_reg_new_internal(s, TCG_TYPE_PTR, TCG_AREG0, "env");
  890. cpu_env = temp_tcgv_ptr(ts);
  891. }
  892. /*
  893. * Allocate TBs right before their corresponding translated code, making
  894. * sure that TBs and code are on different cache lines.
  895. */
  896. TranslationBlock *tcg_tb_alloc(TCGContext *s)
  897. {
  898. uintptr_t align = qemu_icache_linesize;
  899. TranslationBlock *tb;
  900. void *next;
  901. retry:
  902. tb = (void *)ROUND_UP((uintptr_t)s->code_gen_ptr, align);
  903. next = (void *)ROUND_UP((uintptr_t)(tb + 1), align);
  904. if (unlikely(next > s->code_gen_highwater)) {
  905. if (tcg_region_alloc(s)) {
  906. return NULL;
  907. }
  908. goto retry;
  909. }
  910. atomic_set(&s->code_gen_ptr, next);
  911. s->data_gen_ptr = NULL;
  912. return (TranslationBlock *)TCG_CODE_PTR_RW(s, tb);
  913. }
  914. void tcg_prologue_init(TCGContext *s)
  915. {
  916. size_t prologue_size, total_size;
  917. void *buf0, *buf1;
  918. /* Put the prologue at the beginning of code_gen_buffer. */
  919. buf0 = s->code_gen_buffer;
  920. total_size = s->code_gen_buffer_size;
  921. s->code_ptr = buf0;
  922. s->code_buf = buf0;
  923. s->data_gen_ptr = NULL;
  924. s->code_gen_prologue = buf0;
  925. /* Compute a high-water mark, at which we voluntarily flush the buffer
  926. and start over. The size here is arbitrary, significantly larger
  927. than we expect the code generation for any one opcode to require. */
  928. s->code_gen_highwater = s->code_gen_buffer + (total_size - TCG_HIGHWATER);
  929. #ifdef TCG_TARGET_NEED_POOL_LABELS
  930. s->pool_labels = NULL;
  931. #endif
  932. /* Generate the prologue. */
  933. tcg_target_qemu_prologue(s);
  934. #ifdef TCG_TARGET_NEED_POOL_LABELS
  935. /* Allow the prologue to put e.g. guest_base into a pool entry. */
  936. {
  937. int result = tcg_out_pool_finalize(s);
  938. tcg_debug_assert(result == 0);
  939. }
  940. #endif
  941. buf1 = s->code_ptr;
  942. #if defined(CONFIG_IOS_JIT)
  943. flush_dcache_range((uintptr_t)TCG_CODE_PTR_RW(s, buf0),
  944. (uintptr_t)TCG_CODE_PTR_RW(s, buf1));
  945. #endif
  946. flush_icache_range((uintptr_t)buf0, (uintptr_t)buf1);
  947. /* Deduct the prologue from the buffer. */
  948. prologue_size = tcg_current_code_size(s);
  949. s->code_gen_ptr = buf1;
  950. s->code_gen_buffer = buf1;
  951. s->code_buf = buf1;
  952. total_size -= prologue_size;
  953. s->code_gen_buffer_size = total_size;
  954. tcg_register_jit(s->code_gen_buffer, total_size);
  955. #ifdef DEBUG_DISAS
  956. if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) {
  957. FILE *logfile = qemu_log_lock();
  958. qemu_log("PROLOGUE: [size=%zu]\n", prologue_size);
  959. if (s->data_gen_ptr) {
  960. size_t code_size = s->data_gen_ptr - buf0;
  961. size_t data_size = prologue_size - code_size;
  962. size_t i;
  963. log_disas(buf0, code_size, NULL);
  964. for (i = 0; i < data_size; i += sizeof(tcg_target_ulong)) {
  965. if (sizeof(tcg_target_ulong) == 8) {
  966. qemu_log("0x%08" PRIxPTR ": .quad 0x%016" PRIx64 "\n",
  967. (uintptr_t)s->data_gen_ptr + i,
  968. *(uint64_t *)(s->data_gen_ptr + i));
  969. } else {
  970. qemu_log("0x%08" PRIxPTR ": .long 0x%08x\n",
  971. (uintptr_t)s->data_gen_ptr + i,
  972. *(uint32_t *)(s->data_gen_ptr + i));
  973. }
  974. }
  975. } else {
  976. log_disas(buf0, prologue_size, NULL);
  977. }
  978. qemu_log("\n");
  979. qemu_log_flush();
  980. qemu_log_unlock(logfile);
  981. }
  982. #endif
  983. /* Assert that goto_ptr is implemented completely. */
  984. if (TCG_TARGET_HAS_goto_ptr) {
  985. tcg_debug_assert(s->code_gen_epilogue != NULL);
  986. }
  987. }
  988. void tcg_func_start(TCGContext *s)
  989. {
  990. tcg_pool_reset(s);
  991. s->nb_temps = s->nb_globals;
  992. /* No temps have been previously allocated for size or locality. */
  993. memset(s->free_temps, 0, sizeof(s->free_temps));
  994. s->nb_ops = 0;
  995. s->nb_labels = 0;
  996. s->current_frame_offset = s->frame_start;
  997. #ifdef CONFIG_DEBUG_TCG
  998. s->goto_tb_issue_mask = 0;
  999. #endif
  1000. QTAILQ_INIT(&s->ops);
  1001. QTAILQ_INIT(&s->free_ops);
  1002. QSIMPLEQ_INIT(&s->labels);
  1003. }
  1004. static inline TCGTemp *tcg_temp_alloc(TCGContext *s)
  1005. {
  1006. int n = s->nb_temps++;
  1007. tcg_debug_assert(n < TCG_MAX_TEMPS);
  1008. return memset(&s->temps[n], 0, sizeof(TCGTemp));
  1009. }
  1010. static inline TCGTemp *tcg_global_alloc(TCGContext *s)
  1011. {
  1012. TCGTemp *ts;
  1013. tcg_debug_assert(s->nb_globals == s->nb_temps);
  1014. s->nb_globals++;
  1015. ts = tcg_temp_alloc(s);
  1016. ts->temp_global = 1;
  1017. return ts;
  1018. }
  1019. static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type,
  1020. TCGReg reg, const char *name)
  1021. {
  1022. TCGTemp *ts;
  1023. if (TCG_TARGET_REG_BITS == 32 && type != TCG_TYPE_I32) {
  1024. tcg_abort();
  1025. }
  1026. ts = tcg_global_alloc(s);
  1027. ts->base_type = type;
  1028. ts->type = type;
  1029. ts->fixed_reg = 1;
  1030. ts->reg = reg;
  1031. ts->name = name;
  1032. tcg_regset_set_reg(s->reserved_regs, reg);
  1033. return ts;
  1034. }
  1035. void tcg_set_frame(TCGContext *s, TCGReg reg, intptr_t start, intptr_t size)
  1036. {
  1037. s->frame_start = start;
  1038. s->frame_end = start + size;
  1039. s->frame_temp
  1040. = tcg_global_reg_new_internal(s, TCG_TYPE_PTR, reg, "_frame");
  1041. }
  1042. TCGTemp *tcg_global_mem_new_internal(TCGType type, TCGv_ptr base,
  1043. intptr_t offset, const char *name)
  1044. {
  1045. TCGContext *s = tcg_ctx;
  1046. TCGTemp *base_ts = tcgv_ptr_temp(base);
  1047. TCGTemp *ts = tcg_global_alloc(s);
  1048. int indirect_reg = 0, bigendian = 0;
  1049. #ifdef HOST_WORDS_BIGENDIAN
  1050. bigendian = 1;
  1051. #endif
  1052. if (!base_ts->fixed_reg) {
  1053. /* We do not support double-indirect registers. */
  1054. tcg_debug_assert(!base_ts->indirect_reg);
  1055. base_ts->indirect_base = 1;
  1056. s->nb_indirects += (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64
  1057. ? 2 : 1);
  1058. indirect_reg = 1;
  1059. }
  1060. if (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64) {
  1061. TCGTemp *ts2 = tcg_global_alloc(s);
  1062. char buf[64];
  1063. ts->base_type = TCG_TYPE_I64;
  1064. ts->type = TCG_TYPE_I32;
  1065. ts->indirect_reg = indirect_reg;
  1066. ts->mem_allocated = 1;
  1067. ts->mem_base = base_ts;
  1068. ts->mem_offset = offset + bigendian * 4;
  1069. pstrcpy(buf, sizeof(buf), name);
  1070. pstrcat(buf, sizeof(buf), "_0");
  1071. ts->name = strdup(buf);
  1072. tcg_debug_assert(ts2 == ts + 1);
  1073. ts2->base_type = TCG_TYPE_I64;
  1074. ts2->type = TCG_TYPE_I32;
  1075. ts2->indirect_reg = indirect_reg;
  1076. ts2->mem_allocated = 1;
  1077. ts2->mem_base = base_ts;
  1078. ts2->mem_offset = offset + (1 - bigendian) * 4;
  1079. pstrcpy(buf, sizeof(buf), name);
  1080. pstrcat(buf, sizeof(buf), "_1");
  1081. ts2->name = strdup(buf);
  1082. } else {
  1083. ts->base_type = type;
  1084. ts->type = type;
  1085. ts->indirect_reg = indirect_reg;
  1086. ts->mem_allocated = 1;
  1087. ts->mem_base = base_ts;
  1088. ts->mem_offset = offset;
  1089. ts->name = name;
  1090. }
  1091. return ts;
  1092. }
  1093. TCGTemp *tcg_temp_new_internal(TCGType type, bool temp_local)
  1094. {
  1095. TCGContext *s = tcg_ctx;
  1096. TCGTemp *ts;
  1097. int idx, k;
  1098. k = type + (temp_local ? TCG_TYPE_COUNT : 0);
  1099. idx = find_first_bit(s->free_temps[k].l, TCG_MAX_TEMPS);
  1100. if (idx < TCG_MAX_TEMPS) {
  1101. /* There is already an available temp with the right type. */
  1102. clear_bit(idx, s->free_temps[k].l);
  1103. ts = &s->temps[idx];
  1104. ts->temp_allocated = 1;
  1105. tcg_debug_assert(ts->base_type == type);
  1106. tcg_debug_assert(ts->temp_local == temp_local);
  1107. } else {
  1108. ts = tcg_temp_alloc(s);
  1109. if (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64) {
  1110. TCGTemp *ts2 = tcg_temp_alloc(s);
  1111. ts->base_type = type;
  1112. ts->type = TCG_TYPE_I32;
  1113. ts->temp_allocated = 1;
  1114. ts->temp_local = temp_local;
  1115. tcg_debug_assert(ts2 == ts + 1);
  1116. ts2->base_type = TCG_TYPE_I64;
  1117. ts2->type = TCG_TYPE_I32;
  1118. ts2->temp_allocated = 1;
  1119. ts2->temp_local = temp_local;
  1120. } else {
  1121. ts->base_type = type;
  1122. ts->type = type;
  1123. ts->temp_allocated = 1;
  1124. ts->temp_local = temp_local;
  1125. }
  1126. }
  1127. #if defined(CONFIG_DEBUG_TCG)
  1128. s->temps_in_use++;
  1129. #endif
  1130. return ts;
  1131. }
  1132. TCGv_vec tcg_temp_new_vec(TCGType type)
  1133. {
  1134. TCGTemp *t;
  1135. #ifdef CONFIG_DEBUG_TCG
  1136. switch (type) {
  1137. case TCG_TYPE_V64:
  1138. assert(TCG_TARGET_HAS_v64);
  1139. break;
  1140. case TCG_TYPE_V128:
  1141. assert(TCG_TARGET_HAS_v128);
  1142. break;
  1143. case TCG_TYPE_V256:
  1144. assert(TCG_TARGET_HAS_v256);
  1145. break;
  1146. default:
  1147. g_assert_not_reached();
  1148. }
  1149. #endif
  1150. t = tcg_temp_new_internal(type, 0);
  1151. return temp_tcgv_vec(t);
  1152. }
  1153. /* Create a new temp of the same type as an existing temp. */
  1154. TCGv_vec tcg_temp_new_vec_matching(TCGv_vec match)
  1155. {
  1156. TCGTemp *t = tcgv_vec_temp(match);
  1157. tcg_debug_assert(t->temp_allocated != 0);
  1158. t = tcg_temp_new_internal(t->base_type, 0);
  1159. return temp_tcgv_vec(t);
  1160. }
  1161. void tcg_temp_free_internal(TCGTemp *ts)
  1162. {
  1163. TCGContext *s = tcg_ctx;
  1164. int k, idx;
  1165. #if defined(CONFIG_DEBUG_TCG)
  1166. s->temps_in_use--;
  1167. if (s->temps_in_use < 0) {
  1168. fprintf(stderr, "More temporaries freed than allocated!\n");
  1169. }
  1170. #endif
  1171. tcg_debug_assert(ts->temp_global == 0);
  1172. tcg_debug_assert(ts->temp_allocated != 0);
  1173. ts->temp_allocated = 0;
  1174. idx = temp_idx(ts);
  1175. k = ts->base_type + (ts->temp_local ? TCG_TYPE_COUNT : 0);
  1176. set_bit(idx, s->free_temps[k].l);
  1177. }
  1178. TCGv_i32 tcg_const_i32(int32_t val)
  1179. {
  1180. TCGv_i32 t0;
  1181. t0 = tcg_temp_new_i32();
  1182. tcg_gen_movi_i32(t0, val);
  1183. return t0;
  1184. }
  1185. TCGv_i64 tcg_const_i64(int64_t val)
  1186. {
  1187. TCGv_i64 t0;
  1188. t0 = tcg_temp_new_i64();
  1189. tcg_gen_movi_i64(t0, val);
  1190. return t0;
  1191. }
  1192. TCGv_i32 tcg_const_local_i32(int32_t val)
  1193. {
  1194. TCGv_i32 t0;
  1195. t0 = tcg_temp_local_new_i32();
  1196. tcg_gen_movi_i32(t0, val);
  1197. return t0;
  1198. }
  1199. TCGv_i64 tcg_const_local_i64(int64_t val)
  1200. {
  1201. TCGv_i64 t0;
  1202. t0 = tcg_temp_local_new_i64();
  1203. tcg_gen_movi_i64(t0, val);
  1204. return t0;
  1205. }
  1206. #if defined(CONFIG_DEBUG_TCG)
  1207. void tcg_clear_temp_count(void)
  1208. {
  1209. TCGContext *s = tcg_ctx;
  1210. s->temps_in_use = 0;
  1211. }
  1212. int tcg_check_temp_count(void)
  1213. {
  1214. TCGContext *s = tcg_ctx;
  1215. if (s->temps_in_use) {
  1216. /* Clear the count so that we don't give another
  1217. * warning immediately next time around.
  1218. */
  1219. s->temps_in_use = 0;
  1220. return 1;
  1221. }
  1222. return 0;
  1223. }
  1224. #endif
  1225. /* Return true if OP may appear in the opcode stream.
  1226. Test the runtime variable that controls each opcode. */
  1227. bool tcg_op_supported(TCGOpcode op)
  1228. {
  1229. const bool have_vec
  1230. = TCG_TARGET_HAS_v64 | TCG_TARGET_HAS_v128 | TCG_TARGET_HAS_v256;
  1231. switch (op) {
  1232. case INDEX_op_discard:
  1233. case INDEX_op_set_label:
  1234. case INDEX_op_call:
  1235. case INDEX_op_br:
  1236. case INDEX_op_mb:
  1237. case INDEX_op_insn_start:
  1238. case INDEX_op_exit_tb:
  1239. case INDEX_op_goto_tb:
  1240. case INDEX_op_qemu_ld_i32:
  1241. case INDEX_op_qemu_st_i32:
  1242. case INDEX_op_qemu_ld_i64:
  1243. case INDEX_op_qemu_st_i64:
  1244. return true;
  1245. case INDEX_op_goto_ptr:
  1246. return TCG_TARGET_HAS_goto_ptr;
  1247. case INDEX_op_mov_i32:
  1248. case INDEX_op_movi_i32:
  1249. case INDEX_op_setcond_i32:
  1250. case INDEX_op_brcond_i32:
  1251. case INDEX_op_ld8u_i32:
  1252. case INDEX_op_ld8s_i32:
  1253. case INDEX_op_ld16u_i32:
  1254. case INDEX_op_ld16s_i32:
  1255. case INDEX_op_ld_i32:
  1256. case INDEX_op_st8_i32:
  1257. case INDEX_op_st16_i32:
  1258. case INDEX_op_st_i32:
  1259. case INDEX_op_add_i32:
  1260. case INDEX_op_sub_i32:
  1261. case INDEX_op_mul_i32:
  1262. case INDEX_op_and_i32:
  1263. case INDEX_op_or_i32:
  1264. case INDEX_op_xor_i32:
  1265. case INDEX_op_shl_i32:
  1266. case INDEX_op_shr_i32:
  1267. case INDEX_op_sar_i32:
  1268. return true;
  1269. case INDEX_op_movcond_i32:
  1270. return TCG_TARGET_HAS_movcond_i32;
  1271. case INDEX_op_div_i32:
  1272. case INDEX_op_divu_i32:
  1273. return TCG_TARGET_HAS_div_i32;
  1274. case INDEX_op_rem_i32:
  1275. case INDEX_op_remu_i32:
  1276. return TCG_TARGET_HAS_rem_i32;
  1277. case INDEX_op_div2_i32:
  1278. case INDEX_op_divu2_i32:
  1279. return TCG_TARGET_HAS_div2_i32;
  1280. case INDEX_op_rotl_i32:
  1281. case INDEX_op_rotr_i32:
  1282. return TCG_TARGET_HAS_rot_i32;
  1283. case INDEX_op_deposit_i32:
  1284. return TCG_TARGET_HAS_deposit_i32;
  1285. case INDEX_op_extract_i32:
  1286. return TCG_TARGET_HAS_extract_i32;
  1287. case INDEX_op_sextract_i32:
  1288. return TCG_TARGET_HAS_sextract_i32;
  1289. case INDEX_op_extract2_i32:
  1290. return TCG_TARGET_HAS_extract2_i32;
  1291. case INDEX_op_add2_i32:
  1292. return TCG_TARGET_HAS_add2_i32;
  1293. case INDEX_op_sub2_i32:
  1294. return TCG_TARGET_HAS_sub2_i32;
  1295. case INDEX_op_mulu2_i32:
  1296. return TCG_TARGET_HAS_mulu2_i32;
  1297. case INDEX_op_muls2_i32:
  1298. return TCG_TARGET_HAS_muls2_i32;
  1299. case INDEX_op_muluh_i32:
  1300. return TCG_TARGET_HAS_muluh_i32;
  1301. case INDEX_op_mulsh_i32:
  1302. return TCG_TARGET_HAS_mulsh_i32;
  1303. case INDEX_op_ext8s_i32:
  1304. return TCG_TARGET_HAS_ext8s_i32;
  1305. case INDEX_op_ext16s_i32:
  1306. return TCG_TARGET_HAS_ext16s_i32;
  1307. case INDEX_op_ext8u_i32:
  1308. return TCG_TARGET_HAS_ext8u_i32;
  1309. case INDEX_op_ext16u_i32:
  1310. return TCG_TARGET_HAS_ext16u_i32;
  1311. case INDEX_op_bswap16_i32:
  1312. return TCG_TARGET_HAS_bswap16_i32;
  1313. case INDEX_op_bswap32_i32:
  1314. return TCG_TARGET_HAS_bswap32_i32;
  1315. case INDEX_op_not_i32:
  1316. return TCG_TARGET_HAS_not_i32;
  1317. case INDEX_op_neg_i32:
  1318. return TCG_TARGET_HAS_neg_i32;
  1319. case INDEX_op_andc_i32:
  1320. return TCG_TARGET_HAS_andc_i32;
  1321. case INDEX_op_orc_i32:
  1322. return TCG_TARGET_HAS_orc_i32;
  1323. case INDEX_op_eqv_i32:
  1324. return TCG_TARGET_HAS_eqv_i32;
  1325. case INDEX_op_nand_i32:
  1326. return TCG_TARGET_HAS_nand_i32;
  1327. case INDEX_op_nor_i32:
  1328. return TCG_TARGET_HAS_nor_i32;
  1329. case INDEX_op_clz_i32:
  1330. return TCG_TARGET_HAS_clz_i32;
  1331. case INDEX_op_ctz_i32:
  1332. return TCG_TARGET_HAS_ctz_i32;
  1333. case INDEX_op_ctpop_i32:
  1334. return TCG_TARGET_HAS_ctpop_i32;
  1335. case INDEX_op_brcond2_i32:
  1336. case INDEX_op_setcond2_i32:
  1337. return TCG_TARGET_REG_BITS == 32;
  1338. case INDEX_op_mov_i64:
  1339. case INDEX_op_movi_i64:
  1340. case INDEX_op_setcond_i64:
  1341. case INDEX_op_brcond_i64:
  1342. case INDEX_op_ld8u_i64:
  1343. case INDEX_op_ld8s_i64:
  1344. case INDEX_op_ld16u_i64:
  1345. case INDEX_op_ld16s_i64:
  1346. case INDEX_op_ld32u_i64:
  1347. case INDEX_op_ld32s_i64:
  1348. case INDEX_op_ld_i64:
  1349. case INDEX_op_st8_i64:
  1350. case INDEX_op_st16_i64:
  1351. case INDEX_op_st32_i64:
  1352. case INDEX_op_st_i64:
  1353. case INDEX_op_add_i64:
  1354. case INDEX_op_sub_i64:
  1355. case INDEX_op_mul_i64:
  1356. case INDEX_op_and_i64:
  1357. case INDEX_op_or_i64:
  1358. case INDEX_op_xor_i64:
  1359. case INDEX_op_shl_i64:
  1360. case INDEX_op_shr_i64:
  1361. case INDEX_op_sar_i64:
  1362. case INDEX_op_ext_i32_i64:
  1363. case INDEX_op_extu_i32_i64:
  1364. return TCG_TARGET_REG_BITS == 64;
  1365. case INDEX_op_movcond_i64:
  1366. return TCG_TARGET_HAS_movcond_i64;
  1367. case INDEX_op_div_i64:
  1368. case INDEX_op_divu_i64:
  1369. return TCG_TARGET_HAS_div_i64;
  1370. case INDEX_op_rem_i64:
  1371. case INDEX_op_remu_i64:
  1372. return TCG_TARGET_HAS_rem_i64;
  1373. case INDEX_op_div2_i64:
  1374. case INDEX_op_divu2_i64:
  1375. return TCG_TARGET_HAS_div2_i64;
  1376. case INDEX_op_rotl_i64:
  1377. case INDEX_op_rotr_i64:
  1378. return TCG_TARGET_HAS_rot_i64;
  1379. case INDEX_op_deposit_i64:
  1380. return TCG_TARGET_HAS_deposit_i64;
  1381. case INDEX_op_extract_i64:
  1382. return TCG_TARGET_HAS_extract_i64;
  1383. case INDEX_op_sextract_i64:
  1384. return TCG_TARGET_HAS_sextract_i64;
  1385. case INDEX_op_extract2_i64:
  1386. return TCG_TARGET_HAS_extract2_i64;
  1387. case INDEX_op_extrl_i64_i32:
  1388. return TCG_TARGET_HAS_extrl_i64_i32;
  1389. case INDEX_op_extrh_i64_i32:
  1390. return TCG_TARGET_HAS_extrh_i64_i32;
  1391. case INDEX_op_ext8s_i64:
  1392. return TCG_TARGET_HAS_ext8s_i64;
  1393. case INDEX_op_ext16s_i64:
  1394. return TCG_TARGET_HAS_ext16s_i64;
  1395. case INDEX_op_ext32s_i64:
  1396. return TCG_TARGET_HAS_ext32s_i64;
  1397. case INDEX_op_ext8u_i64:
  1398. return TCG_TARGET_HAS_ext8u_i64;
  1399. case INDEX_op_ext16u_i64:
  1400. return TCG_TARGET_HAS_ext16u_i64;
  1401. case INDEX_op_ext32u_i64:
  1402. return TCG_TARGET_HAS_ext32u_i64;
  1403. case INDEX_op_bswap16_i64:
  1404. return TCG_TARGET_HAS_bswap16_i64;
  1405. case INDEX_op_bswap32_i64:
  1406. return TCG_TARGET_HAS_bswap32_i64;
  1407. case INDEX_op_bswap64_i64:
  1408. return TCG_TARGET_HAS_bswap64_i64;
  1409. case INDEX_op_not_i64:
  1410. return TCG_TARGET_HAS_not_i64;
  1411. case INDEX_op_neg_i64:
  1412. return TCG_TARGET_HAS_neg_i64;
  1413. case INDEX_op_andc_i64:
  1414. return TCG_TARGET_HAS_andc_i64;
  1415. case INDEX_op_orc_i64:
  1416. return TCG_TARGET_HAS_orc_i64;
  1417. case INDEX_op_eqv_i64:
  1418. return TCG_TARGET_HAS_eqv_i64;
  1419. case INDEX_op_nand_i64:
  1420. return TCG_TARGET_HAS_nand_i64;
  1421. case INDEX_op_nor_i64:
  1422. return TCG_TARGET_HAS_nor_i64;
  1423. case INDEX_op_clz_i64:
  1424. return TCG_TARGET_HAS_clz_i64;
  1425. case INDEX_op_ctz_i64:
  1426. return TCG_TARGET_HAS_ctz_i64;
  1427. case INDEX_op_ctpop_i64:
  1428. return TCG_TARGET_HAS_ctpop_i64;
  1429. case INDEX_op_add2_i64:
  1430. return TCG_TARGET_HAS_add2_i64;
  1431. case INDEX_op_sub2_i64:
  1432. return TCG_TARGET_HAS_sub2_i64;
  1433. case INDEX_op_mulu2_i64:
  1434. return TCG_TARGET_HAS_mulu2_i64;
  1435. case INDEX_op_muls2_i64:
  1436. return TCG_TARGET_HAS_muls2_i64;
  1437. case INDEX_op_muluh_i64:
  1438. return TCG_TARGET_HAS_muluh_i64;
  1439. case INDEX_op_mulsh_i64:
  1440. return TCG_TARGET_HAS_mulsh_i64;
  1441. case INDEX_op_mov_vec:
  1442. case INDEX_op_dup_vec:
  1443. case INDEX_op_dupi_vec:
  1444. case INDEX_op_dupm_vec:
  1445. case INDEX_op_ld_vec:
  1446. case INDEX_op_st_vec:
  1447. case INDEX_op_add_vec:
  1448. case INDEX_op_sub_vec:
  1449. case INDEX_op_and_vec:
  1450. case INDEX_op_or_vec:
  1451. case INDEX_op_xor_vec:
  1452. case INDEX_op_cmp_vec:
  1453. return have_vec;
  1454. case INDEX_op_dup2_vec:
  1455. return have_vec && TCG_TARGET_REG_BITS == 32;
  1456. case INDEX_op_not_vec:
  1457. return have_vec && TCG_TARGET_HAS_not_vec;
  1458. case INDEX_op_neg_vec:
  1459. return have_vec && TCG_TARGET_HAS_neg_vec;
  1460. case INDEX_op_abs_vec:
  1461. return have_vec && TCG_TARGET_HAS_abs_vec;
  1462. case INDEX_op_andc_vec:
  1463. return have_vec && TCG_TARGET_HAS_andc_vec;
  1464. case INDEX_op_orc_vec:
  1465. return have_vec && TCG_TARGET_HAS_orc_vec;
  1466. case INDEX_op_mul_vec:
  1467. return have_vec && TCG_TARGET_HAS_mul_vec;
  1468. case INDEX_op_shli_vec:
  1469. case INDEX_op_shri_vec:
  1470. case INDEX_op_sari_vec:
  1471. return have_vec && TCG_TARGET_HAS_shi_vec;
  1472. case INDEX_op_shls_vec:
  1473. case INDEX_op_shrs_vec:
  1474. case INDEX_op_sars_vec:
  1475. return have_vec && TCG_TARGET_HAS_shs_vec;
  1476. case INDEX_op_shlv_vec:
  1477. case INDEX_op_shrv_vec:
  1478. case INDEX_op_sarv_vec:
  1479. return have_vec && TCG_TARGET_HAS_shv_vec;
  1480. case INDEX_op_rotli_vec:
  1481. return have_vec && TCG_TARGET_HAS_roti_vec;
  1482. case INDEX_op_rotls_vec:
  1483. return have_vec && TCG_TARGET_HAS_rots_vec;
  1484. case INDEX_op_rotlv_vec:
  1485. case INDEX_op_rotrv_vec:
  1486. return have_vec && TCG_TARGET_HAS_rotv_vec;
  1487. case INDEX_op_ssadd_vec:
  1488. case INDEX_op_usadd_vec:
  1489. case INDEX_op_sssub_vec:
  1490. case INDEX_op_ussub_vec:
  1491. return have_vec && TCG_TARGET_HAS_sat_vec;
  1492. case INDEX_op_smin_vec:
  1493. case INDEX_op_umin_vec:
  1494. case INDEX_op_smax_vec:
  1495. case INDEX_op_umax_vec:
  1496. return have_vec && TCG_TARGET_HAS_minmax_vec;
  1497. case INDEX_op_bitsel_vec:
  1498. return have_vec && TCG_TARGET_HAS_bitsel_vec;
  1499. case INDEX_op_cmpsel_vec:
  1500. return have_vec && TCG_TARGET_HAS_cmpsel_vec;
  1501. default:
  1502. tcg_debug_assert(op > INDEX_op_last_generic && op < NB_OPS);
  1503. return true;
  1504. }
  1505. }
  1506. /* Note: we convert the 64 bit args to 32 bit and do some alignment
  1507. and endian swap. Maybe it would be better to do the alignment
  1508. and endian swap in tcg_reg_alloc_call(). */
  1509. void tcg_gen_callN(void *func, TCGTemp *ret, int nargs, TCGTemp **args)
  1510. {
  1511. int i, real_args, nb_rets, pi;
  1512. unsigned sizemask, flags;
  1513. TCGHelperInfo *info;
  1514. TCGOp *op;
  1515. info = g_hash_table_lookup(helper_table, (gpointer)func);
  1516. flags = info->flags;
  1517. sizemask = info->sizemask;
  1518. #ifdef CONFIG_PLUGIN
  1519. /* detect non-plugin helpers */
  1520. if (tcg_ctx->plugin_insn && unlikely(strncmp(info->name, "plugin_", 7))) {
  1521. tcg_ctx->plugin_insn->calls_helpers = true;
  1522. }
  1523. #endif
  1524. #if defined(__sparc__) && !defined(__arch64__) \
  1525. && !defined(CONFIG_TCG_INTERPRETER)
  1526. /* We have 64-bit values in one register, but need to pass as two
  1527. separate parameters. Split them. */
  1528. int orig_sizemask = sizemask;
  1529. int orig_nargs = nargs;
  1530. TCGv_i64 retl, reth;
  1531. TCGTemp *split_args[MAX_OPC_PARAM];
  1532. retl = NULL;
  1533. reth = NULL;
  1534. if (sizemask != 0) {
  1535. for (i = real_args = 0; i < nargs; ++i) {
  1536. int is_64bit = sizemask & (1 << (i+1)*2);
  1537. if (is_64bit) {
  1538. TCGv_i64 orig = temp_tcgv_i64(args[i]);
  1539. TCGv_i32 h = tcg_temp_new_i32();
  1540. TCGv_i32 l = tcg_temp_new_i32();
  1541. tcg_gen_extr_i64_i32(l, h, orig);
  1542. split_args[real_args++] = tcgv_i32_temp(h);
  1543. split_args[real_args++] = tcgv_i32_temp(l);
  1544. } else {
  1545. split_args[real_args++] = args[i];
  1546. }
  1547. }
  1548. nargs = real_args;
  1549. args = split_args;
  1550. sizemask = 0;
  1551. }
  1552. #elif defined(TCG_TARGET_EXTEND_ARGS) && TCG_TARGET_REG_BITS == 64
  1553. for (i = 0; i < nargs; ++i) {
  1554. int is_64bit = sizemask & (1 << (i+1)*2);
  1555. int is_signed = sizemask & (2 << (i+1)*2);
  1556. if (!is_64bit) {
  1557. TCGv_i64 temp = tcg_temp_new_i64();
  1558. TCGv_i64 orig = temp_tcgv_i64(args[i]);
  1559. if (is_signed) {
  1560. tcg_gen_ext32s_i64(temp, orig);
  1561. } else {
  1562. tcg_gen_ext32u_i64(temp, orig);
  1563. }
  1564. args[i] = tcgv_i64_temp(temp);
  1565. }
  1566. }
  1567. #endif /* TCG_TARGET_EXTEND_ARGS */
  1568. op = tcg_emit_op(INDEX_op_call);
  1569. pi = 0;
  1570. if (ret != NULL) {
  1571. #if defined(__sparc__) && !defined(__arch64__) \
  1572. && !defined(CONFIG_TCG_INTERPRETER)
  1573. if (orig_sizemask & 1) {
  1574. /* The 32-bit ABI is going to return the 64-bit value in
  1575. the %o0/%o1 register pair. Prepare for this by using
  1576. two return temporaries, and reassemble below. */
  1577. retl = tcg_temp_new_i64();
  1578. reth = tcg_temp_new_i64();
  1579. op->args[pi++] = tcgv_i64_arg(reth);
  1580. op->args[pi++] = tcgv_i64_arg(retl);
  1581. nb_rets = 2;
  1582. } else {
  1583. op->args[pi++] = temp_arg(ret);
  1584. nb_rets = 1;
  1585. }
  1586. #else
  1587. if (TCG_TARGET_REG_BITS < 64 && (sizemask & 1)) {
  1588. #ifdef HOST_WORDS_BIGENDIAN
  1589. op->args[pi++] = temp_arg(ret + 1);
  1590. op->args[pi++] = temp_arg(ret);
  1591. #else
  1592. op->args[pi++] = temp_arg(ret);
  1593. op->args[pi++] = temp_arg(ret + 1);
  1594. #endif
  1595. nb_rets = 2;
  1596. } else {
  1597. op->args[pi++] = temp_arg(ret);
  1598. nb_rets = 1;
  1599. }
  1600. #endif
  1601. } else {
  1602. nb_rets = 0;
  1603. }
  1604. TCGOP_CALLO(op) = nb_rets;
  1605. real_args = 0;
  1606. for (i = 0; i < nargs; i++) {
  1607. int is_64bit = sizemask & (1 << (i+1)*2);
  1608. if (TCG_TARGET_REG_BITS < 64 && is_64bit) {
  1609. #ifdef TCG_TARGET_CALL_ALIGN_ARGS
  1610. /* some targets want aligned 64 bit args */
  1611. if (real_args & 1) {
  1612. op->args[pi++] = TCG_CALL_DUMMY_ARG;
  1613. real_args++;
  1614. }
  1615. #endif
  1616. /* If stack grows up, then we will be placing successive
  1617. arguments at lower addresses, which means we need to
  1618. reverse the order compared to how we would normally
  1619. treat either big or little-endian. For those arguments
  1620. that will wind up in registers, this still works for
  1621. HPPA (the only current STACK_GROWSUP target) since the
  1622. argument registers are *also* allocated in decreasing
  1623. order. If another such target is added, this logic may
  1624. have to get more complicated to differentiate between
  1625. stack arguments and register arguments. */
  1626. #if defined(HOST_WORDS_BIGENDIAN) != defined(TCG_TARGET_STACK_GROWSUP)
  1627. op->args[pi++] = temp_arg(args[i] + 1);
  1628. op->args[pi++] = temp_arg(args[i]);
  1629. #else
  1630. op->args[pi++] = temp_arg(args[i]);
  1631. op->args[pi++] = temp_arg(args[i] + 1);
  1632. #endif
  1633. real_args += 2;
  1634. continue;
  1635. }
  1636. op->args[pi++] = temp_arg(args[i]);
  1637. real_args++;
  1638. }
  1639. op->args[pi++] = (uintptr_t)func;
  1640. op->args[pi++] = flags;
  1641. TCGOP_CALLI(op) = real_args;
  1642. /* Make sure the fields didn't overflow. */
  1643. tcg_debug_assert(TCGOP_CALLI(op) == real_args);
  1644. tcg_debug_assert(pi <= ARRAY_SIZE(op->args));
  1645. #if defined(__sparc__) && !defined(__arch64__) \
  1646. && !defined(CONFIG_TCG_INTERPRETER)
  1647. /* Free all of the parts we allocated above. */
  1648. for (i = real_args = 0; i < orig_nargs; ++i) {
  1649. int is_64bit = orig_sizemask & (1 << (i+1)*2);
  1650. if (is_64bit) {
  1651. tcg_temp_free_internal(args[real_args++]);
  1652. tcg_temp_free_internal(args[real_args++]);
  1653. } else {
  1654. real_args++;
  1655. }
  1656. }
  1657. if (orig_sizemask & 1) {
  1658. /* The 32-bit ABI returned two 32-bit pieces. Re-assemble them.
  1659. Note that describing these as TCGv_i64 eliminates an unnecessary
  1660. zero-extension that tcg_gen_concat_i32_i64 would create. */
  1661. tcg_gen_concat32_i64(temp_tcgv_i64(ret), retl, reth);
  1662. tcg_temp_free_i64(retl);
  1663. tcg_temp_free_i64(reth);
  1664. }
  1665. #elif defined(TCG_TARGET_EXTEND_ARGS) && TCG_TARGET_REG_BITS == 64
  1666. for (i = 0; i < nargs; ++i) {
  1667. int is_64bit = sizemask & (1 << (i+1)*2);
  1668. if (!is_64bit) {
  1669. tcg_temp_free_internal(args[i]);
  1670. }
  1671. }
  1672. #endif /* TCG_TARGET_EXTEND_ARGS */
  1673. }
  1674. static void tcg_reg_alloc_start(TCGContext *s)
  1675. {
  1676. int i, n;
  1677. TCGTemp *ts;
  1678. for (i = 0, n = s->nb_globals; i < n; i++) {
  1679. ts = &s->temps[i];
  1680. ts->val_type = (ts->fixed_reg ? TEMP_VAL_REG : TEMP_VAL_MEM);
  1681. }
  1682. for (n = s->nb_temps; i < n; i++) {
  1683. ts = &s->temps[i];
  1684. ts->val_type = (ts->temp_local ? TEMP_VAL_MEM : TEMP_VAL_DEAD);
  1685. ts->mem_allocated = 0;
  1686. ts->fixed_reg = 0;
  1687. }
  1688. memset(s->reg_to_temp, 0, sizeof(s->reg_to_temp));
  1689. }
  1690. static char *tcg_get_arg_str_ptr(TCGContext *s, char *buf, int buf_size,
  1691. TCGTemp *ts)
  1692. {
  1693. int idx = temp_idx(ts);
  1694. if (ts->temp_global) {
  1695. pstrcpy(buf, buf_size, ts->name);
  1696. } else if (ts->temp_local) {
  1697. snprintf(buf, buf_size, "loc%d", idx - s->nb_globals);
  1698. } else {
  1699. snprintf(buf, buf_size, "tmp%d", idx - s->nb_globals);
  1700. }
  1701. return buf;
  1702. }
  1703. static char *tcg_get_arg_str(TCGContext *s, char *buf,
  1704. int buf_size, TCGArg arg)
  1705. {
  1706. return tcg_get_arg_str_ptr(s, buf, buf_size, arg_temp(arg));
  1707. }
  1708. /* Find helper name. */
  1709. static inline const char *tcg_find_helper(TCGContext *s, uintptr_t val)
  1710. {
  1711. const char *ret = NULL;
  1712. if (helper_table) {
  1713. TCGHelperInfo *info = g_hash_table_lookup(helper_table, (gpointer)val);
  1714. if (info) {
  1715. ret = info->name;
  1716. }
  1717. }
  1718. return ret;
  1719. }
  1720. static const char * const cond_name[] =
  1721. {
  1722. [TCG_COND_NEVER] = "never",
  1723. [TCG_COND_ALWAYS] = "always",
  1724. [TCG_COND_EQ] = "eq",
  1725. [TCG_COND_NE] = "ne",
  1726. [TCG_COND_LT] = "lt",
  1727. [TCG_COND_GE] = "ge",
  1728. [TCG_COND_LE] = "le",
  1729. [TCG_COND_GT] = "gt",
  1730. [TCG_COND_LTU] = "ltu",
  1731. [TCG_COND_GEU] = "geu",
  1732. [TCG_COND_LEU] = "leu",
  1733. [TCG_COND_GTU] = "gtu"
  1734. };
  1735. static const char * const ldst_name[] =
  1736. {
  1737. [MO_UB] = "ub",
  1738. [MO_SB] = "sb",
  1739. [MO_LEUW] = "leuw",
  1740. [MO_LESW] = "lesw",
  1741. [MO_LEUL] = "leul",
  1742. [MO_LESL] = "lesl",
  1743. [MO_LEQ] = "leq",
  1744. [MO_BEUW] = "beuw",
  1745. [MO_BESW] = "besw",
  1746. [MO_BEUL] = "beul",
  1747. [MO_BESL] = "besl",
  1748. [MO_BEQ] = "beq",
  1749. };
  1750. static const char * const alignment_name[(MO_AMASK >> MO_ASHIFT) + 1] = {
  1751. #ifdef TARGET_ALIGNED_ONLY
  1752. [MO_UNALN >> MO_ASHIFT] = "un+",
  1753. [MO_ALIGN >> MO_ASHIFT] = "",
  1754. #else
  1755. [MO_UNALN >> MO_ASHIFT] = "",
  1756. [MO_ALIGN >> MO_ASHIFT] = "al+",
  1757. #endif
  1758. [MO_ALIGN_2 >> MO_ASHIFT] = "al2+",
  1759. [MO_ALIGN_4 >> MO_ASHIFT] = "al4+",
  1760. [MO_ALIGN_8 >> MO_ASHIFT] = "al8+",
  1761. [MO_ALIGN_16 >> MO_ASHIFT] = "al16+",
  1762. [MO_ALIGN_32 >> MO_ASHIFT] = "al32+",
  1763. [MO_ALIGN_64 >> MO_ASHIFT] = "al64+",
  1764. };
  1765. static inline bool tcg_regset_single(TCGRegSet d)
  1766. {
  1767. return (d & (d - 1)) == 0;
  1768. }
  1769. static inline TCGReg tcg_regset_first(TCGRegSet d)
  1770. {
  1771. if (TCG_TARGET_NB_REGS <= 32) {
  1772. return ctz32(d);
  1773. } else {
  1774. return ctz64(d);
  1775. }
  1776. }
  1777. static void tcg_dump_ops(TCGContext *s, bool have_prefs)
  1778. {
  1779. char buf[128];
  1780. TCGOp *op;
  1781. QTAILQ_FOREACH(op, &s->ops, link) {
  1782. int i, k, nb_oargs, nb_iargs, nb_cargs;
  1783. const TCGOpDef *def;
  1784. TCGOpcode c;
  1785. int col = 0;
  1786. c = op->opc;
  1787. def = &tcg_op_defs[c];
  1788. if (c == INDEX_op_insn_start) {
  1789. nb_oargs = 0;
  1790. col += qemu_log("\n ----");
  1791. for (i = 0; i < TARGET_INSN_START_WORDS; ++i) {
  1792. target_ulong a;
  1793. #if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
  1794. a = deposit64(op->args[i * 2], 32, 32, op->args[i * 2 + 1]);
  1795. #else
  1796. a = op->args[i];
  1797. #endif
  1798. col += qemu_log(" " TARGET_FMT_lx, a);
  1799. }
  1800. } else if (c == INDEX_op_call) {
  1801. /* variable number of arguments */
  1802. nb_oargs = TCGOP_CALLO(op);
  1803. nb_iargs = TCGOP_CALLI(op);
  1804. nb_cargs = def->nb_cargs;
  1805. /* function name, flags, out args */
  1806. col += qemu_log(" %s %s,$0x%" TCG_PRIlx ",$%d", def->name,
  1807. tcg_find_helper(s, op->args[nb_oargs + nb_iargs]),
  1808. op->args[nb_oargs + nb_iargs + 1], nb_oargs);
  1809. for (i = 0; i < nb_oargs; i++) {
  1810. col += qemu_log(",%s", tcg_get_arg_str(s, buf, sizeof(buf),
  1811. op->args[i]));
  1812. }
  1813. for (i = 0; i < nb_iargs; i++) {
  1814. TCGArg arg = op->args[nb_oargs + i];
  1815. const char *t = "<dummy>";
  1816. if (arg != TCG_CALL_DUMMY_ARG) {
  1817. t = tcg_get_arg_str(s, buf, sizeof(buf), arg);
  1818. }
  1819. col += qemu_log(",%s", t);
  1820. }
  1821. } else {
  1822. col += qemu_log(" %s ", def->name);
  1823. nb_oargs = def->nb_oargs;
  1824. nb_iargs = def->nb_iargs;
  1825. nb_cargs = def->nb_cargs;
  1826. if (def->flags & TCG_OPF_VECTOR) {
  1827. col += qemu_log("v%d,e%d,", 64 << TCGOP_VECL(op),
  1828. 8 << TCGOP_VECE(op));
  1829. }
  1830. k = 0;
  1831. for (i = 0; i < nb_oargs; i++) {
  1832. if (k != 0) {
  1833. col += qemu_log(",");
  1834. }
  1835. col += qemu_log("%s", tcg_get_arg_str(s, buf, sizeof(buf),
  1836. op->args[k++]));
  1837. }
  1838. for (i = 0; i < nb_iargs; i++) {
  1839. if (k != 0) {
  1840. col += qemu_log(",");
  1841. }
  1842. col += qemu_log("%s", tcg_get_arg_str(s, buf, sizeof(buf),
  1843. op->args[k++]));
  1844. }
  1845. switch (c) {
  1846. case INDEX_op_brcond_i32:
  1847. case INDEX_op_setcond_i32:
  1848. case INDEX_op_movcond_i32:
  1849. case INDEX_op_brcond2_i32:
  1850. case INDEX_op_setcond2_i32:
  1851. case INDEX_op_brcond_i64:
  1852. case INDEX_op_setcond_i64:
  1853. case INDEX_op_movcond_i64:
  1854. case INDEX_op_cmp_vec:
  1855. case INDEX_op_cmpsel_vec:
  1856. if (op->args[k] < ARRAY_SIZE(cond_name)
  1857. && cond_name[op->args[k]]) {
  1858. col += qemu_log(",%s", cond_name[op->args[k++]]);
  1859. } else {
  1860. col += qemu_log(",$0x%" TCG_PRIlx, op->args[k++]);
  1861. }
  1862. i = 1;
  1863. break;
  1864. case INDEX_op_qemu_ld_i32:
  1865. case INDEX_op_qemu_st_i32:
  1866. case INDEX_op_qemu_ld_i64:
  1867. case INDEX_op_qemu_st_i64:
  1868. {
  1869. TCGMemOpIdx oi = op->args[k++];
  1870. MemOp op = get_memop(oi);
  1871. unsigned ix = get_mmuidx(oi);
  1872. if (op & ~(MO_AMASK | MO_BSWAP | MO_SSIZE)) {
  1873. col += qemu_log(",$0x%x,%u", op, ix);
  1874. } else {
  1875. const char *s_al, *s_op;
  1876. s_al = alignment_name[(op & MO_AMASK) >> MO_ASHIFT];
  1877. s_op = ldst_name[op & (MO_BSWAP | MO_SSIZE)];
  1878. col += qemu_log(",%s%s,%u", s_al, s_op, ix);
  1879. }
  1880. i = 1;
  1881. }
  1882. break;
  1883. default:
  1884. i = 0;
  1885. break;
  1886. }
  1887. switch (c) {
  1888. case INDEX_op_set_label:
  1889. case INDEX_op_br:
  1890. case INDEX_op_brcond_i32:
  1891. case INDEX_op_brcond_i64:
  1892. case INDEX_op_brcond2_i32:
  1893. col += qemu_log("%s$L%d", k ? "," : "",
  1894. arg_label(op->args[k])->id);
  1895. i++, k++;
  1896. break;
  1897. default:
  1898. break;
  1899. }
  1900. for (; i < nb_cargs; i++, k++) {
  1901. col += qemu_log("%s$0x%" TCG_PRIlx, k ? "," : "", op->args[k]);
  1902. }
  1903. }
  1904. if (have_prefs || op->life) {
  1905. QemuLogFile *logfile;
  1906. rcu_read_lock();
  1907. logfile = atomic_rcu_read(&qemu_logfile);
  1908. if (logfile) {
  1909. for (; col < 40; ++col) {
  1910. putc(' ', logfile->fd);
  1911. }
  1912. }
  1913. rcu_read_unlock();
  1914. }
  1915. if (op->life) {
  1916. unsigned life = op->life;
  1917. if (life & (SYNC_ARG * 3)) {
  1918. qemu_log(" sync:");
  1919. for (i = 0; i < 2; ++i) {
  1920. if (life & (SYNC_ARG << i)) {
  1921. qemu_log(" %d", i);
  1922. }
  1923. }
  1924. }
  1925. life /= DEAD_ARG;
  1926. if (life) {
  1927. qemu_log(" dead:");
  1928. for (i = 0; life; ++i, life >>= 1) {
  1929. if (life & 1) {
  1930. qemu_log(" %d", i);
  1931. }
  1932. }
  1933. }
  1934. }
  1935. if (have_prefs) {
  1936. for (i = 0; i < nb_oargs; ++i) {
  1937. TCGRegSet set = op->output_pref[i];
  1938. if (i == 0) {
  1939. qemu_log(" pref=");
  1940. } else {
  1941. qemu_log(",");
  1942. }
  1943. if (set == 0) {
  1944. qemu_log("none");
  1945. } else if (set == MAKE_64BIT_MASK(0, TCG_TARGET_NB_REGS)) {
  1946. qemu_log("all");
  1947. #ifdef CONFIG_DEBUG_TCG
  1948. } else if (tcg_regset_single(set)) {
  1949. TCGReg reg = tcg_regset_first(set);
  1950. qemu_log("%s", tcg_target_reg_names[reg]);
  1951. #endif
  1952. } else if (TCG_TARGET_NB_REGS <= 32) {
  1953. qemu_log("%#x", (uint32_t)set);
  1954. } else {
  1955. qemu_log("%#" PRIx64, (uint64_t)set);
  1956. }
  1957. }
  1958. }
  1959. qemu_log("\n");
  1960. }
  1961. }
  1962. /* we give more priority to constraints with less registers */
  1963. static int get_constraint_priority(const TCGOpDef *def, int k)
  1964. {
  1965. const TCGArgConstraint *arg_ct;
  1966. int i, n;
  1967. arg_ct = &def->args_ct[k];
  1968. if (arg_ct->ct & TCG_CT_ALIAS) {
  1969. /* an alias is equivalent to a single register */
  1970. n = 1;
  1971. } else {
  1972. if (!(arg_ct->ct & TCG_CT_REG))
  1973. return 0;
  1974. n = 0;
  1975. for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
  1976. if (tcg_regset_test_reg(arg_ct->u.regs, i))
  1977. n++;
  1978. }
  1979. }
  1980. return TCG_TARGET_NB_REGS - n + 1;
  1981. }
  1982. /* sort from highest priority to lowest */
  1983. static void sort_constraints(TCGOpDef *def, int start, int n)
  1984. {
  1985. int i, j, p1, p2, tmp;
  1986. for(i = 0; i < n; i++)
  1987. def->sorted_args[start + i] = start + i;
  1988. if (n <= 1)
  1989. return;
  1990. for(i = 0; i < n - 1; i++) {
  1991. for(j = i + 1; j < n; j++) {
  1992. p1 = get_constraint_priority(def, def->sorted_args[start + i]);
  1993. p2 = get_constraint_priority(def, def->sorted_args[start + j]);
  1994. if (p1 < p2) {
  1995. tmp = def->sorted_args[start + i];
  1996. def->sorted_args[start + i] = def->sorted_args[start + j];
  1997. def->sorted_args[start + j] = tmp;
  1998. }
  1999. }
  2000. }
  2001. }
  2002. static void process_op_defs(TCGContext *s)
  2003. {
  2004. TCGOpcode op;
  2005. for (op = 0; op < NB_OPS; op++) {
  2006. TCGOpDef *def = &tcg_op_defs[op];
  2007. const TCGTargetOpDef *tdefs;
  2008. TCGType type;
  2009. int i, nb_args;
  2010. if (def->flags & TCG_OPF_NOT_PRESENT) {
  2011. continue;
  2012. }
  2013. nb_args = def->nb_iargs + def->nb_oargs;
  2014. if (nb_args == 0) {
  2015. continue;
  2016. }
  2017. tdefs = tcg_target_op_def(op);
  2018. /* Missing TCGTargetOpDef entry. */
  2019. tcg_debug_assert(tdefs != NULL);
  2020. type = (def->flags & TCG_OPF_64BIT ? TCG_TYPE_I64 : TCG_TYPE_I32);
  2021. for (i = 0; i < nb_args; i++) {
  2022. const char *ct_str = tdefs->args_ct_str[i];
  2023. /* Incomplete TCGTargetOpDef entry. */
  2024. tcg_debug_assert(ct_str != NULL);
  2025. def->args_ct[i].u.regs = 0;
  2026. def->args_ct[i].ct = 0;
  2027. while (*ct_str != '\0') {
  2028. switch(*ct_str) {
  2029. case '0' ... '9':
  2030. {
  2031. int oarg = *ct_str - '0';
  2032. tcg_debug_assert(ct_str == tdefs->args_ct_str[i]);
  2033. tcg_debug_assert(oarg < def->nb_oargs);
  2034. tcg_debug_assert(def->args_ct[oarg].ct & TCG_CT_REG);
  2035. /* TCG_CT_ALIAS is for the output arguments.
  2036. The input is tagged with TCG_CT_IALIAS. */
  2037. def->args_ct[i] = def->args_ct[oarg];
  2038. def->args_ct[oarg].ct |= TCG_CT_ALIAS;
  2039. def->args_ct[oarg].alias_index = i;
  2040. def->args_ct[i].ct |= TCG_CT_IALIAS;
  2041. def->args_ct[i].alias_index = oarg;
  2042. }
  2043. ct_str++;
  2044. break;
  2045. case '&':
  2046. def->args_ct[i].ct |= TCG_CT_NEWREG;
  2047. ct_str++;
  2048. break;
  2049. case 'i':
  2050. def->args_ct[i].ct |= TCG_CT_CONST;
  2051. ct_str++;
  2052. break;
  2053. default:
  2054. ct_str = target_parse_constraint(&def->args_ct[i],
  2055. ct_str, type);
  2056. /* Typo in TCGTargetOpDef constraint. */
  2057. tcg_debug_assert(ct_str != NULL);
  2058. }
  2059. }
  2060. }
  2061. /* TCGTargetOpDef entry with too much information? */
  2062. tcg_debug_assert(i == TCG_MAX_OP_ARGS || tdefs->args_ct_str[i] == NULL);
  2063. /* sort the constraints (XXX: this is just an heuristic) */
  2064. sort_constraints(def, 0, def->nb_oargs);
  2065. sort_constraints(def, def->nb_oargs, def->nb_iargs);
  2066. }
  2067. }
  2068. void tcg_op_remove(TCGContext *s, TCGOp *op)
  2069. {
  2070. TCGLabel *label;
  2071. switch (op->opc) {
  2072. case INDEX_op_br:
  2073. label = arg_label(op->args[0]);
  2074. label->refs--;
  2075. break;
  2076. case INDEX_op_brcond_i32:
  2077. case INDEX_op_brcond_i64:
  2078. label = arg_label(op->args[3]);
  2079. label->refs--;
  2080. break;
  2081. case INDEX_op_brcond2_i32:
  2082. label = arg_label(op->args[5]);
  2083. label->refs--;
  2084. break;
  2085. default:
  2086. break;
  2087. }
  2088. QTAILQ_REMOVE(&s->ops, op, link);
  2089. QTAILQ_INSERT_TAIL(&s->free_ops, op, link);
  2090. s->nb_ops--;
  2091. #ifdef CONFIG_PROFILER
  2092. atomic_set(&s->prof.del_op_count, s->prof.del_op_count + 1);
  2093. #endif
  2094. }
  2095. static TCGOp *tcg_op_alloc(TCGOpcode opc)
  2096. {
  2097. TCGContext *s = tcg_ctx;
  2098. TCGOp *op;
  2099. if (likely(QTAILQ_EMPTY(&s->free_ops))) {
  2100. op = tcg_malloc(sizeof(TCGOp));
  2101. } else {
  2102. op = QTAILQ_FIRST(&s->free_ops);
  2103. QTAILQ_REMOVE(&s->free_ops, op, link);
  2104. }
  2105. memset(op, 0, offsetof(TCGOp, link));
  2106. op->opc = opc;
  2107. s->nb_ops++;
  2108. return op;
  2109. }
  2110. TCGOp *tcg_emit_op(TCGOpcode opc)
  2111. {
  2112. TCGOp *op = tcg_op_alloc(opc);
  2113. QTAILQ_INSERT_TAIL(&tcg_ctx->ops, op, link);
  2114. return op;
  2115. }
  2116. TCGOp *tcg_op_insert_before(TCGContext *s, TCGOp *old_op, TCGOpcode opc)
  2117. {
  2118. TCGOp *new_op = tcg_op_alloc(opc);
  2119. QTAILQ_INSERT_BEFORE(old_op, new_op, link);
  2120. return new_op;
  2121. }
  2122. TCGOp *tcg_op_insert_after(TCGContext *s, TCGOp *old_op, TCGOpcode opc)
  2123. {
  2124. TCGOp *new_op = tcg_op_alloc(opc);
  2125. QTAILQ_INSERT_AFTER(&s->ops, old_op, new_op, link);
  2126. return new_op;
  2127. }
  2128. /* Reachable analysis : remove unreachable code. */
  2129. static void reachable_code_pass(TCGContext *s)
  2130. {
  2131. TCGOp *op, *op_next;
  2132. bool dead = false;
  2133. QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) {
  2134. bool remove = dead;
  2135. TCGLabel *label;
  2136. int call_flags;
  2137. switch (op->opc) {
  2138. case INDEX_op_set_label:
  2139. label = arg_label(op->args[0]);
  2140. if (label->refs == 0) {
  2141. /*
  2142. * While there is an occasional backward branch, virtually
  2143. * all branches generated by the translators are forward.
  2144. * Which means that generally we will have already removed
  2145. * all references to the label that will be, and there is
  2146. * little to be gained by iterating.
  2147. */
  2148. remove = true;
  2149. } else {
  2150. /* Once we see a label, insns become live again. */
  2151. dead = false;
  2152. remove = false;
  2153. /*
  2154. * Optimization can fold conditional branches to unconditional.
  2155. * If we find a label with one reference which is preceded by
  2156. * an unconditional branch to it, remove both. This needed to
  2157. * wait until the dead code in between them was removed.
  2158. */
  2159. if (label->refs == 1) {
  2160. TCGOp *op_prev = QTAILQ_PREV(op, link);
  2161. if (op_prev->opc == INDEX_op_br &&
  2162. label == arg_label(op_prev->args[0])) {
  2163. tcg_op_remove(s, op_prev);
  2164. remove = true;
  2165. }
  2166. }
  2167. }
  2168. break;
  2169. case INDEX_op_br:
  2170. case INDEX_op_exit_tb:
  2171. case INDEX_op_goto_ptr:
  2172. /* Unconditional branches; everything following is dead. */
  2173. dead = true;
  2174. break;
  2175. case INDEX_op_call:
  2176. /* Notice noreturn helper calls, raising exceptions. */
  2177. call_flags = op->args[TCGOP_CALLO(op) + TCGOP_CALLI(op) + 1];
  2178. if (call_flags & TCG_CALL_NO_RETURN) {
  2179. dead = true;
  2180. }
  2181. break;
  2182. case INDEX_op_insn_start:
  2183. /* Never remove -- we need to keep these for unwind. */
  2184. remove = false;
  2185. break;
  2186. default:
  2187. break;
  2188. }
  2189. if (remove) {
  2190. tcg_op_remove(s, op);
  2191. }
  2192. }
  2193. }
  2194. #define TS_DEAD 1
  2195. #define TS_MEM 2
  2196. #define IS_DEAD_ARG(n) (arg_life & (DEAD_ARG << (n)))
  2197. #define NEED_SYNC_ARG(n) (arg_life & (SYNC_ARG << (n)))
  2198. /* For liveness_pass_1, the register preferences for a given temp. */
  2199. static inline TCGRegSet *la_temp_pref(TCGTemp *ts)
  2200. {
  2201. return ts->state_ptr;
  2202. }
  2203. /* For liveness_pass_1, reset the preferences for a given temp to the
  2204. * maximal regset for its type.
  2205. */
  2206. static inline void la_reset_pref(TCGTemp *ts)
  2207. {
  2208. *la_temp_pref(ts)
  2209. = (ts->state == TS_DEAD ? 0 : tcg_target_available_regs[ts->type]);
  2210. }
  2211. /* liveness analysis: end of function: all temps are dead, and globals
  2212. should be in memory. */
  2213. static void la_func_end(TCGContext *s, int ng, int nt)
  2214. {
  2215. int i;
  2216. for (i = 0; i < ng; ++i) {
  2217. s->temps[i].state = TS_DEAD | TS_MEM;
  2218. la_reset_pref(&s->temps[i]);
  2219. }
  2220. for (i = ng; i < nt; ++i) {
  2221. s->temps[i].state = TS_DEAD;
  2222. la_reset_pref(&s->temps[i]);
  2223. }
  2224. }
  2225. /* liveness analysis: end of basic block: all temps are dead, globals
  2226. and local temps should be in memory. */
  2227. static void la_bb_end(TCGContext *s, int ng, int nt)
  2228. {
  2229. int i;
  2230. for (i = 0; i < ng; ++i) {
  2231. s->temps[i].state = TS_DEAD | TS_MEM;
  2232. la_reset_pref(&s->temps[i]);
  2233. }
  2234. for (i = ng; i < nt; ++i) {
  2235. s->temps[i].state = (s->temps[i].temp_local
  2236. ? TS_DEAD | TS_MEM
  2237. : TS_DEAD);
  2238. la_reset_pref(&s->temps[i]);
  2239. }
  2240. }
  2241. /* liveness analysis: sync globals back to memory. */
  2242. static void la_global_sync(TCGContext *s, int ng)
  2243. {
  2244. int i;
  2245. for (i = 0; i < ng; ++i) {
  2246. int state = s->temps[i].state;
  2247. s->temps[i].state = state | TS_MEM;
  2248. if (state == TS_DEAD) {
  2249. /* If the global was previously dead, reset prefs. */
  2250. la_reset_pref(&s->temps[i]);
  2251. }
  2252. }
  2253. }
  2254. /* liveness analysis: sync globals back to memory and kill. */
  2255. static void la_global_kill(TCGContext *s, int ng)
  2256. {
  2257. int i;
  2258. for (i = 0; i < ng; i++) {
  2259. s->temps[i].state = TS_DEAD | TS_MEM;
  2260. la_reset_pref(&s->temps[i]);
  2261. }
  2262. }
  2263. /* liveness analysis: note live globals crossing calls. */
  2264. static void la_cross_call(TCGContext *s, int nt)
  2265. {
  2266. TCGRegSet mask = ~tcg_target_call_clobber_regs;
  2267. int i;
  2268. for (i = 0; i < nt; i++) {
  2269. TCGTemp *ts = &s->temps[i];
  2270. if (!(ts->state & TS_DEAD)) {
  2271. TCGRegSet *pset = la_temp_pref(ts);
  2272. TCGRegSet set = *pset;
  2273. set &= mask;
  2274. /* If the combination is not possible, restart. */
  2275. if (set == 0) {
  2276. set = tcg_target_available_regs[ts->type] & mask;
  2277. }
  2278. *pset = set;
  2279. }
  2280. }
  2281. }
  2282. /* Liveness analysis : update the opc_arg_life array to tell if a
  2283. given input arguments is dead. Instructions updating dead
  2284. temporaries are removed. */
  2285. static void liveness_pass_1(TCGContext *s)
  2286. {
  2287. int nb_globals = s->nb_globals;
  2288. int nb_temps = s->nb_temps;
  2289. TCGOp *op, *op_prev;
  2290. TCGRegSet *prefs;
  2291. int i;
  2292. prefs = tcg_malloc(sizeof(TCGRegSet) * nb_temps);
  2293. for (i = 0; i < nb_temps; ++i) {
  2294. s->temps[i].state_ptr = prefs + i;
  2295. }
  2296. /* ??? Should be redundant with the exit_tb that ends the TB. */
  2297. la_func_end(s, nb_globals, nb_temps);
  2298. QTAILQ_FOREACH_REVERSE_SAFE(op, &s->ops, link, op_prev) {
  2299. int nb_iargs, nb_oargs;
  2300. TCGOpcode opc_new, opc_new2;
  2301. bool have_opc_new2;
  2302. TCGLifeData arg_life = 0;
  2303. TCGTemp *ts;
  2304. TCGOpcode opc = op->opc;
  2305. const TCGOpDef *def = &tcg_op_defs[opc];
  2306. switch (opc) {
  2307. case INDEX_op_call:
  2308. {
  2309. int call_flags;
  2310. int nb_call_regs;
  2311. nb_oargs = TCGOP_CALLO(op);
  2312. nb_iargs = TCGOP_CALLI(op);
  2313. call_flags = op->args[nb_oargs + nb_iargs + 1];
  2314. /* pure functions can be removed if their result is unused */
  2315. if (call_flags & TCG_CALL_NO_SIDE_EFFECTS) {
  2316. for (i = 0; i < nb_oargs; i++) {
  2317. ts = arg_temp(op->args[i]);
  2318. if (ts->state != TS_DEAD) {
  2319. goto do_not_remove_call;
  2320. }
  2321. }
  2322. goto do_remove;
  2323. }
  2324. do_not_remove_call:
  2325. /* Output args are dead. */
  2326. for (i = 0; i < nb_oargs; i++) {
  2327. ts = arg_temp(op->args[i]);
  2328. if (ts->state & TS_DEAD) {
  2329. arg_life |= DEAD_ARG << i;
  2330. }
  2331. if (ts->state & TS_MEM) {
  2332. arg_life |= SYNC_ARG << i;
  2333. }
  2334. ts->state = TS_DEAD;
  2335. la_reset_pref(ts);
  2336. /* Not used -- it will be tcg_target_call_oarg_regs[i]. */
  2337. op->output_pref[i] = 0;
  2338. }
  2339. if (!(call_flags & (TCG_CALL_NO_WRITE_GLOBALS |
  2340. TCG_CALL_NO_READ_GLOBALS))) {
  2341. la_global_kill(s, nb_globals);
  2342. } else if (!(call_flags & TCG_CALL_NO_READ_GLOBALS)) {
  2343. la_global_sync(s, nb_globals);
  2344. }
  2345. /* Record arguments that die in this helper. */
  2346. for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
  2347. ts = arg_temp(op->args[i]);
  2348. if (ts && ts->state & TS_DEAD) {
  2349. arg_life |= DEAD_ARG << i;
  2350. }
  2351. }
  2352. /* For all live registers, remove call-clobbered prefs. */
  2353. la_cross_call(s, nb_temps);
  2354. nb_call_regs = ARRAY_SIZE(tcg_target_call_iarg_regs);
  2355. /* Input arguments are live for preceding opcodes. */
  2356. for (i = 0; i < nb_iargs; i++) {
  2357. ts = arg_temp(op->args[i + nb_oargs]);
  2358. if (ts && ts->state & TS_DEAD) {
  2359. /* For those arguments that die, and will be allocated
  2360. * in registers, clear the register set for that arg,
  2361. * to be filled in below. For args that will be on
  2362. * the stack, reset to any available reg.
  2363. */
  2364. *la_temp_pref(ts)
  2365. = (i < nb_call_regs ? 0 :
  2366. tcg_target_available_regs[ts->type]);
  2367. ts->state &= ~TS_DEAD;
  2368. }
  2369. }
  2370. /* For each input argument, add its input register to prefs.
  2371. If a temp is used once, this produces a single set bit. */
  2372. for (i = 0; i < MIN(nb_call_regs, nb_iargs); i++) {
  2373. ts = arg_temp(op->args[i + nb_oargs]);
  2374. if (ts) {
  2375. tcg_regset_set_reg(*la_temp_pref(ts),
  2376. tcg_target_call_iarg_regs[i]);
  2377. }
  2378. }
  2379. }
  2380. break;
  2381. case INDEX_op_insn_start:
  2382. break;
  2383. case INDEX_op_discard:
  2384. /* mark the temporary as dead */
  2385. ts = arg_temp(op->args[0]);
  2386. ts->state = TS_DEAD;
  2387. la_reset_pref(ts);
  2388. break;
  2389. case INDEX_op_add2_i32:
  2390. opc_new = INDEX_op_add_i32;
  2391. goto do_addsub2;
  2392. case INDEX_op_sub2_i32:
  2393. opc_new = INDEX_op_sub_i32;
  2394. goto do_addsub2;
  2395. case INDEX_op_add2_i64:
  2396. opc_new = INDEX_op_add_i64;
  2397. goto do_addsub2;
  2398. case INDEX_op_sub2_i64:
  2399. opc_new = INDEX_op_sub_i64;
  2400. do_addsub2:
  2401. nb_iargs = 4;
  2402. nb_oargs = 2;
  2403. /* Test if the high part of the operation is dead, but not
  2404. the low part. The result can be optimized to a simple
  2405. add or sub. This happens often for x86_64 guest when the
  2406. cpu mode is set to 32 bit. */
  2407. if (arg_temp(op->args[1])->state == TS_DEAD) {
  2408. if (arg_temp(op->args[0])->state == TS_DEAD) {
  2409. goto do_remove;
  2410. }
  2411. /* Replace the opcode and adjust the args in place,
  2412. leaving 3 unused args at the end. */
  2413. op->opc = opc = opc_new;
  2414. op->args[1] = op->args[2];
  2415. op->args[2] = op->args[4];
  2416. /* Fall through and mark the single-word operation live. */
  2417. nb_iargs = 2;
  2418. nb_oargs = 1;
  2419. }
  2420. goto do_not_remove;
  2421. case INDEX_op_mulu2_i32:
  2422. opc_new = INDEX_op_mul_i32;
  2423. opc_new2 = INDEX_op_muluh_i32;
  2424. have_opc_new2 = TCG_TARGET_HAS_muluh_i32;
  2425. goto do_mul2;
  2426. case INDEX_op_muls2_i32:
  2427. opc_new = INDEX_op_mul_i32;
  2428. opc_new2 = INDEX_op_mulsh_i32;
  2429. have_opc_new2 = TCG_TARGET_HAS_mulsh_i32;
  2430. goto do_mul2;
  2431. case INDEX_op_mulu2_i64:
  2432. opc_new = INDEX_op_mul_i64;
  2433. opc_new2 = INDEX_op_muluh_i64;
  2434. have_opc_new2 = TCG_TARGET_HAS_muluh_i64;
  2435. goto do_mul2;
  2436. case INDEX_op_muls2_i64:
  2437. opc_new = INDEX_op_mul_i64;
  2438. opc_new2 = INDEX_op_mulsh_i64;
  2439. have_opc_new2 = TCG_TARGET_HAS_mulsh_i64;
  2440. goto do_mul2;
  2441. do_mul2:
  2442. nb_iargs = 2;
  2443. nb_oargs = 2;
  2444. if (arg_temp(op->args[1])->state == TS_DEAD) {
  2445. if (arg_temp(op->args[0])->state == TS_DEAD) {
  2446. /* Both parts of the operation are dead. */
  2447. goto do_remove;
  2448. }
  2449. /* The high part of the operation is dead; generate the low. */
  2450. op->opc = opc = opc_new;
  2451. op->args[1] = op->args[2];
  2452. op->args[2] = op->args[3];
  2453. } else if (arg_temp(op->args[0])->state == TS_DEAD && have_opc_new2) {
  2454. /* The low part of the operation is dead; generate the high. */
  2455. op->opc = opc = opc_new2;
  2456. op->args[0] = op->args[1];
  2457. op->args[1] = op->args[2];
  2458. op->args[2] = op->args[3];
  2459. } else {
  2460. goto do_not_remove;
  2461. }
  2462. /* Mark the single-word operation live. */
  2463. nb_oargs = 1;
  2464. goto do_not_remove;
  2465. default:
  2466. /* XXX: optimize by hardcoding common cases (e.g. triadic ops) */
  2467. nb_iargs = def->nb_iargs;
  2468. nb_oargs = def->nb_oargs;
  2469. /* Test if the operation can be removed because all
  2470. its outputs are dead. We assume that nb_oargs == 0
  2471. implies side effects */
  2472. if (!(def->flags & TCG_OPF_SIDE_EFFECTS) && nb_oargs != 0) {
  2473. for (i = 0; i < nb_oargs; i++) {
  2474. if (arg_temp(op->args[i])->state != TS_DEAD) {
  2475. goto do_not_remove;
  2476. }
  2477. }
  2478. goto do_remove;
  2479. }
  2480. goto do_not_remove;
  2481. do_remove:
  2482. tcg_op_remove(s, op);
  2483. break;
  2484. do_not_remove:
  2485. for (i = 0; i < nb_oargs; i++) {
  2486. ts = arg_temp(op->args[i]);
  2487. /* Remember the preference of the uses that followed. */
  2488. op->output_pref[i] = *la_temp_pref(ts);
  2489. /* Output args are dead. */
  2490. if (ts->state & TS_DEAD) {
  2491. arg_life |= DEAD_ARG << i;
  2492. }
  2493. if (ts->state & TS_MEM) {
  2494. arg_life |= SYNC_ARG << i;
  2495. }
  2496. ts->state = TS_DEAD;
  2497. la_reset_pref(ts);
  2498. }
  2499. /* If end of basic block, update. */
  2500. if (def->flags & TCG_OPF_BB_EXIT) {
  2501. la_func_end(s, nb_globals, nb_temps);
  2502. } else if (def->flags & TCG_OPF_BB_END) {
  2503. la_bb_end(s, nb_globals, nb_temps);
  2504. } else if (def->flags & TCG_OPF_SIDE_EFFECTS) {
  2505. la_global_sync(s, nb_globals);
  2506. if (def->flags & TCG_OPF_CALL_CLOBBER) {
  2507. la_cross_call(s, nb_temps);
  2508. }
  2509. }
  2510. /* Record arguments that die in this opcode. */
  2511. for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
  2512. ts = arg_temp(op->args[i]);
  2513. if (ts->state & TS_DEAD) {
  2514. arg_life |= DEAD_ARG << i;
  2515. }
  2516. }
  2517. /* Input arguments are live for preceding opcodes. */
  2518. for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
  2519. ts = arg_temp(op->args[i]);
  2520. if (ts->state & TS_DEAD) {
  2521. /* For operands that were dead, initially allow
  2522. all regs for the type. */
  2523. *la_temp_pref(ts) = tcg_target_available_regs[ts->type];
  2524. ts->state &= ~TS_DEAD;
  2525. }
  2526. }
  2527. /* Incorporate constraints for this operand. */
  2528. switch (opc) {
  2529. case INDEX_op_mov_i32:
  2530. case INDEX_op_mov_i64:
  2531. /* Note that these are TCG_OPF_NOT_PRESENT and do not
  2532. have proper constraints. That said, special case
  2533. moves to propagate preferences backward. */
  2534. if (IS_DEAD_ARG(1)) {
  2535. *la_temp_pref(arg_temp(op->args[0]))
  2536. = *la_temp_pref(arg_temp(op->args[1]));
  2537. }
  2538. break;
  2539. default:
  2540. for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
  2541. const TCGArgConstraint *ct = &def->args_ct[i];
  2542. TCGRegSet set, *pset;
  2543. ts = arg_temp(op->args[i]);
  2544. pset = la_temp_pref(ts);
  2545. set = *pset;
  2546. set &= ct->u.regs;
  2547. if (ct->ct & TCG_CT_IALIAS) {
  2548. set &= op->output_pref[ct->alias_index];
  2549. }
  2550. /* If the combination is not possible, restart. */
  2551. if (set == 0) {
  2552. set = ct->u.regs;
  2553. }
  2554. *pset = set;
  2555. }
  2556. break;
  2557. }
  2558. break;
  2559. }
  2560. op->life = arg_life;
  2561. }
  2562. }
  2563. /* Liveness analysis: Convert indirect regs to direct temporaries. */
  2564. static bool liveness_pass_2(TCGContext *s)
  2565. {
  2566. int nb_globals = s->nb_globals;
  2567. int nb_temps, i;
  2568. bool changes = false;
  2569. TCGOp *op, *op_next;
  2570. /* Create a temporary for each indirect global. */
  2571. for (i = 0; i < nb_globals; ++i) {
  2572. TCGTemp *its = &s->temps[i];
  2573. if (its->indirect_reg) {
  2574. TCGTemp *dts = tcg_temp_alloc(s);
  2575. dts->type = its->type;
  2576. dts->base_type = its->base_type;
  2577. its->state_ptr = dts;
  2578. } else {
  2579. its->state_ptr = NULL;
  2580. }
  2581. /* All globals begin dead. */
  2582. its->state = TS_DEAD;
  2583. }
  2584. for (nb_temps = s->nb_temps; i < nb_temps; ++i) {
  2585. TCGTemp *its = &s->temps[i];
  2586. its->state_ptr = NULL;
  2587. its->state = TS_DEAD;
  2588. }
  2589. QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) {
  2590. TCGOpcode opc = op->opc;
  2591. const TCGOpDef *def = &tcg_op_defs[opc];
  2592. TCGLifeData arg_life = op->life;
  2593. int nb_iargs, nb_oargs, call_flags;
  2594. TCGTemp *arg_ts, *dir_ts;
  2595. if (opc == INDEX_op_call) {
  2596. nb_oargs = TCGOP_CALLO(op);
  2597. nb_iargs = TCGOP_CALLI(op);
  2598. call_flags = op->args[nb_oargs + nb_iargs + 1];
  2599. } else {
  2600. nb_iargs = def->nb_iargs;
  2601. nb_oargs = def->nb_oargs;
  2602. /* Set flags similar to how calls require. */
  2603. if (def->flags & TCG_OPF_BB_END) {
  2604. /* Like writing globals: save_globals */
  2605. call_flags = 0;
  2606. } else if (def->flags & TCG_OPF_SIDE_EFFECTS) {
  2607. /* Like reading globals: sync_globals */
  2608. call_flags = TCG_CALL_NO_WRITE_GLOBALS;
  2609. } else {
  2610. /* No effect on globals. */
  2611. call_flags = (TCG_CALL_NO_READ_GLOBALS |
  2612. TCG_CALL_NO_WRITE_GLOBALS);
  2613. }
  2614. }
  2615. /* Make sure that input arguments are available. */
  2616. for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
  2617. arg_ts = arg_temp(op->args[i]);
  2618. if (arg_ts) {
  2619. dir_ts = arg_ts->state_ptr;
  2620. if (dir_ts && arg_ts->state == TS_DEAD) {
  2621. TCGOpcode lopc = (arg_ts->type == TCG_TYPE_I32
  2622. ? INDEX_op_ld_i32
  2623. : INDEX_op_ld_i64);
  2624. TCGOp *lop = tcg_op_insert_before(s, op, lopc);
  2625. lop->args[0] = temp_arg(dir_ts);
  2626. lop->args[1] = temp_arg(arg_ts->mem_base);
  2627. lop->args[2] = arg_ts->mem_offset;
  2628. /* Loaded, but synced with memory. */
  2629. arg_ts->state = TS_MEM;
  2630. }
  2631. }
  2632. }
  2633. /* Perform input replacement, and mark inputs that became dead.
  2634. No action is required except keeping temp_state up to date
  2635. so that we reload when needed. */
  2636. for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
  2637. arg_ts = arg_temp(op->args[i]);
  2638. if (arg_ts) {
  2639. dir_ts = arg_ts->state_ptr;
  2640. if (dir_ts) {
  2641. op->args[i] = temp_arg(dir_ts);
  2642. changes = true;
  2643. if (IS_DEAD_ARG(i)) {
  2644. arg_ts->state = TS_DEAD;
  2645. }
  2646. }
  2647. }
  2648. }
  2649. /* Liveness analysis should ensure that the following are
  2650. all correct, for call sites and basic block end points. */
  2651. if (call_flags & TCG_CALL_NO_READ_GLOBALS) {
  2652. /* Nothing to do */
  2653. } else if (call_flags & TCG_CALL_NO_WRITE_GLOBALS) {
  2654. for (i = 0; i < nb_globals; ++i) {
  2655. /* Liveness should see that globals are synced back,
  2656. that is, either TS_DEAD or TS_MEM. */
  2657. arg_ts = &s->temps[i];
  2658. tcg_debug_assert(arg_ts->state_ptr == 0
  2659. || arg_ts->state != 0);
  2660. }
  2661. } else {
  2662. for (i = 0; i < nb_globals; ++i) {
  2663. /* Liveness should see that globals are saved back,
  2664. that is, TS_DEAD, waiting to be reloaded. */
  2665. arg_ts = &s->temps[i];
  2666. tcg_debug_assert(arg_ts->state_ptr == 0
  2667. || arg_ts->state == TS_DEAD);
  2668. }
  2669. }
  2670. /* Outputs become available. */
  2671. if (opc == INDEX_op_mov_i32 || opc == INDEX_op_mov_i64) {
  2672. arg_ts = arg_temp(op->args[0]);
  2673. dir_ts = arg_ts->state_ptr;
  2674. if (dir_ts) {
  2675. op->args[0] = temp_arg(dir_ts);
  2676. changes = true;
  2677. /* The output is now live and modified. */
  2678. arg_ts->state = 0;
  2679. if (NEED_SYNC_ARG(0)) {
  2680. TCGOpcode sopc = (arg_ts->type == TCG_TYPE_I32
  2681. ? INDEX_op_st_i32
  2682. : INDEX_op_st_i64);
  2683. TCGOp *sop = tcg_op_insert_after(s, op, sopc);
  2684. TCGTemp *out_ts = dir_ts;
  2685. if (IS_DEAD_ARG(0)) {
  2686. out_ts = arg_temp(op->args[1]);
  2687. arg_ts->state = TS_DEAD;
  2688. tcg_op_remove(s, op);
  2689. } else {
  2690. arg_ts->state = TS_MEM;
  2691. }
  2692. sop->args[0] = temp_arg(out_ts);
  2693. sop->args[1] = temp_arg(arg_ts->mem_base);
  2694. sop->args[2] = arg_ts->mem_offset;
  2695. } else {
  2696. tcg_debug_assert(!IS_DEAD_ARG(0));
  2697. }
  2698. }
  2699. } else {
  2700. for (i = 0; i < nb_oargs; i++) {
  2701. arg_ts = arg_temp(op->args[i]);
  2702. dir_ts = arg_ts->state_ptr;
  2703. if (!dir_ts) {
  2704. continue;
  2705. }
  2706. op->args[i] = temp_arg(dir_ts);
  2707. changes = true;
  2708. /* The output is now live and modified. */
  2709. arg_ts->state = 0;
  2710. /* Sync outputs upon their last write. */
  2711. if (NEED_SYNC_ARG(i)) {
  2712. TCGOpcode sopc = (arg_ts->type == TCG_TYPE_I32
  2713. ? INDEX_op_st_i32
  2714. : INDEX_op_st_i64);
  2715. TCGOp *sop = tcg_op_insert_after(s, op, sopc);
  2716. sop->args[0] = temp_arg(dir_ts);
  2717. sop->args[1] = temp_arg(arg_ts->mem_base);
  2718. sop->args[2] = arg_ts->mem_offset;
  2719. arg_ts->state = TS_MEM;
  2720. }
  2721. /* Drop outputs that are dead. */
  2722. if (IS_DEAD_ARG(i)) {
  2723. arg_ts->state = TS_DEAD;
  2724. }
  2725. }
  2726. }
  2727. }
  2728. return changes;
  2729. }
  2730. #ifdef CONFIG_DEBUG_TCG
  2731. static void dump_regs(TCGContext *s)
  2732. {
  2733. TCGTemp *ts;
  2734. int i;
  2735. char buf[64];
  2736. for(i = 0; i < s->nb_temps; i++) {
  2737. ts = &s->temps[i];
  2738. printf(" %10s: ", tcg_get_arg_str_ptr(s, buf, sizeof(buf), ts));
  2739. switch(ts->val_type) {
  2740. case TEMP_VAL_REG:
  2741. printf("%s", tcg_target_reg_names[ts->reg]);
  2742. break;
  2743. case TEMP_VAL_MEM:
  2744. printf("%d(%s)", (int)ts->mem_offset,
  2745. tcg_target_reg_names[ts->mem_base->reg]);
  2746. break;
  2747. case TEMP_VAL_CONST:
  2748. printf("$0x%" TCG_PRIlx, ts->val);
  2749. break;
  2750. case TEMP_VAL_DEAD:
  2751. printf("D");
  2752. break;
  2753. default:
  2754. printf("???");
  2755. break;
  2756. }
  2757. printf("\n");
  2758. }
  2759. for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
  2760. if (s->reg_to_temp[i] != NULL) {
  2761. printf("%s: %s\n",
  2762. tcg_target_reg_names[i],
  2763. tcg_get_arg_str_ptr(s, buf, sizeof(buf), s->reg_to_temp[i]));
  2764. }
  2765. }
  2766. }
  2767. static void check_regs(TCGContext *s)
  2768. {
  2769. int reg;
  2770. int k;
  2771. TCGTemp *ts;
  2772. char buf[64];
  2773. for (reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
  2774. ts = s->reg_to_temp[reg];
  2775. if (ts != NULL) {
  2776. if (ts->val_type != TEMP_VAL_REG || ts->reg != reg) {
  2777. printf("Inconsistency for register %s:\n",
  2778. tcg_target_reg_names[reg]);
  2779. goto fail;
  2780. }
  2781. }
  2782. }
  2783. for (k = 0; k < s->nb_temps; k++) {
  2784. ts = &s->temps[k];
  2785. if (ts->val_type == TEMP_VAL_REG && !ts->fixed_reg
  2786. && s->reg_to_temp[ts->reg] != ts) {
  2787. printf("Inconsistency for temp %s:\n",
  2788. tcg_get_arg_str_ptr(s, buf, sizeof(buf), ts));
  2789. fail:
  2790. printf("reg state:\n");
  2791. dump_regs(s);
  2792. tcg_abort();
  2793. }
  2794. }
  2795. }
  2796. #endif
  2797. static void temp_allocate_frame(TCGContext *s, TCGTemp *ts)
  2798. {
  2799. #if !(defined(__sparc__) && TCG_TARGET_REG_BITS == 64)
  2800. /* Sparc64 stack is accessed with offset of 2047 */
  2801. s->current_frame_offset = (s->current_frame_offset +
  2802. (tcg_target_long)sizeof(tcg_target_long) - 1) &
  2803. ~(sizeof(tcg_target_long) - 1);
  2804. #endif
  2805. if (s->current_frame_offset + (tcg_target_long)sizeof(tcg_target_long) >
  2806. s->frame_end) {
  2807. tcg_abort();
  2808. }
  2809. ts->mem_offset = s->current_frame_offset;
  2810. ts->mem_base = s->frame_temp;
  2811. ts->mem_allocated = 1;
  2812. s->current_frame_offset += sizeof(tcg_target_long);
  2813. }
  2814. static void temp_load(TCGContext *, TCGTemp *, TCGRegSet, TCGRegSet, TCGRegSet);
  2815. /* Mark a temporary as free or dead. If 'free_or_dead' is negative,
  2816. mark it free; otherwise mark it dead. */
  2817. static void temp_free_or_dead(TCGContext *s, TCGTemp *ts, int free_or_dead)
  2818. {
  2819. if (ts->fixed_reg) {
  2820. return;
  2821. }
  2822. if (ts->val_type == TEMP_VAL_REG) {
  2823. s->reg_to_temp[ts->reg] = NULL;
  2824. }
  2825. ts->val_type = (free_or_dead < 0
  2826. || ts->temp_local
  2827. || ts->temp_global
  2828. ? TEMP_VAL_MEM : TEMP_VAL_DEAD);
  2829. }
  2830. /* Mark a temporary as dead. */
  2831. static inline void temp_dead(TCGContext *s, TCGTemp *ts)
  2832. {
  2833. temp_free_or_dead(s, ts, 1);
  2834. }
  2835. /* Sync a temporary to memory. 'allocated_regs' is used in case a temporary
  2836. registers needs to be allocated to store a constant. If 'free_or_dead'
  2837. is non-zero, subsequently release the temporary; if it is positive, the
  2838. temp is dead; if it is negative, the temp is free. */
  2839. static void temp_sync(TCGContext *s, TCGTemp *ts, TCGRegSet allocated_regs,
  2840. TCGRegSet preferred_regs, int free_or_dead)
  2841. {
  2842. if (ts->fixed_reg) {
  2843. return;
  2844. }
  2845. if (!ts->mem_coherent) {
  2846. if (!ts->mem_allocated) {
  2847. temp_allocate_frame(s, ts);
  2848. }
  2849. switch (ts->val_type) {
  2850. case TEMP_VAL_CONST:
  2851. /* If we're going to free the temp immediately, then we won't
  2852. require it later in a register, so attempt to store the
  2853. constant to memory directly. */
  2854. if (free_or_dead
  2855. && tcg_out_sti(s, ts->type, ts->val,
  2856. ts->mem_base->reg, ts->mem_offset)) {
  2857. break;
  2858. }
  2859. temp_load(s, ts, tcg_target_available_regs[ts->type],
  2860. allocated_regs, preferred_regs);
  2861. /* fallthrough */
  2862. case TEMP_VAL_REG:
  2863. tcg_out_st(s, ts->type, ts->reg,
  2864. ts->mem_base->reg, ts->mem_offset);
  2865. break;
  2866. case TEMP_VAL_MEM:
  2867. break;
  2868. case TEMP_VAL_DEAD:
  2869. default:
  2870. tcg_abort();
  2871. }
  2872. ts->mem_coherent = 1;
  2873. }
  2874. if (free_or_dead) {
  2875. temp_free_or_dead(s, ts, free_or_dead);
  2876. }
  2877. }
  2878. /* free register 'reg' by spilling the corresponding temporary if necessary */
  2879. static void tcg_reg_free(TCGContext *s, TCGReg reg, TCGRegSet allocated_regs)
  2880. {
  2881. TCGTemp *ts = s->reg_to_temp[reg];
  2882. if (ts != NULL) {
  2883. temp_sync(s, ts, allocated_regs, 0, -1);
  2884. }
  2885. }
  2886. /**
  2887. * tcg_reg_alloc:
  2888. * @required_regs: Set of registers in which we must allocate.
  2889. * @allocated_regs: Set of registers which must be avoided.
  2890. * @preferred_regs: Set of registers we should prefer.
  2891. * @rev: True if we search the registers in "indirect" order.
  2892. *
  2893. * The allocated register must be in @required_regs & ~@allocated_regs,
  2894. * but if we can put it in @preferred_regs we may save a move later.
  2895. */
  2896. static TCGReg tcg_reg_alloc(TCGContext *s, TCGRegSet required_regs,
  2897. TCGRegSet allocated_regs,
  2898. TCGRegSet preferred_regs, bool rev)
  2899. {
  2900. int i, j, f, n = ARRAY_SIZE(tcg_target_reg_alloc_order);
  2901. TCGRegSet reg_ct[2];
  2902. const int *order;
  2903. reg_ct[1] = required_regs & ~allocated_regs;
  2904. tcg_debug_assert(reg_ct[1] != 0);
  2905. reg_ct[0] = reg_ct[1] & preferred_regs;
  2906. /* Skip the preferred_regs option if it cannot be satisfied,
  2907. or if the preference made no difference. */
  2908. f = reg_ct[0] == 0 || reg_ct[0] == reg_ct[1];
  2909. order = rev ? indirect_reg_alloc_order : tcg_target_reg_alloc_order;
  2910. /* Try free registers, preferences first. */
  2911. for (j = f; j < 2; j++) {
  2912. TCGRegSet set = reg_ct[j];
  2913. if (tcg_regset_single(set)) {
  2914. /* One register in the set. */
  2915. TCGReg reg = tcg_regset_first(set);
  2916. if (s->reg_to_temp[reg] == NULL) {
  2917. return reg;
  2918. }
  2919. } else {
  2920. for (i = 0; i < n; i++) {
  2921. TCGReg reg = order[i];
  2922. if (s->reg_to_temp[reg] == NULL &&
  2923. tcg_regset_test_reg(set, reg)) {
  2924. return reg;
  2925. }
  2926. }
  2927. }
  2928. }
  2929. /* We must spill something. */
  2930. for (j = f; j < 2; j++) {
  2931. TCGRegSet set = reg_ct[j];
  2932. if (tcg_regset_single(set)) {
  2933. /* One register in the set. */
  2934. TCGReg reg = tcg_regset_first(set);
  2935. tcg_reg_free(s, reg, allocated_regs);
  2936. return reg;
  2937. } else {
  2938. for (i = 0; i < n; i++) {
  2939. TCGReg reg = order[i];
  2940. if (tcg_regset_test_reg(set, reg)) {
  2941. tcg_reg_free(s, reg, allocated_regs);
  2942. return reg;
  2943. }
  2944. }
  2945. }
  2946. }
  2947. tcg_abort();
  2948. }
  2949. /* Make sure the temporary is in a register. If needed, allocate the register
  2950. from DESIRED while avoiding ALLOCATED. */
  2951. static void temp_load(TCGContext *s, TCGTemp *ts, TCGRegSet desired_regs,
  2952. TCGRegSet allocated_regs, TCGRegSet preferred_regs)
  2953. {
  2954. TCGReg reg;
  2955. switch (ts->val_type) {
  2956. case TEMP_VAL_REG:
  2957. return;
  2958. case TEMP_VAL_CONST:
  2959. reg = tcg_reg_alloc(s, desired_regs, allocated_regs,
  2960. preferred_regs, ts->indirect_base);
  2961. tcg_out_movi(s, ts->type, reg, ts->val);
  2962. ts->mem_coherent = 0;
  2963. break;
  2964. case TEMP_VAL_MEM:
  2965. reg = tcg_reg_alloc(s, desired_regs, allocated_regs,
  2966. preferred_regs, ts->indirect_base);
  2967. tcg_out_ld(s, ts->type, reg, ts->mem_base->reg, ts->mem_offset);
  2968. ts->mem_coherent = 1;
  2969. break;
  2970. case TEMP_VAL_DEAD:
  2971. default:
  2972. tcg_abort();
  2973. }
  2974. ts->reg = reg;
  2975. ts->val_type = TEMP_VAL_REG;
  2976. s->reg_to_temp[reg] = ts;
  2977. }
  2978. /* Save a temporary to memory. 'allocated_regs' is used in case a
  2979. temporary registers needs to be allocated to store a constant. */
  2980. static void temp_save(TCGContext *s, TCGTemp *ts, TCGRegSet allocated_regs)
  2981. {
  2982. /* The liveness analysis already ensures that globals are back
  2983. in memory. Keep an tcg_debug_assert for safety. */
  2984. tcg_debug_assert(ts->val_type == TEMP_VAL_MEM || ts->fixed_reg);
  2985. }
  2986. /* save globals to their canonical location and assume they can be
  2987. modified be the following code. 'allocated_regs' is used in case a
  2988. temporary registers needs to be allocated to store a constant. */
  2989. static void save_globals(TCGContext *s, TCGRegSet allocated_regs)
  2990. {
  2991. int i, n;
  2992. for (i = 0, n = s->nb_globals; i < n; i++) {
  2993. temp_save(s, &s->temps[i], allocated_regs);
  2994. }
  2995. }
  2996. /* sync globals to their canonical location and assume they can be
  2997. read by the following code. 'allocated_regs' is used in case a
  2998. temporary registers needs to be allocated to store a constant. */
  2999. static void sync_globals(TCGContext *s, TCGRegSet allocated_regs)
  3000. {
  3001. int i, n;
  3002. for (i = 0, n = s->nb_globals; i < n; i++) {
  3003. TCGTemp *ts = &s->temps[i];
  3004. tcg_debug_assert(ts->val_type != TEMP_VAL_REG
  3005. || ts->fixed_reg
  3006. || ts->mem_coherent);
  3007. }
  3008. }
  3009. /* at the end of a basic block, we assume all temporaries are dead and
  3010. all globals are stored at their canonical location. */
  3011. static void tcg_reg_alloc_bb_end(TCGContext *s, TCGRegSet allocated_regs)
  3012. {
  3013. int i;
  3014. for (i = s->nb_globals; i < s->nb_temps; i++) {
  3015. TCGTemp *ts = &s->temps[i];
  3016. if (ts->temp_local) {
  3017. temp_save(s, ts, allocated_regs);
  3018. } else {
  3019. /* The liveness analysis already ensures that temps are dead.
  3020. Keep an tcg_debug_assert for safety. */
  3021. tcg_debug_assert(ts->val_type == TEMP_VAL_DEAD);
  3022. }
  3023. }
  3024. save_globals(s, allocated_regs);
  3025. }
  3026. /*
  3027. * Specialized code generation for INDEX_op_movi_*.
  3028. */
  3029. static void tcg_reg_alloc_do_movi(TCGContext *s, TCGTemp *ots,
  3030. tcg_target_ulong val, TCGLifeData arg_life,
  3031. TCGRegSet preferred_regs)
  3032. {
  3033. /* ENV should not be modified. */
  3034. tcg_debug_assert(!ots->fixed_reg);
  3035. /* The movi is not explicitly generated here. */
  3036. if (ots->val_type == TEMP_VAL_REG) {
  3037. s->reg_to_temp[ots->reg] = NULL;
  3038. }
  3039. ots->val_type = TEMP_VAL_CONST;
  3040. ots->val = val;
  3041. ots->mem_coherent = 0;
  3042. if (NEED_SYNC_ARG(0)) {
  3043. temp_sync(s, ots, s->reserved_regs, preferred_regs, IS_DEAD_ARG(0));
  3044. } else if (IS_DEAD_ARG(0)) {
  3045. temp_dead(s, ots);
  3046. }
  3047. }
  3048. static void tcg_reg_alloc_movi(TCGContext *s, const TCGOp *op)
  3049. {
  3050. TCGTemp *ots = arg_temp(op->args[0]);
  3051. tcg_target_ulong val = op->args[1];
  3052. tcg_reg_alloc_do_movi(s, ots, val, op->life, op->output_pref[0]);
  3053. }
  3054. /*
  3055. * Specialized code generation for INDEX_op_mov_*.
  3056. */
  3057. static void tcg_reg_alloc_mov(TCGContext *s, const TCGOp *op)
  3058. {
  3059. const TCGLifeData arg_life = op->life;
  3060. TCGRegSet allocated_regs, preferred_regs;
  3061. TCGTemp *ts, *ots;
  3062. TCGType otype, itype;
  3063. allocated_regs = s->reserved_regs;
  3064. preferred_regs = op->output_pref[0];
  3065. ots = arg_temp(op->args[0]);
  3066. ts = arg_temp(op->args[1]);
  3067. /* ENV should not be modified. */
  3068. tcg_debug_assert(!ots->fixed_reg);
  3069. /* Note that otype != itype for no-op truncation. */
  3070. otype = ots->type;
  3071. itype = ts->type;
  3072. if (ts->val_type == TEMP_VAL_CONST) {
  3073. /* propagate constant or generate sti */
  3074. tcg_target_ulong val = ts->val;
  3075. if (IS_DEAD_ARG(1)) {
  3076. temp_dead(s, ts);
  3077. }
  3078. tcg_reg_alloc_do_movi(s, ots, val, arg_life, preferred_regs);
  3079. return;
  3080. }
  3081. /* If the source value is in memory we're going to be forced
  3082. to have it in a register in order to perform the copy. Copy
  3083. the SOURCE value into its own register first, that way we
  3084. don't have to reload SOURCE the next time it is used. */
  3085. if (ts->val_type == TEMP_VAL_MEM) {
  3086. temp_load(s, ts, tcg_target_available_regs[itype],
  3087. allocated_regs, preferred_regs);
  3088. }
  3089. tcg_debug_assert(ts->val_type == TEMP_VAL_REG);
  3090. if (IS_DEAD_ARG(0)) {
  3091. /* mov to a non-saved dead register makes no sense (even with
  3092. liveness analysis disabled). */
  3093. tcg_debug_assert(NEED_SYNC_ARG(0));
  3094. if (!ots->mem_allocated) {
  3095. temp_allocate_frame(s, ots);
  3096. }
  3097. tcg_out_st(s, otype, ts->reg, ots->mem_base->reg, ots->mem_offset);
  3098. if (IS_DEAD_ARG(1)) {
  3099. temp_dead(s, ts);
  3100. }
  3101. temp_dead(s, ots);
  3102. } else {
  3103. if (IS_DEAD_ARG(1) && !ts->fixed_reg) {
  3104. /* the mov can be suppressed */
  3105. if (ots->val_type == TEMP_VAL_REG) {
  3106. s->reg_to_temp[ots->reg] = NULL;
  3107. }
  3108. ots->reg = ts->reg;
  3109. temp_dead(s, ts);
  3110. } else {
  3111. if (ots->val_type != TEMP_VAL_REG) {
  3112. /* When allocating a new register, make sure to not spill the
  3113. input one. */
  3114. tcg_regset_set_reg(allocated_regs, ts->reg);
  3115. ots->reg = tcg_reg_alloc(s, tcg_target_available_regs[otype],
  3116. allocated_regs, preferred_regs,
  3117. ots->indirect_base);
  3118. }
  3119. if (!tcg_out_mov(s, otype, ots->reg, ts->reg)) {
  3120. /*
  3121. * Cross register class move not supported.
  3122. * Store the source register into the destination slot
  3123. * and leave the destination temp as TEMP_VAL_MEM.
  3124. */
  3125. assert(!ots->fixed_reg);
  3126. if (!ts->mem_allocated) {
  3127. temp_allocate_frame(s, ots);
  3128. }
  3129. tcg_out_st(s, ts->type, ts->reg,
  3130. ots->mem_base->reg, ots->mem_offset);
  3131. ots->mem_coherent = 1;
  3132. temp_free_or_dead(s, ots, -1);
  3133. return;
  3134. }
  3135. }
  3136. ots->val_type = TEMP_VAL_REG;
  3137. ots->mem_coherent = 0;
  3138. s->reg_to_temp[ots->reg] = ots;
  3139. if (NEED_SYNC_ARG(0)) {
  3140. temp_sync(s, ots, allocated_regs, 0, 0);
  3141. }
  3142. }
  3143. }
  3144. /*
  3145. * Specialized code generation for INDEX_op_dup_vec.
  3146. */
  3147. static void tcg_reg_alloc_dup(TCGContext *s, const TCGOp *op)
  3148. {
  3149. const TCGLifeData arg_life = op->life;
  3150. TCGRegSet dup_out_regs, dup_in_regs;
  3151. TCGTemp *its, *ots;
  3152. TCGType itype, vtype;
  3153. intptr_t endian_fixup;
  3154. unsigned vece;
  3155. bool ok;
  3156. ots = arg_temp(op->args[0]);
  3157. its = arg_temp(op->args[1]);
  3158. /* ENV should not be modified. */
  3159. tcg_debug_assert(!ots->fixed_reg);
  3160. itype = its->type;
  3161. vece = TCGOP_VECE(op);
  3162. vtype = TCGOP_VECL(op) + TCG_TYPE_V64;
  3163. if (its->val_type == TEMP_VAL_CONST) {
  3164. /* Propagate constant via movi -> dupi. */
  3165. tcg_target_ulong val = its->val;
  3166. if (IS_DEAD_ARG(1)) {
  3167. temp_dead(s, its);
  3168. }
  3169. tcg_reg_alloc_do_movi(s, ots, val, arg_life, op->output_pref[0]);
  3170. return;
  3171. }
  3172. dup_out_regs = tcg_op_defs[INDEX_op_dup_vec].args_ct[0].u.regs;
  3173. dup_in_regs = tcg_op_defs[INDEX_op_dup_vec].args_ct[1].u.regs;
  3174. /* Allocate the output register now. */
  3175. if (ots->val_type != TEMP_VAL_REG) {
  3176. TCGRegSet allocated_regs = s->reserved_regs;
  3177. if (!IS_DEAD_ARG(1) && its->val_type == TEMP_VAL_REG) {
  3178. /* Make sure to not spill the input register. */
  3179. tcg_regset_set_reg(allocated_regs, its->reg);
  3180. }
  3181. ots->reg = tcg_reg_alloc(s, dup_out_regs, allocated_regs,
  3182. op->output_pref[0], ots->indirect_base);
  3183. ots->val_type = TEMP_VAL_REG;
  3184. ots->mem_coherent = 0;
  3185. s->reg_to_temp[ots->reg] = ots;
  3186. }
  3187. switch (its->val_type) {
  3188. case TEMP_VAL_REG:
  3189. /*
  3190. * The dup constriaints must be broad, covering all possible VECE.
  3191. * However, tcg_op_dup_vec() gets to see the VECE and we allow it
  3192. * to fail, indicating that extra moves are required for that case.
  3193. */
  3194. if (tcg_regset_test_reg(dup_in_regs, its->reg)) {
  3195. if (tcg_out_dup_vec(s, vtype, vece, ots->reg, its->reg)) {
  3196. goto done;
  3197. }
  3198. /* Try again from memory or a vector input register. */
  3199. }
  3200. if (!its->mem_coherent) {
  3201. /*
  3202. * The input register is not synced, and so an extra store
  3203. * would be required to use memory. Attempt an integer-vector
  3204. * register move first. We do not have a TCGRegSet for this.
  3205. */
  3206. if (tcg_out_mov(s, itype, ots->reg, its->reg)) {
  3207. break;
  3208. }
  3209. /* Sync the temp back to its slot and load from there. */
  3210. temp_sync(s, its, s->reserved_regs, 0, 0);
  3211. }
  3212. /* fall through */
  3213. case TEMP_VAL_MEM:
  3214. #ifdef HOST_WORDS_BIGENDIAN
  3215. endian_fixup = itype == TCG_TYPE_I32 ? 4 : 8;
  3216. endian_fixup -= 1 << vece;
  3217. #else
  3218. endian_fixup = 0;
  3219. #endif
  3220. if (tcg_out_dupm_vec(s, vtype, vece, ots->reg, its->mem_base->reg,
  3221. its->mem_offset + endian_fixup)) {
  3222. goto done;
  3223. }
  3224. tcg_out_ld(s, itype, ots->reg, its->mem_base->reg, its->mem_offset);
  3225. break;
  3226. default:
  3227. g_assert_not_reached();
  3228. }
  3229. /* We now have a vector input register, so dup must succeed. */
  3230. ok = tcg_out_dup_vec(s, vtype, vece, ots->reg, ots->reg);
  3231. tcg_debug_assert(ok);
  3232. done:
  3233. if (IS_DEAD_ARG(1)) {
  3234. temp_dead(s, its);
  3235. }
  3236. if (NEED_SYNC_ARG(0)) {
  3237. temp_sync(s, ots, s->reserved_regs, 0, 0);
  3238. }
  3239. if (IS_DEAD_ARG(0)) {
  3240. temp_dead(s, ots);
  3241. }
  3242. }
  3243. static void tcg_reg_alloc_op(TCGContext *s, const TCGOp *op)
  3244. {
  3245. const TCGLifeData arg_life = op->life;
  3246. const TCGOpDef * const def = &tcg_op_defs[op->opc];
  3247. TCGRegSet i_allocated_regs;
  3248. TCGRegSet o_allocated_regs;
  3249. int i, k, nb_iargs, nb_oargs;
  3250. TCGReg reg;
  3251. TCGArg arg;
  3252. const TCGArgConstraint *arg_ct;
  3253. TCGTemp *ts;
  3254. TCGArg new_args[TCG_MAX_OP_ARGS];
  3255. int const_args[TCG_MAX_OP_ARGS];
  3256. nb_oargs = def->nb_oargs;
  3257. nb_iargs = def->nb_iargs;
  3258. /* copy constants */
  3259. memcpy(new_args + nb_oargs + nb_iargs,
  3260. op->args + nb_oargs + nb_iargs,
  3261. sizeof(TCGArg) * def->nb_cargs);
  3262. i_allocated_regs = s->reserved_regs;
  3263. o_allocated_regs = s->reserved_regs;
  3264. /* satisfy input constraints */
  3265. for (k = 0; k < nb_iargs; k++) {
  3266. TCGRegSet i_preferred_regs, o_preferred_regs;
  3267. i = def->sorted_args[nb_oargs + k];
  3268. arg = op->args[i];
  3269. arg_ct = &def->args_ct[i];
  3270. ts = arg_temp(arg);
  3271. if (ts->val_type == TEMP_VAL_CONST
  3272. && tcg_target_const_match(ts->val, ts->type, arg_ct)) {
  3273. /* constant is OK for instruction */
  3274. const_args[i] = 1;
  3275. new_args[i] = ts->val;
  3276. continue;
  3277. }
  3278. i_preferred_regs = o_preferred_regs = 0;
  3279. if (arg_ct->ct & TCG_CT_IALIAS) {
  3280. o_preferred_regs = op->output_pref[arg_ct->alias_index];
  3281. if (ts->fixed_reg) {
  3282. /* if fixed register, we must allocate a new register
  3283. if the alias is not the same register */
  3284. if (arg != op->args[arg_ct->alias_index]) {
  3285. goto allocate_in_reg;
  3286. }
  3287. } else {
  3288. /* if the input is aliased to an output and if it is
  3289. not dead after the instruction, we must allocate
  3290. a new register and move it */
  3291. if (!IS_DEAD_ARG(i)) {
  3292. goto allocate_in_reg;
  3293. }
  3294. /* check if the current register has already been allocated
  3295. for another input aliased to an output */
  3296. if (ts->val_type == TEMP_VAL_REG) {
  3297. int k2, i2;
  3298. reg = ts->reg;
  3299. for (k2 = 0 ; k2 < k ; k2++) {
  3300. i2 = def->sorted_args[nb_oargs + k2];
  3301. if ((def->args_ct[i2].ct & TCG_CT_IALIAS) &&
  3302. reg == new_args[i2]) {
  3303. goto allocate_in_reg;
  3304. }
  3305. }
  3306. }
  3307. i_preferred_regs = o_preferred_regs;
  3308. }
  3309. }
  3310. temp_load(s, ts, arg_ct->u.regs, i_allocated_regs, i_preferred_regs);
  3311. reg = ts->reg;
  3312. if (tcg_regset_test_reg(arg_ct->u.regs, reg)) {
  3313. /* nothing to do : the constraint is satisfied */
  3314. } else {
  3315. allocate_in_reg:
  3316. /* allocate a new register matching the constraint
  3317. and move the temporary register into it */
  3318. temp_load(s, ts, tcg_target_available_regs[ts->type],
  3319. i_allocated_regs, 0);
  3320. reg = tcg_reg_alloc(s, arg_ct->u.regs, i_allocated_regs,
  3321. o_preferred_regs, ts->indirect_base);
  3322. if (!tcg_out_mov(s, ts->type, reg, ts->reg)) {
  3323. /*
  3324. * Cross register class move not supported. Sync the
  3325. * temp back to its slot and load from there.
  3326. */
  3327. temp_sync(s, ts, i_allocated_regs, 0, 0);
  3328. tcg_out_ld(s, ts->type, reg,
  3329. ts->mem_base->reg, ts->mem_offset);
  3330. }
  3331. }
  3332. new_args[i] = reg;
  3333. const_args[i] = 0;
  3334. tcg_regset_set_reg(i_allocated_regs, reg);
  3335. }
  3336. /* mark dead temporaries and free the associated registers */
  3337. for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
  3338. if (IS_DEAD_ARG(i)) {
  3339. temp_dead(s, arg_temp(op->args[i]));
  3340. }
  3341. }
  3342. if (def->flags & TCG_OPF_BB_END) {
  3343. tcg_reg_alloc_bb_end(s, i_allocated_regs);
  3344. } else {
  3345. if (def->flags & TCG_OPF_CALL_CLOBBER) {
  3346. /* XXX: permit generic clobber register list ? */
  3347. for (i = 0; i < TCG_TARGET_NB_REGS; i++) {
  3348. if (tcg_regset_test_reg(tcg_target_call_clobber_regs, i)) {
  3349. tcg_reg_free(s, i, i_allocated_regs);
  3350. }
  3351. }
  3352. }
  3353. if (def->flags & TCG_OPF_SIDE_EFFECTS) {
  3354. /* sync globals if the op has side effects and might trigger
  3355. an exception. */
  3356. sync_globals(s, i_allocated_regs);
  3357. }
  3358. /* satisfy the output constraints */
  3359. for(k = 0; k < nb_oargs; k++) {
  3360. i = def->sorted_args[k];
  3361. arg = op->args[i];
  3362. arg_ct = &def->args_ct[i];
  3363. ts = arg_temp(arg);
  3364. /* ENV should not be modified. */
  3365. tcg_debug_assert(!ts->fixed_reg);
  3366. if ((arg_ct->ct & TCG_CT_ALIAS)
  3367. && !const_args[arg_ct->alias_index]) {
  3368. reg = new_args[arg_ct->alias_index];
  3369. } else if (arg_ct->ct & TCG_CT_NEWREG) {
  3370. reg = tcg_reg_alloc(s, arg_ct->u.regs,
  3371. i_allocated_regs | o_allocated_regs,
  3372. op->output_pref[k], ts->indirect_base);
  3373. } else {
  3374. reg = tcg_reg_alloc(s, arg_ct->u.regs, o_allocated_regs,
  3375. op->output_pref[k], ts->indirect_base);
  3376. }
  3377. tcg_regset_set_reg(o_allocated_regs, reg);
  3378. if (ts->val_type == TEMP_VAL_REG) {
  3379. s->reg_to_temp[ts->reg] = NULL;
  3380. }
  3381. ts->val_type = TEMP_VAL_REG;
  3382. ts->reg = reg;
  3383. /*
  3384. * Temp value is modified, so the value kept in memory is
  3385. * potentially not the same.
  3386. */
  3387. ts->mem_coherent = 0;
  3388. s->reg_to_temp[reg] = ts;
  3389. new_args[i] = reg;
  3390. }
  3391. }
  3392. /* emit instruction */
  3393. if (def->flags & TCG_OPF_VECTOR) {
  3394. tcg_out_vec_op(s, op->opc, TCGOP_VECL(op), TCGOP_VECE(op),
  3395. new_args, const_args);
  3396. } else {
  3397. tcg_out_op(s, op->opc, new_args, const_args);
  3398. }
  3399. /* move the outputs in the correct register if needed */
  3400. for(i = 0; i < nb_oargs; i++) {
  3401. ts = arg_temp(op->args[i]);
  3402. /* ENV should not be modified. */
  3403. tcg_debug_assert(!ts->fixed_reg);
  3404. if (NEED_SYNC_ARG(i)) {
  3405. temp_sync(s, ts, o_allocated_regs, 0, IS_DEAD_ARG(i));
  3406. } else if (IS_DEAD_ARG(i)) {
  3407. temp_dead(s, ts);
  3408. }
  3409. }
  3410. }
  3411. #ifdef TCG_TARGET_STACK_GROWSUP
  3412. #define STACK_DIR(x) (-(x))
  3413. #else
  3414. #define STACK_DIR(x) (x)
  3415. #endif
  3416. static void tcg_reg_alloc_call(TCGContext *s, TCGOp *op)
  3417. {
  3418. const int nb_oargs = TCGOP_CALLO(op);
  3419. const int nb_iargs = TCGOP_CALLI(op);
  3420. const TCGLifeData arg_life = op->life;
  3421. int flags, nb_regs, i;
  3422. TCGReg reg;
  3423. TCGArg arg;
  3424. TCGTemp *ts;
  3425. intptr_t stack_offset;
  3426. size_t call_stack_size;
  3427. tcg_insn_unit *func_addr;
  3428. int allocate_args;
  3429. TCGRegSet allocated_regs;
  3430. func_addr = (tcg_insn_unit *)(intptr_t)op->args[nb_oargs + nb_iargs];
  3431. flags = op->args[nb_oargs + nb_iargs + 1];
  3432. nb_regs = ARRAY_SIZE(tcg_target_call_iarg_regs);
  3433. if (nb_regs > nb_iargs) {
  3434. nb_regs = nb_iargs;
  3435. }
  3436. /* assign stack slots first */
  3437. call_stack_size = (nb_iargs - nb_regs) * sizeof(tcg_target_long);
  3438. call_stack_size = (call_stack_size + TCG_TARGET_STACK_ALIGN - 1) &
  3439. ~(TCG_TARGET_STACK_ALIGN - 1);
  3440. allocate_args = (call_stack_size > TCG_STATIC_CALL_ARGS_SIZE);
  3441. if (allocate_args) {
  3442. /* XXX: if more than TCG_STATIC_CALL_ARGS_SIZE is needed,
  3443. preallocate call stack */
  3444. tcg_abort();
  3445. }
  3446. stack_offset = TCG_TARGET_CALL_STACK_OFFSET;
  3447. for (i = nb_regs; i < nb_iargs; i++) {
  3448. arg = op->args[nb_oargs + i];
  3449. #ifdef TCG_TARGET_STACK_GROWSUP
  3450. stack_offset -= sizeof(tcg_target_long);
  3451. #endif
  3452. if (arg != TCG_CALL_DUMMY_ARG) {
  3453. ts = arg_temp(arg);
  3454. temp_load(s, ts, tcg_target_available_regs[ts->type],
  3455. s->reserved_regs, 0);
  3456. tcg_out_st(s, ts->type, ts->reg, TCG_REG_CALL_STACK, stack_offset);
  3457. }
  3458. #ifndef TCG_TARGET_STACK_GROWSUP
  3459. stack_offset += sizeof(tcg_target_long);
  3460. #endif
  3461. }
  3462. /* assign input registers */
  3463. allocated_regs = s->reserved_regs;
  3464. for (i = 0; i < nb_regs; i++) {
  3465. arg = op->args[nb_oargs + i];
  3466. if (arg != TCG_CALL_DUMMY_ARG) {
  3467. ts = arg_temp(arg);
  3468. reg = tcg_target_call_iarg_regs[i];
  3469. if (ts->val_type == TEMP_VAL_REG) {
  3470. if (ts->reg != reg) {
  3471. tcg_reg_free(s, reg, allocated_regs);
  3472. if (!tcg_out_mov(s, ts->type, reg, ts->reg)) {
  3473. /*
  3474. * Cross register class move not supported. Sync the
  3475. * temp back to its slot and load from there.
  3476. */
  3477. temp_sync(s, ts, allocated_regs, 0, 0);
  3478. tcg_out_ld(s, ts->type, reg,
  3479. ts->mem_base->reg, ts->mem_offset);
  3480. }
  3481. }
  3482. } else {
  3483. TCGRegSet arg_set = 0;
  3484. tcg_reg_free(s, reg, allocated_regs);
  3485. tcg_regset_set_reg(arg_set, reg);
  3486. temp_load(s, ts, arg_set, allocated_regs, 0);
  3487. }
  3488. tcg_regset_set_reg(allocated_regs, reg);
  3489. }
  3490. }
  3491. /* mark dead temporaries and free the associated registers */
  3492. for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
  3493. if (IS_DEAD_ARG(i)) {
  3494. temp_dead(s, arg_temp(op->args[i]));
  3495. }
  3496. }
  3497. /* clobber call registers */
  3498. for (i = 0; i < TCG_TARGET_NB_REGS; i++) {
  3499. if (tcg_regset_test_reg(tcg_target_call_clobber_regs, i)) {
  3500. tcg_reg_free(s, i, allocated_regs);
  3501. }
  3502. }
  3503. /* Save globals if they might be written by the helper, sync them if
  3504. they might be read. */
  3505. if (flags & TCG_CALL_NO_READ_GLOBALS) {
  3506. /* Nothing to do */
  3507. } else if (flags & TCG_CALL_NO_WRITE_GLOBALS) {
  3508. sync_globals(s, allocated_regs);
  3509. } else {
  3510. save_globals(s, allocated_regs);
  3511. }
  3512. tcg_out_call(s, func_addr);
  3513. /* assign output registers and emit moves if needed */
  3514. for(i = 0; i < nb_oargs; i++) {
  3515. arg = op->args[i];
  3516. ts = arg_temp(arg);
  3517. /* ENV should not be modified. */
  3518. tcg_debug_assert(!ts->fixed_reg);
  3519. reg = tcg_target_call_oarg_regs[i];
  3520. tcg_debug_assert(s->reg_to_temp[reg] == NULL);
  3521. if (ts->val_type == TEMP_VAL_REG) {
  3522. s->reg_to_temp[ts->reg] = NULL;
  3523. }
  3524. ts->val_type = TEMP_VAL_REG;
  3525. ts->reg = reg;
  3526. ts->mem_coherent = 0;
  3527. s->reg_to_temp[reg] = ts;
  3528. if (NEED_SYNC_ARG(i)) {
  3529. temp_sync(s, ts, allocated_regs, 0, IS_DEAD_ARG(i));
  3530. } else if (IS_DEAD_ARG(i)) {
  3531. temp_dead(s, ts);
  3532. }
  3533. }
  3534. }
  3535. #ifdef CONFIG_PROFILER
  3536. /* avoid copy/paste errors */
  3537. #define PROF_ADD(to, from, field) \
  3538. do { \
  3539. (to)->field += atomic_read(&((from)->field)); \
  3540. } while (0)
  3541. #define PROF_MAX(to, from, field) \
  3542. do { \
  3543. typeof((from)->field) val__ = atomic_read(&((from)->field)); \
  3544. if (val__ > (to)->field) { \
  3545. (to)->field = val__; \
  3546. } \
  3547. } while (0)
  3548. /* Pass in a zero'ed @prof */
  3549. static inline
  3550. void tcg_profile_snapshot(TCGProfile *prof, bool counters, bool table)
  3551. {
  3552. unsigned int n_ctxs = atomic_read(&n_tcg_ctxs);
  3553. unsigned int i;
  3554. for (i = 0; i < n_ctxs; i++) {
  3555. TCGContext *s = atomic_read(&tcg_ctxs[i]);
  3556. const TCGProfile *orig = &s->prof;
  3557. if (counters) {
  3558. PROF_ADD(prof, orig, cpu_exec_time);
  3559. PROF_ADD(prof, orig, tb_count1);
  3560. PROF_ADD(prof, orig, tb_count);
  3561. PROF_ADD(prof, orig, op_count);
  3562. PROF_MAX(prof, orig, op_count_max);
  3563. PROF_ADD(prof, orig, temp_count);
  3564. PROF_MAX(prof, orig, temp_count_max);
  3565. PROF_ADD(prof, orig, del_op_count);
  3566. PROF_ADD(prof, orig, code_in_len);
  3567. PROF_ADD(prof, orig, code_out_len);
  3568. PROF_ADD(prof, orig, search_out_len);
  3569. PROF_ADD(prof, orig, interm_time);
  3570. PROF_ADD(prof, orig, code_time);
  3571. PROF_ADD(prof, orig, la_time);
  3572. PROF_ADD(prof, orig, opt_time);
  3573. PROF_ADD(prof, orig, restore_count);
  3574. PROF_ADD(prof, orig, restore_time);
  3575. }
  3576. if (table) {
  3577. int i;
  3578. for (i = 0; i < NB_OPS; i++) {
  3579. PROF_ADD(prof, orig, table_op_count[i]);
  3580. }
  3581. }
  3582. }
  3583. }
  3584. #undef PROF_ADD
  3585. #undef PROF_MAX
  3586. static void tcg_profile_snapshot_counters(TCGProfile *prof)
  3587. {
  3588. tcg_profile_snapshot(prof, true, false);
  3589. }
  3590. static void tcg_profile_snapshot_table(TCGProfile *prof)
  3591. {
  3592. tcg_profile_snapshot(prof, false, true);
  3593. }
  3594. void tcg_dump_op_count(void)
  3595. {
  3596. TCGProfile prof = {};
  3597. int i;
  3598. tcg_profile_snapshot_table(&prof);
  3599. for (i = 0; i < NB_OPS; i++) {
  3600. qemu_printf("%s %" PRId64 "\n", tcg_op_defs[i].name,
  3601. prof.table_op_count[i]);
  3602. }
  3603. }
  3604. int64_t tcg_cpu_exec_time(void)
  3605. {
  3606. unsigned int n_ctxs = atomic_read(&n_tcg_ctxs);
  3607. unsigned int i;
  3608. int64_t ret = 0;
  3609. for (i = 0; i < n_ctxs; i++) {
  3610. const TCGContext *s = atomic_read(&tcg_ctxs[i]);
  3611. const TCGProfile *prof = &s->prof;
  3612. ret += atomic_read(&prof->cpu_exec_time);
  3613. }
  3614. return ret;
  3615. }
  3616. #else
  3617. void tcg_dump_op_count(void)
  3618. {
  3619. qemu_printf("[TCG profiler not compiled]\n");
  3620. }
  3621. int64_t tcg_cpu_exec_time(void)
  3622. {
  3623. error_report("%s: TCG profiler not compiled", __func__);
  3624. exit(EXIT_FAILURE);
  3625. }
  3626. #endif
  3627. int tcg_gen_code(TCGContext *s, TranslationBlock *tb)
  3628. {
  3629. #ifdef CONFIG_PROFILER
  3630. TCGProfile *prof = &s->prof;
  3631. #endif
  3632. int i, num_insns;
  3633. TCGOp *op;
  3634. #ifdef CONFIG_PROFILER
  3635. {
  3636. int n = 0;
  3637. QTAILQ_FOREACH(op, &s->ops, link) {
  3638. n++;
  3639. }
  3640. atomic_set(&prof->op_count, prof->op_count + n);
  3641. if (n > prof->op_count_max) {
  3642. atomic_set(&prof->op_count_max, n);
  3643. }
  3644. n = s->nb_temps;
  3645. atomic_set(&prof->temp_count, prof->temp_count + n);
  3646. if (n > prof->temp_count_max) {
  3647. atomic_set(&prof->temp_count_max, n);
  3648. }
  3649. }
  3650. #endif
  3651. #ifdef DEBUG_DISAS
  3652. if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP)
  3653. && qemu_log_in_addr_range(tb->pc))) {
  3654. FILE *logfile = qemu_log_lock();
  3655. qemu_log("OP:\n");
  3656. tcg_dump_ops(s, false);
  3657. qemu_log("\n");
  3658. qemu_log_unlock(logfile);
  3659. }
  3660. #endif
  3661. #ifdef CONFIG_DEBUG_TCG
  3662. /* Ensure all labels referenced have been emitted. */
  3663. {
  3664. TCGLabel *l;
  3665. bool error = false;
  3666. QSIMPLEQ_FOREACH(l, &s->labels, next) {
  3667. if (unlikely(!l->present) && l->refs) {
  3668. qemu_log_mask(CPU_LOG_TB_OP,
  3669. "$L%d referenced but not present.\n", l->id);
  3670. error = true;
  3671. }
  3672. }
  3673. assert(!error);
  3674. }
  3675. #endif
  3676. #ifdef CONFIG_PROFILER
  3677. atomic_set(&prof->opt_time, prof->opt_time - profile_getclock());
  3678. #endif
  3679. #ifdef USE_TCG_OPTIMIZATIONS
  3680. tcg_optimize(s);
  3681. #endif
  3682. #ifdef CONFIG_PROFILER
  3683. atomic_set(&prof->opt_time, prof->opt_time + profile_getclock());
  3684. atomic_set(&prof->la_time, prof->la_time - profile_getclock());
  3685. #endif
  3686. reachable_code_pass(s);
  3687. liveness_pass_1(s);
  3688. if (s->nb_indirects > 0) {
  3689. #ifdef DEBUG_DISAS
  3690. if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_IND)
  3691. && qemu_log_in_addr_range(tb->pc))) {
  3692. FILE *logfile = qemu_log_lock();
  3693. qemu_log("OP before indirect lowering:\n");
  3694. tcg_dump_ops(s, false);
  3695. qemu_log("\n");
  3696. qemu_log_unlock(logfile);
  3697. }
  3698. #endif
  3699. /* Replace indirect temps with direct temps. */
  3700. if (liveness_pass_2(s)) {
  3701. /* If changes were made, re-run liveness. */
  3702. liveness_pass_1(s);
  3703. }
  3704. }
  3705. #ifdef CONFIG_PROFILER
  3706. atomic_set(&prof->la_time, prof->la_time + profile_getclock());
  3707. #endif
  3708. #ifdef DEBUG_DISAS
  3709. if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_OPT)
  3710. && qemu_log_in_addr_range(tb->pc))) {
  3711. FILE *logfile = qemu_log_lock();
  3712. qemu_log("OP after optimization and liveness analysis:\n");
  3713. tcg_dump_ops(s, true);
  3714. qemu_log("\n");
  3715. qemu_log_unlock(logfile);
  3716. }
  3717. #endif
  3718. tcg_reg_alloc_start(s);
  3719. s->code_buf = tb->tc.ptr;
  3720. s->code_ptr = tb->tc.ptr;
  3721. #ifdef TCG_TARGET_NEED_LDST_LABELS
  3722. QSIMPLEQ_INIT(&s->ldst_labels);
  3723. #endif
  3724. #ifdef TCG_TARGET_NEED_POOL_LABELS
  3725. s->pool_labels = NULL;
  3726. #endif
  3727. num_insns = -1;
  3728. QTAILQ_FOREACH(op, &s->ops, link) {
  3729. TCGOpcode opc = op->opc;
  3730. #ifdef CONFIG_PROFILER
  3731. atomic_set(&prof->table_op_count[opc], prof->table_op_count[opc] + 1);
  3732. #endif
  3733. switch (opc) {
  3734. case INDEX_op_mov_i32:
  3735. case INDEX_op_mov_i64:
  3736. case INDEX_op_mov_vec:
  3737. tcg_reg_alloc_mov(s, op);
  3738. break;
  3739. case INDEX_op_movi_i32:
  3740. case INDEX_op_movi_i64:
  3741. case INDEX_op_dupi_vec:
  3742. tcg_reg_alloc_movi(s, op);
  3743. break;
  3744. case INDEX_op_dup_vec:
  3745. tcg_reg_alloc_dup(s, op);
  3746. break;
  3747. case INDEX_op_insn_start:
  3748. if (num_insns >= 0) {
  3749. size_t off = tcg_current_code_size(s);
  3750. s->gen_insn_end_off[num_insns] = off;
  3751. /* Assert that we do not overflow our stored offset. */
  3752. assert(s->gen_insn_end_off[num_insns] == off);
  3753. }
  3754. num_insns++;
  3755. for (i = 0; i < TARGET_INSN_START_WORDS; ++i) {
  3756. target_ulong a;
  3757. #if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
  3758. a = deposit64(op->args[i * 2], 32, 32, op->args[i * 2 + 1]);
  3759. #else
  3760. a = op->args[i];
  3761. #endif
  3762. s->gen_insn_data[num_insns][i] = a;
  3763. }
  3764. break;
  3765. case INDEX_op_discard:
  3766. temp_dead(s, arg_temp(op->args[0]));
  3767. break;
  3768. case INDEX_op_set_label:
  3769. tcg_reg_alloc_bb_end(s, s->reserved_regs);
  3770. tcg_out_label(s, arg_label(op->args[0]), s->code_ptr);
  3771. break;
  3772. case INDEX_op_call:
  3773. tcg_reg_alloc_call(s, op);
  3774. break;
  3775. default:
  3776. /* Sanity check that we've not introduced any unhandled opcodes. */
  3777. tcg_debug_assert(tcg_op_supported(opc));
  3778. /* Note: in order to speed up the code, it would be much
  3779. faster to have specialized register allocator functions for
  3780. some common argument patterns */
  3781. tcg_reg_alloc_op(s, op);
  3782. break;
  3783. }
  3784. #ifdef CONFIG_DEBUG_TCG
  3785. check_regs(s);
  3786. #endif
  3787. /* Test for (pending) buffer overflow. The assumption is that any
  3788. one operation beginning below the high water mark cannot overrun
  3789. the buffer completely. Thus we can test for overflow after
  3790. generating code without having to check during generation. */
  3791. if (unlikely((void *)s->code_ptr > s->code_gen_highwater)) {
  3792. return -1;
  3793. }
  3794. /* Test for TB overflow, as seen by gen_insn_end_off. */
  3795. if (unlikely(tcg_current_code_size(s) > UINT16_MAX)) {
  3796. return -2;
  3797. }
  3798. }
  3799. tcg_debug_assert(num_insns >= 0);
  3800. s->gen_insn_end_off[num_insns] = tcg_current_code_size(s);
  3801. /* Generate TB finalization at the end of block */
  3802. #ifdef TCG_TARGET_NEED_LDST_LABELS
  3803. i = tcg_out_ldst_finalize(s);
  3804. if (i < 0) {
  3805. return i;
  3806. }
  3807. #endif
  3808. #ifdef TCG_TARGET_NEED_POOL_LABELS
  3809. i = tcg_out_pool_finalize(s);
  3810. if (i < 0) {
  3811. return i;
  3812. }
  3813. #endif
  3814. if (!tcg_resolve_relocs(s)) {
  3815. return -2;
  3816. }
  3817. #if defined(CONFIG_IOS_JIT)
  3818. /* flush data cache on mirror */
  3819. flush_dcache_range((uintptr_t)TCG_CODE_PTR_RW(s, s->code_buf),
  3820. (uintptr_t)TCG_CODE_PTR_RW(s, s->code_ptr));
  3821. #endif
  3822. /* flush instruction cache */
  3823. flush_icache_range((uintptr_t)s->code_buf, (uintptr_t)s->code_ptr);
  3824. return tcg_current_code_size(s);
  3825. }
  3826. #ifdef CONFIG_PROFILER
  3827. void tcg_dump_info(void)
  3828. {
  3829. TCGProfile prof = {};
  3830. const TCGProfile *s;
  3831. int64_t tb_count;
  3832. int64_t tb_div_count;
  3833. int64_t tot;
  3834. tcg_profile_snapshot_counters(&prof);
  3835. s = &prof;
  3836. tb_count = s->tb_count;
  3837. tb_div_count = tb_count ? tb_count : 1;
  3838. tot = s->interm_time + s->code_time;
  3839. qemu_printf("JIT cycles %" PRId64 " (%0.3f s at 2.4 GHz)\n",
  3840. tot, tot / 2.4e9);
  3841. qemu_printf("translated TBs %" PRId64 " (aborted=%" PRId64
  3842. " %0.1f%%)\n",
  3843. tb_count, s->tb_count1 - tb_count,
  3844. (double)(s->tb_count1 - s->tb_count)
  3845. / (s->tb_count1 ? s->tb_count1 : 1) * 100.0);
  3846. qemu_printf("avg ops/TB %0.1f max=%d\n",
  3847. (double)s->op_count / tb_div_count, s->op_count_max);
  3848. qemu_printf("deleted ops/TB %0.2f\n",
  3849. (double)s->del_op_count / tb_div_count);
  3850. qemu_printf("avg temps/TB %0.2f max=%d\n",
  3851. (double)s->temp_count / tb_div_count, s->temp_count_max);
  3852. qemu_printf("avg host code/TB %0.1f\n",
  3853. (double)s->code_out_len / tb_div_count);
  3854. qemu_printf("avg search data/TB %0.1f\n",
  3855. (double)s->search_out_len / tb_div_count);
  3856. qemu_printf("cycles/op %0.1f\n",
  3857. s->op_count ? (double)tot / s->op_count : 0);
  3858. qemu_printf("cycles/in byte %0.1f\n",
  3859. s->code_in_len ? (double)tot / s->code_in_len : 0);
  3860. qemu_printf("cycles/out byte %0.1f\n",
  3861. s->code_out_len ? (double)tot / s->code_out_len : 0);
  3862. qemu_printf("cycles/search byte %0.1f\n",
  3863. s->search_out_len ? (double)tot / s->search_out_len : 0);
  3864. if (tot == 0) {
  3865. tot = 1;
  3866. }
  3867. qemu_printf(" gen_interm time %0.1f%%\n",
  3868. (double)s->interm_time / tot * 100.0);
  3869. qemu_printf(" gen_code time %0.1f%%\n",
  3870. (double)s->code_time / tot * 100.0);
  3871. qemu_printf("optim./code time %0.1f%%\n",
  3872. (double)s->opt_time / (s->code_time ? s->code_time : 1)
  3873. * 100.0);
  3874. qemu_printf("liveness/code time %0.1f%%\n",
  3875. (double)s->la_time / (s->code_time ? s->code_time : 1) * 100.0);
  3876. qemu_printf("cpu_restore count %" PRId64 "\n",
  3877. s->restore_count);
  3878. qemu_printf(" avg cycles %0.1f\n",
  3879. s->restore_count ? (double)s->restore_time / s->restore_count : 0);
  3880. }
  3881. #else
  3882. void tcg_dump_info(void)
  3883. {
  3884. qemu_printf("[TCG profiler not compiled]\n");
  3885. }
  3886. #endif
  3887. #ifdef ELF_HOST_MACHINE
  3888. /* In order to use this feature, the backend needs to do three things:
  3889. (1) Define ELF_HOST_MACHINE to indicate both what value to
  3890. put into the ELF image and to indicate support for the feature.
  3891. (2) Define tcg_register_jit. This should create a buffer containing
  3892. the contents of a .debug_frame section that describes the post-
  3893. prologue unwind info for the tcg machine.
  3894. (3) Call tcg_register_jit_int, with the constructed .debug_frame.
  3895. */
  3896. /* Begin GDB interface. THE FOLLOWING MUST MATCH GDB DOCS. */
  3897. typedef enum {
  3898. JIT_NOACTION = 0,
  3899. JIT_REGISTER_FN,
  3900. JIT_UNREGISTER_FN
  3901. } jit_actions_t;
  3902. struct jit_code_entry {
  3903. struct jit_code_entry *next_entry;
  3904. struct jit_code_entry *prev_entry;
  3905. const void *symfile_addr;
  3906. uint64_t symfile_size;
  3907. };
  3908. struct jit_descriptor {
  3909. uint32_t version;
  3910. uint32_t action_flag;
  3911. struct jit_code_entry *relevant_entry;
  3912. struct jit_code_entry *first_entry;
  3913. };
  3914. void __jit_debug_register_code(void) __attribute__((noinline));
  3915. void __jit_debug_register_code(void)
  3916. {
  3917. asm("");
  3918. }
  3919. /* Must statically initialize the version, because GDB may check
  3920. the version before we can set it. */
  3921. struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
  3922. /* End GDB interface. */
  3923. static int find_string(const char *strtab, const char *str)
  3924. {
  3925. const char *p = strtab + 1;
  3926. while (1) {
  3927. if (strcmp(p, str) == 0) {
  3928. return p - strtab;
  3929. }
  3930. p += strlen(p) + 1;
  3931. }
  3932. }
  3933. static void tcg_register_jit_int(void *buf_ptr, size_t buf_size,
  3934. const void *debug_frame,
  3935. size_t debug_frame_size)
  3936. {
  3937. struct __attribute__((packed)) DebugInfo {
  3938. uint32_t len;
  3939. uint16_t version;
  3940. uint32_t abbrev;
  3941. uint8_t ptr_size;
  3942. uint8_t cu_die;
  3943. uint16_t cu_lang;
  3944. uintptr_t cu_low_pc;
  3945. uintptr_t cu_high_pc;
  3946. uint8_t fn_die;
  3947. char fn_name[16];
  3948. uintptr_t fn_low_pc;
  3949. uintptr_t fn_high_pc;
  3950. uint8_t cu_eoc;
  3951. };
  3952. struct ElfImage {
  3953. ElfW(Ehdr) ehdr;
  3954. ElfW(Phdr) phdr;
  3955. ElfW(Shdr) shdr[7];
  3956. ElfW(Sym) sym[2];
  3957. struct DebugInfo di;
  3958. uint8_t da[24];
  3959. char str[80];
  3960. };
  3961. struct ElfImage *img;
  3962. static const struct ElfImage img_template = {
  3963. .ehdr = {
  3964. .e_ident[EI_MAG0] = ELFMAG0,
  3965. .e_ident[EI_MAG1] = ELFMAG1,
  3966. .e_ident[EI_MAG2] = ELFMAG2,
  3967. .e_ident[EI_MAG3] = ELFMAG3,
  3968. .e_ident[EI_CLASS] = ELF_CLASS,
  3969. .e_ident[EI_DATA] = ELF_DATA,
  3970. .e_ident[EI_VERSION] = EV_CURRENT,
  3971. .e_type = ET_EXEC,
  3972. .e_machine = ELF_HOST_MACHINE,
  3973. .e_version = EV_CURRENT,
  3974. .e_phoff = offsetof(struct ElfImage, phdr),
  3975. .e_shoff = offsetof(struct ElfImage, shdr),
  3976. .e_ehsize = sizeof(ElfW(Shdr)),
  3977. .e_phentsize = sizeof(ElfW(Phdr)),
  3978. .e_phnum = 1,
  3979. .e_shentsize = sizeof(ElfW(Shdr)),
  3980. .e_shnum = ARRAY_SIZE(img->shdr),
  3981. .e_shstrndx = ARRAY_SIZE(img->shdr) - 1,
  3982. #ifdef ELF_HOST_FLAGS
  3983. .e_flags = ELF_HOST_FLAGS,
  3984. #endif
  3985. #ifdef ELF_OSABI
  3986. .e_ident[EI_OSABI] = ELF_OSABI,
  3987. #endif
  3988. },
  3989. .phdr = {
  3990. .p_type = PT_LOAD,
  3991. .p_flags = PF_X,
  3992. },
  3993. .shdr = {
  3994. [0] = { .sh_type = SHT_NULL },
  3995. /* Trick: The contents of code_gen_buffer are not present in
  3996. this fake ELF file; that got allocated elsewhere. Therefore
  3997. we mark .text as SHT_NOBITS (similar to .bss) so that readers
  3998. will not look for contents. We can record any address. */
  3999. [1] = { /* .text */
  4000. .sh_type = SHT_NOBITS,
  4001. .sh_flags = SHF_EXECINSTR | SHF_ALLOC,
  4002. },
  4003. [2] = { /* .debug_info */
  4004. .sh_type = SHT_PROGBITS,
  4005. .sh_offset = offsetof(struct ElfImage, di),
  4006. .sh_size = sizeof(struct DebugInfo),
  4007. },
  4008. [3] = { /* .debug_abbrev */
  4009. .sh_type = SHT_PROGBITS,
  4010. .sh_offset = offsetof(struct ElfImage, da),
  4011. .sh_size = sizeof(img->da),
  4012. },
  4013. [4] = { /* .debug_frame */
  4014. .sh_type = SHT_PROGBITS,
  4015. .sh_offset = sizeof(struct ElfImage),
  4016. },
  4017. [5] = { /* .symtab */
  4018. .sh_type = SHT_SYMTAB,
  4019. .sh_offset = offsetof(struct ElfImage, sym),
  4020. .sh_size = sizeof(img->sym),
  4021. .sh_info = 1,
  4022. .sh_link = ARRAY_SIZE(img->shdr) - 1,
  4023. .sh_entsize = sizeof(ElfW(Sym)),
  4024. },
  4025. [6] = { /* .strtab */
  4026. .sh_type = SHT_STRTAB,
  4027. .sh_offset = offsetof(struct ElfImage, str),
  4028. .sh_size = sizeof(img->str),
  4029. }
  4030. },
  4031. .sym = {
  4032. [1] = { /* code_gen_buffer */
  4033. .st_info = ELF_ST_INFO(STB_GLOBAL, STT_FUNC),
  4034. .st_shndx = 1,
  4035. }
  4036. },
  4037. .di = {
  4038. .len = sizeof(struct DebugInfo) - 4,
  4039. .version = 2,
  4040. .ptr_size = sizeof(void *),
  4041. .cu_die = 1,
  4042. .cu_lang = 0x8001, /* DW_LANG_Mips_Assembler */
  4043. .fn_die = 2,
  4044. .fn_name = "code_gen_buffer"
  4045. },
  4046. .da = {
  4047. 1, /* abbrev number (the cu) */
  4048. 0x11, 1, /* DW_TAG_compile_unit, has children */
  4049. 0x13, 0x5, /* DW_AT_language, DW_FORM_data2 */
  4050. 0x11, 0x1, /* DW_AT_low_pc, DW_FORM_addr */
  4051. 0x12, 0x1, /* DW_AT_high_pc, DW_FORM_addr */
  4052. 0, 0, /* end of abbrev */
  4053. 2, /* abbrev number (the fn) */
  4054. 0x2e, 0, /* DW_TAG_subprogram, no children */
  4055. 0x3, 0x8, /* DW_AT_name, DW_FORM_string */
  4056. 0x11, 0x1, /* DW_AT_low_pc, DW_FORM_addr */
  4057. 0x12, 0x1, /* DW_AT_high_pc, DW_FORM_addr */
  4058. 0, 0, /* end of abbrev */
  4059. 0 /* no more abbrev */
  4060. },
  4061. .str = "\0" ".text\0" ".debug_info\0" ".debug_abbrev\0"
  4062. ".debug_frame\0" ".symtab\0" ".strtab\0" "code_gen_buffer",
  4063. };
  4064. /* We only need a single jit entry; statically allocate it. */
  4065. static struct jit_code_entry one_entry;
  4066. uintptr_t buf = (uintptr_t)buf_ptr;
  4067. size_t img_size = sizeof(struct ElfImage) + debug_frame_size;
  4068. DebugFrameHeader *dfh;
  4069. img = g_malloc(img_size);
  4070. *img = img_template;
  4071. img->phdr.p_vaddr = buf;
  4072. img->phdr.p_paddr = buf;
  4073. img->phdr.p_memsz = buf_size;
  4074. img->shdr[1].sh_name = find_string(img->str, ".text");
  4075. img->shdr[1].sh_addr = buf;
  4076. img->shdr[1].sh_size = buf_size;
  4077. img->shdr[2].sh_name = find_string(img->str, ".debug_info");
  4078. img->shdr[3].sh_name = find_string(img->str, ".debug_abbrev");
  4079. img->shdr[4].sh_name = find_string(img->str, ".debug_frame");
  4080. img->shdr[4].sh_size = debug_frame_size;
  4081. img->shdr[5].sh_name = find_string(img->str, ".symtab");
  4082. img->shdr[6].sh_name = find_string(img->str, ".strtab");
  4083. img->sym[1].st_name = find_string(img->str, "code_gen_buffer");
  4084. img->sym[1].st_value = buf;
  4085. img->sym[1].st_size = buf_size;
  4086. img->di.cu_low_pc = buf;
  4087. img->di.cu_high_pc = buf + buf_size;
  4088. img->di.fn_low_pc = buf;
  4089. img->di.fn_high_pc = buf + buf_size;
  4090. dfh = (DebugFrameHeader *)(img + 1);
  4091. memcpy(dfh, debug_frame, debug_frame_size);
  4092. dfh->fde.func_start = buf;
  4093. dfh->fde.func_len = buf_size;
  4094. #ifdef DEBUG_JIT
  4095. /* Enable this block to be able to debug the ELF image file creation.
  4096. One can use readelf, objdump, or other inspection utilities. */
  4097. {
  4098. FILE *f = fopen("/tmp/qemu.jit", "w+b");
  4099. if (f) {
  4100. if (fwrite(img, img_size, 1, f) != img_size) {
  4101. /* Avoid stupid unused return value warning for fwrite. */
  4102. }
  4103. fclose(f);
  4104. }
  4105. }
  4106. #endif
  4107. one_entry.symfile_addr = img;
  4108. one_entry.symfile_size = img_size;
  4109. __jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
  4110. __jit_debug_descriptor.relevant_entry = &one_entry;
  4111. __jit_debug_descriptor.first_entry = &one_entry;
  4112. __jit_debug_register_code();
  4113. }
  4114. #else
  4115. /* No support for the feature. Provide the entry point expected by exec.c,
  4116. and implement the internal function we declared earlier. */
  4117. static void tcg_register_jit_int(void *buf, size_t size,
  4118. const void *debug_frame,
  4119. size_t debug_frame_size)
  4120. {
  4121. }
  4122. void tcg_register_jit(void *buf, size_t buf_size)
  4123. {
  4124. }
  4125. #endif /* ELF_HOST_MACHINE */
  4126. #if !TCG_TARGET_MAYBE_vec
  4127. void tcg_expand_vec_op(TCGOpcode o, TCGType t, unsigned e, TCGArg a0, ...)
  4128. {
  4129. g_assert_not_reached();
  4130. }
  4131. #endif