aasmcpu.pas 138 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Peter Vreman
  3. Contains the abstract assembler implementation for the i386
  4. * Portions of this code was inspired by the NASM sources
  5. The Netwide Assembler is Copyright (c) 1996 Simon Tatham and
  6. Julian Hall. All rights reserved.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ****************************************************************************
  19. }
  20. unit aasmcpu;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. globtype,verbose,
  25. cpubase,
  26. cgbase,cgutils,
  27. symtype,
  28. aasmbase,aasmtai,aasmdata,aasmsym,
  29. ogbase;
  30. const
  31. { "mov reg,reg" source operand number }
  32. O_MOV_SOURCE = 0;
  33. { "mov reg,reg" destination operand number }
  34. O_MOV_DEST = 1;
  35. { Operand types }
  36. OT_NONE = $00000000;
  37. { Bits 0..7: sizes }
  38. OT_BITS8 = $00000001;
  39. OT_BITS16 = $00000002;
  40. OT_BITS32 = $00000004;
  41. OT_BITS64 = $00000008; { x86_64 and FPU }
  42. OT_BITS128 = $10000000; { 16 byte SSE }
  43. OT_BITS256 = $20000000; { 32 byte AVX }
  44. OT_BITS80 = $00000010; { FPU only }
  45. OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
  46. OT_NEAR = $00000040;
  47. OT_SHORT = $00000080;
  48. { TODO: FAR/NEAR/SHORT are sizes too, they should be included into size mask,
  49. but this requires adjusting the opcode table }
  50. OT_SIZE_MASK = $3000001F; { all the size attributes }
  51. OT_NON_SIZE = longint(not OT_SIZE_MASK);
  52. { Bits 8..11: modifiers }
  53. OT_SIGNED = $00000100; { the operand need to be signed -128-127 }
  54. OT_TO = $00000200; { reverse effect in FADD, FSUB &c }
  55. OT_COLON = $00000400; { operand is followed by a colon }
  56. OT_MODIFIER_MASK = $00000F00;
  57. { Bits 12..15: type of operand }
  58. OT_REGISTER = $00001000;
  59. OT_IMMEDIATE = $00002000;
  60. OT_MEMORY = $0000C000; { always includes 'OT_REGMEM' bit as well }
  61. OT_REGMEM = $00008000; { for r/m, ie EA, operands }
  62. OT_TYPE_MASK = OT_REGISTER or OT_IMMEDIATE or OT_MEMORY or OT_REGMEM;
  63. OT_REGNORM = OT_REGISTER or OT_REGMEM; { 'normal' reg, qualifies as EA }
  64. { Bits 20..22, 24..26: register classes
  65. otf_* consts are not used alone, only to build other constants. }
  66. otf_reg_cdt = $00100000;
  67. otf_reg_gpr = $00200000;
  68. otf_reg_sreg = $00400000;
  69. otf_reg_fpu = $01000000;
  70. otf_reg_mmx = $02000000;
  71. otf_reg_xmm = $04000000;
  72. otf_reg_ymm = $08000000;
  73. { Bits 16..19: subclasses, meaning depends on classes field }
  74. otf_sub0 = $00010000;
  75. otf_sub1 = $00020000;
  76. otf_sub2 = $00040000;
  77. otf_sub3 = $00080000;
  78. OT_REG_SMASK = otf_sub0 or otf_sub1 or otf_sub2 or otf_sub3;
  79. OT_REG_TYPMASK = otf_reg_cdt or otf_reg_gpr or otf_reg_sreg or otf_reg_fpu or otf_reg_mmx or otf_reg_xmm or otf_reg_ymm;
  80. { register class 0: CRx, DRx and TRx }
  81. {$ifdef x86_64}
  82. OT_REG_CDT = OT_REGISTER or otf_reg_cdt or OT_BITS64;
  83. {$else x86_64}
  84. OT_REG_CDT = OT_REGISTER or otf_reg_cdt or OT_BITS32;
  85. {$endif x86_64}
  86. OT_REG_CREG = OT_REG_CDT or otf_sub0; { CRn }
  87. OT_REG_DREG = OT_REG_CDT or otf_sub1; { DRn }
  88. OT_REG_TREG = OT_REG_CDT or otf_sub2; { TRn }
  89. OT_REG_CR4 = OT_REG_CDT or otf_sub3; { CR4 (Pentium only) }
  90. { register class 1: general-purpose registers }
  91. OT_REG_GPR = OT_REGNORM or otf_reg_gpr;
  92. OT_RM_GPR = OT_REGMEM or otf_reg_gpr;
  93. OT_REG8 = OT_REG_GPR or OT_BITS8; { 8-bit GPR }
  94. OT_REG16 = OT_REG_GPR or OT_BITS16;
  95. OT_REG32 = OT_REG_GPR or OT_BITS32;
  96. OT_REG64 = OT_REG_GPR or OT_BITS64;
  97. { GPR subclass 0: accumulator: AL, AX, EAX or RAX }
  98. OT_REG_ACCUM = OT_REG_GPR or otf_sub0;
  99. OT_REG_AL = OT_REG_ACCUM or OT_BITS8;
  100. OT_REG_AX = OT_REG_ACCUM or OT_BITS16;
  101. OT_REG_EAX = OT_REG_ACCUM or OT_BITS32;
  102. {$ifdef x86_64}
  103. OT_REG_RAX = OT_REG_ACCUM or OT_BITS64;
  104. {$endif x86_64}
  105. { GPR subclass 1: counter: CL, CX, ECX or RCX }
  106. OT_REG_COUNT = OT_REG_GPR or otf_sub1;
  107. OT_REG_CL = OT_REG_COUNT or OT_BITS8;
  108. OT_REG_CX = OT_REG_COUNT or OT_BITS16;
  109. OT_REG_ECX = OT_REG_COUNT or OT_BITS32;
  110. {$ifdef x86_64}
  111. OT_REG_RCX = OT_REG_COUNT or OT_BITS64;
  112. {$endif x86_64}
  113. { GPR subclass 2: data register: DL, DX, EDX or RDX }
  114. OT_REG_DX = OT_REG_GPR or otf_sub2 or OT_BITS16;
  115. OT_REG_EDX = OT_REG_GPR or otf_sub2 or OT_BITS32;
  116. { register class 2: Segment registers }
  117. OT_REG_SREG = OT_REGISTER or otf_reg_sreg or OT_BITS16;
  118. OT_REG_CS = OT_REG_SREG or otf_sub0; { CS }
  119. OT_REG_DESS = OT_REG_SREG or otf_sub1; { DS, ES, SS (non-CS 86 registers) }
  120. OT_REG_FSGS = OT_REG_SREG or otf_sub2; { FS, GS (386 extended registers) }
  121. { register class 3: FPU registers }
  122. OT_FPUREG = OT_REGISTER or otf_reg_fpu;
  123. OT_FPU0 = OT_FPUREG or otf_sub0; { FPU stack register zero }
  124. { register class 4: MMX (both reg and r/m) }
  125. OT_MMXREG = OT_REGNORM or otf_reg_mmx;
  126. OT_MMXRM = OT_REGMEM or otf_reg_mmx;
  127. { register class 5: XMM (both reg and r/m) }
  128. OT_XMMREG = OT_REGNORM or otf_reg_xmm;
  129. OT_XMMRM = OT_REGMEM or otf_reg_xmm;
  130. OT_XMEM32 = OT_REGNORM or otf_reg_xmm or otf_reg_gpr or OT_BITS32;
  131. OT_XMEM64 = OT_REGNORM or otf_reg_xmm or otf_reg_gpr or OT_BITS64;
  132. { register class 5: XMM (both reg and r/m) }
  133. OT_YMMREG = OT_REGNORM or otf_reg_ymm;
  134. OT_YMMRM = OT_REGMEM or otf_reg_ymm;
  135. OT_YMEM32 = OT_REGNORM or otf_reg_ymm or otf_reg_gpr or OT_BITS32;
  136. OT_YMEM64 = OT_REGNORM or otf_reg_ymm or otf_reg_gpr or OT_BITS64;
  137. { Vector-Memory operands }
  138. OT_VMEM_ANY = OT_XMEM32 or OT_XMEM64 or OT_YMEM32 or OT_YMEM64;
  139. { Memory operands }
  140. OT_MEM8 = OT_MEMORY or OT_BITS8;
  141. OT_MEM16 = OT_MEMORY or OT_BITS16;
  142. OT_MEM32 = OT_MEMORY or OT_BITS32;
  143. OT_MEM64 = OT_MEMORY or OT_BITS64;
  144. OT_MEM128 = OT_MEMORY or OT_BITS128;
  145. OT_MEM256 = OT_MEMORY or OT_BITS256;
  146. OT_MEM80 = OT_MEMORY or OT_BITS80;
  147. OT_MEM_OFFS = OT_MEMORY or otf_sub0; { special type of EA }
  148. { simple [address] offset }
  149. { Matches any type of r/m operand }
  150. OT_MEMORY_ANY = OT_MEMORY or OT_RM_GPR or OT_XMMRM or OT_MMXRM or OT_YMMRM;
  151. { Immediate operands }
  152. OT_IMM8 = OT_IMMEDIATE or OT_BITS8;
  153. OT_IMM16 = OT_IMMEDIATE or OT_BITS16;
  154. OT_IMM32 = OT_IMMEDIATE or OT_BITS32;
  155. OT_IMM64 = OT_IMMEDIATE or OT_BITS64;
  156. OT_ONENESS = otf_sub0; { special type of immediate operand }
  157. OT_UNITY = OT_IMMEDIATE or OT_ONENESS; { for shift/rotate instructions }
  158. { Size of the instruction table converted by nasmconv.pas }
  159. {$if defined(x86_64)}
  160. instabentries = {$i x8664nop.inc}
  161. {$elseif defined(i386)}
  162. instabentries = {$i i386nop.inc}
  163. {$elseif defined(i8086)}
  164. instabentries = {$i i8086nop.inc}
  165. {$endif}
  166. maxinfolen = 8;
  167. MaxInsChanges = 3; { Max things a instruction can change }
  168. type
  169. { What an instruction can change. Needed for optimizer and spilling code.
  170. Note: The order of this enumeration is should not be changed! }
  171. TInsChange = (Ch_None,
  172. {Read from a register}
  173. Ch_REAX, Ch_RECX, Ch_REDX, Ch_REBX, Ch_RESP, Ch_REBP, Ch_RESI, Ch_REDI,
  174. {write from a register}
  175. Ch_WEAX, Ch_WECX, Ch_WEDX, Ch_WEBX, Ch_WESP, Ch_WEBP, Ch_WESI, Ch_WEDI,
  176. {read and write from/to a register}
  177. Ch_RWEAX, Ch_RWECX, Ch_RWEDX, Ch_RWEBX, Ch_RWESP, Ch_RWEBP, Ch_RWESI, Ch_RWEDI,
  178. {modify the contents of a register with the purpose of using
  179. this changed content afterwards (add/sub/..., but e.g. not rep
  180. or movsd)}
  181. Ch_MEAX, Ch_MECX, Ch_MEDX, Ch_MEBX, Ch_MESP, Ch_MEBP, Ch_MESI, Ch_MEDI,
  182. Ch_CDirFlag {clear direction flag}, Ch_SDirFlag {set dir flag},
  183. Ch_RFlags, Ch_WFlags, Ch_RWFlags, Ch_FPU,
  184. Ch_Rop1, Ch_Wop1, Ch_RWop1,Ch_Mop1,
  185. Ch_Rop2, Ch_Wop2, Ch_RWop2,Ch_Mop2,
  186. Ch_Rop3, Ch_WOp3, Ch_RWOp3,Ch_Mop3,
  187. Ch_WMemEDI,
  188. Ch_All,
  189. { x86_64 registers }
  190. Ch_RRAX, Ch_RRCX, Ch_RRDX, Ch_RRBX, Ch_RRSP, Ch_RRBP, Ch_RRSI, Ch_RRDI,
  191. Ch_WRAX, Ch_WRCX, Ch_WRDX, Ch_WRBX, Ch_WRSP, Ch_WRBP, Ch_WRSI, Ch_WRDI,
  192. Ch_RWRAX, Ch_RWRCX, Ch_RWRDX, Ch_RWRBX, Ch_RWRSP, Ch_RWRBP, Ch_RWRSI, Ch_RWRDI,
  193. Ch_MRAX, Ch_MRCX, Ch_MRDX, Ch_MRBX, Ch_MRSP, Ch_MRBP, Ch_MRSI, Ch_MRDI
  194. );
  195. TInsProp = packed record
  196. Ch : Array[1..MaxInsChanges] of TInsChange;
  197. end;
  198. TMemRefSizeInfo = (msiUnkown, msiUnsupported, msiNoSize,
  199. msiMultiple, msiMultiple8, msiMultiple16, msiMultiple32,
  200. msiMultiple64, msiMultiple128, msiMultiple256,
  201. msiMemRegSize, msiMemRegx16y32, msiMemRegx32y64, msiMemRegx64y128, msiMemRegx64y256,
  202. msiMem8, msiMem16, msiMem32, msiMem64, msiMem128, msiMem256,
  203. msiXMem32, msiXMem64, msiYMem32, msiYMem64,
  204. msiVMemMultiple, msiVMemRegSize);
  205. TConstSizeInfo = (csiUnkown, csiMultiple, csiNoSize, csiMem8, csiMem16, csiMem32, csiMem64);
  206. TInsTabMemRefSizeInfoRec = record
  207. MemRefSize : TMemRefSizeInfo;
  208. ExistsSSEAVX: boolean;
  209. ConstSize : TConstSizeInfo;
  210. end;
  211. const
  212. MemRefMultiples: set of TMemRefSizeInfo = [msiMultiple, msiMultiple8,
  213. msiMultiple16, msiMultiple32,
  214. msiMultiple64, msiMultiple128,
  215. msiMultiple256, msiVMemMultiple];
  216. MemRefSizeInfoVMems: Set of TMemRefSizeInfo = [msiXMem32, msiXMem64, msiYMem32, msiYMem64,
  217. msiVMemMultiple, msiVMemRegSize];
  218. InsProp : array[tasmop] of TInsProp =
  219. {$if defined(x86_64)}
  220. {$i x8664pro.inc}
  221. {$elseif defined(i386)}
  222. {$i i386prop.inc}
  223. {$elseif defined(i8086)}
  224. {$i i8086prop.inc}
  225. {$endif}
  226. type
  227. TOperandOrder = (op_intel,op_att);
  228. tinsentry=packed record
  229. opcode : tasmop;
  230. ops : byte;
  231. optypes : array[0..max_operands-1] of longint;
  232. code : array[0..maxinfolen] of char;
  233. flags : int64;
  234. end;
  235. pinsentry=^tinsentry;
  236. { alignment for operator }
  237. tai_align = class(tai_align_abstract)
  238. reg : tregister;
  239. constructor create(b:byte);override;
  240. constructor create_op(b: byte; _op: byte);override;
  241. function calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;override;
  242. end;
  243. taicpu = class(tai_cpu_abstract_sym)
  244. opsize : topsize;
  245. constructor op_none(op : tasmop);
  246. constructor op_none(op : tasmop;_size : topsize);
  247. constructor op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  248. constructor op_const(op : tasmop;_size : topsize;_op1 : aint);
  249. constructor op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  250. constructor op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  251. constructor op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  252. constructor op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  253. constructor op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  254. constructor op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  255. constructor op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  256. constructor op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  257. constructor op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  258. constructor op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  259. constructor op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  260. constructor op_ref_reg_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2,_op3 : tregister);
  261. constructor op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  262. { this is for Jmp instructions }
  263. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  264. constructor op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  265. constructor op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  266. constructor op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  267. constructor op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  268. procedure changeopsize(siz:topsize);
  269. function GetString:string;
  270. { This is a workaround for the GAS non commutative fpu instruction braindamage.
  271. Early versions of the UnixWare assembler had a bug where some fpu instructions
  272. were reversed and GAS still keeps this "feature" for compatibility.
  273. for details: http://sourceware.org/binutils/docs/as/i386_002dBugs.html#i386_002dBugs
  274. http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=372528
  275. http://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax#Caveats
  276. Since FPC is "GAS centric" due to its history it generates instructions with the same operand order so
  277. when generating output for other assemblers, the opcodes must be fixed before writing them.
  278. This function returns the fixed opcodes. Changing the opcodes permanently is no good idea
  279. because in case of smartlinking assembler is generated twice so at the second run wrong
  280. assembler is generated.
  281. }
  282. function FixNonCommutativeOpcodes: tasmop;
  283. private
  284. FOperandOrder : TOperandOrder;
  285. procedure init(_size : topsize); { this need to be called by all constructor }
  286. public
  287. { the next will reset all instructions that can change in pass 2 }
  288. procedure ResetPass1;override;
  289. procedure ResetPass2;override;
  290. function CheckIfValid:boolean;
  291. function Pass1(objdata:TObjData):longint;override;
  292. procedure Pass2(objdata:TObjData);override;
  293. procedure SetOperandOrder(order:TOperandOrder);
  294. function is_same_reg_move(regtype: Tregistertype):boolean;override;
  295. { register spilling code }
  296. function spilling_get_operation_type(opnr: longint): topertype;override;
  297. {$ifdef i8086}
  298. procedure loadsegsymbol(opidx:longint;s:tasmsymbol);
  299. {$endif i8086}
  300. private
  301. { next fields are filled in pass1, so pass2 is faster }
  302. insentry : PInsEntry;
  303. insoffset : longint;
  304. LastInsOffset : longint; { need to be public to be reset }
  305. inssize : shortint;
  306. {$ifdef x86_64}
  307. rex : byte;
  308. {$endif x86_64}
  309. function InsEnd:longint;
  310. procedure create_ot(objdata:TObjData);
  311. function Matches(p:PInsEntry):boolean;
  312. function calcsize(p:PInsEntry):shortint;
  313. procedure gencode(objdata:TObjData);
  314. function NeedAddrPrefix(opidx:byte):boolean;
  315. procedure Swapoperands;
  316. function FindInsentry(objdata:TObjData):boolean;
  317. end;
  318. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  319. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  320. function MemRefInfo(aAsmop: TAsmOp): TInsTabMemRefSizeInfoRec;
  321. procedure InitAsm;
  322. procedure DoneAsm;
  323. implementation
  324. uses
  325. cutils,
  326. globals,
  327. systems,
  328. procinfo,
  329. itcpugas,
  330. symsym,
  331. cpuinfo;
  332. {*****************************************************************************
  333. Instruction table
  334. *****************************************************************************}
  335. const
  336. {Instruction flags }
  337. IF_NONE = $00000000;
  338. IF_SM = $00000001; { size match first two operands }
  339. IF_SM2 = $00000002;
  340. IF_SB = $00000004; { unsized operands can't be non-byte }
  341. IF_SW = $00000008; { unsized operands can't be non-word }
  342. IF_SD = $00000010; { unsized operands can't be nondword }
  343. IF_SMASK = $0000001f;
  344. IF_AR0 = $00000020; { SB, SW, SD applies to argument 0 }
  345. IF_AR1 = $00000040; { SB, SW, SD applies to argument 1 }
  346. IF_AR2 = $00000060; { SB, SW, SD applies to argument 2 }
  347. IF_ARMASK = $00000060; { mask for unsized argument spec }
  348. IF_ARSHIFT = 5; { LSB of IF_ARMASK }
  349. IF_PRIV = $00000100; { it's a privileged instruction }
  350. IF_SMM = $00000200; { it's only valid in SMM }
  351. IF_PROT = $00000400; { it's protected mode only }
  352. IF_NOX86_64 = $00000800; { removed instruction in x86_64 }
  353. IF_UNDOC = $00001000; { it's an undocumented instruction }
  354. IF_FPU = $00002000; { it's an FPU instruction }
  355. IF_MMX = $00004000; { it's an MMX instruction }
  356. { it's a 3DNow! instruction }
  357. IF_3DNOW = $00008000;
  358. { it's a SSE (KNI, MMX2) instruction }
  359. IF_SSE = $00010000;
  360. { SSE2 instructions }
  361. IF_SSE2 = $00020000;
  362. { SSE3 instructions }
  363. IF_SSE3 = $00040000;
  364. { SSE64 instructions }
  365. IF_SSE64 = $00080000;
  366. { the mask for processor types }
  367. {IF_PMASK = longint($FF000000);}
  368. { the mask for disassembly "prefer" }
  369. {IF_PFMASK = longint($F001FF00);}
  370. { SVM instructions }
  371. IF_SVM = $00100000;
  372. { SSE4 instructions }
  373. IF_SSE4 = $00200000;
  374. { TODO: These flags were added to make x86ins.dat more readable.
  375. Values must be reassigned to make any other use of them. }
  376. IF_SSSE3 = $00200000;
  377. IF_SSE41 = $00200000;
  378. IF_SSE42 = $00200000;
  379. IF_AVX = $00200000;
  380. IF_AVX2 = $00200000;
  381. IF_BMI1 = $00200000;
  382. IF_BMI2 = $00200000;
  383. IF_16BITONLY = $00200000;
  384. IF_FMA = $00200000;
  385. IF_FMA4 = $00200000;
  386. IF_TSX = $00200000;
  387. IF_RAND = $00200000;
  388. IF_PLEVEL = $0F000000; { mask for processor level }
  389. IF_8086 = $00000000; { 8086 instruction }
  390. IF_186 = $01000000; { 186+ instruction }
  391. IF_286 = $02000000; { 286+ instruction }
  392. IF_386 = $03000000; { 386+ instruction }
  393. IF_486 = $04000000; { 486+ instruction }
  394. IF_PENT = $05000000; { Pentium instruction }
  395. IF_P6 = $06000000; { P6 instruction }
  396. IF_KATMAI = $07000000; { Katmai instructions }
  397. IF_WILLAMETTE = $08000000; { Willamette instructions }
  398. IF_PRESCOTT = $09000000; { Prescott instructions }
  399. IF_X86_64 = $0a000000;
  400. IF_SANDYBRIDGE = $0e000000; { Sandybridge-specific instruction }
  401. IF_NEC = $0f000000; { NEC V20/V30 instruction }
  402. { the following are not strictly part of the processor level, because
  403. they are never used standalone, but always in combination with a
  404. separate processor level flag. Therefore, they use bits outside of
  405. IF_PLEVEL, otherwise they would mess up the processor level they're
  406. used in combination with.
  407. The following combinations are currently used:
  408. IF_AMD or IF_P6,
  409. IF_CYRIX or IF_486,
  410. IF_CYRIX or IF_PENT,
  411. IF_CYRIX or IF_P6 }
  412. IF_CYRIX = $10000000; { Cyrix, Centaur or VIA-specific instruction }
  413. IF_AMD = $20000000; { AMD-specific instruction }
  414. { added flags }
  415. IF_PRE = $40000000; { it's a prefix instruction }
  416. IF_PASS2 = $80000000; { if the instruction can change in a second pass }
  417. IF_IMM4 = $100000000; { immediate operand is a nibble (must be in range [0..15]) }
  418. IF_IMM3 = $200000000; { immediate operand is a triad (must be in range [0..7]) }
  419. type
  420. TInsTabCache=array[TasmOp] of longint;
  421. PInsTabCache=^TInsTabCache;
  422. TInsTabMemRefSizeInfoCache=array[TasmOp] of TInsTabMemRefSizeInfoRec;
  423. PInsTabMemRefSizeInfoCache=^TInsTabMemRefSizeInfoCache;
  424. const
  425. {$if defined(x86_64)}
  426. InsTab:array[0..instabentries-1] of TInsEntry={$i x8664tab.inc}
  427. {$elseif defined(i386)}
  428. InsTab:array[0..instabentries-1] of TInsEntry={$i i386tab.inc}
  429. {$elseif defined(i8086)}
  430. InsTab:array[0..instabentries-1] of TInsEntry={$i i8086tab.inc}
  431. {$endif}
  432. var
  433. InsTabCache : PInsTabCache;
  434. InsTabMemRefSizeInfoCache: PInsTabMemRefSizeInfoCache;
  435. const
  436. {$if defined(x86_64)}
  437. { Intel style operands ! }
  438. opsize_2_type:array[0..2,topsize] of longint=(
  439. (OT_NONE,
  440. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,OT_BITS64,OT_BITS64,OT_BITS64,
  441. OT_BITS16,OT_BITS32,OT_BITS64,
  442. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  443. OT_BITS64,
  444. OT_NEAR,OT_FAR,OT_SHORT,
  445. OT_NONE,
  446. OT_BITS128,
  447. OT_BITS256
  448. ),
  449. (OT_NONE,
  450. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,OT_BITS8,OT_BITS16,OT_BITS32,
  451. OT_BITS16,OT_BITS32,OT_BITS64,
  452. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  453. OT_BITS64,
  454. OT_NEAR,OT_FAR,OT_SHORT,
  455. OT_NONE,
  456. OT_BITS128,
  457. OT_BITS256
  458. ),
  459. (OT_NONE,
  460. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,
  461. OT_BITS16,OT_BITS32,OT_BITS64,
  462. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  463. OT_BITS64,
  464. OT_NEAR,OT_FAR,OT_SHORT,
  465. OT_NONE,
  466. OT_BITS128,
  467. OT_BITS256
  468. )
  469. );
  470. reg_ot_table : array[tregisterindex] of longint = (
  471. {$i r8664ot.inc}
  472. );
  473. {$elseif defined(i386)}
  474. { Intel style operands ! }
  475. opsize_2_type:array[0..2,topsize] of longint=(
  476. (OT_NONE,
  477. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,
  478. OT_BITS16,OT_BITS32,OT_BITS64,
  479. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  480. OT_BITS64,
  481. OT_NEAR,OT_FAR,OT_SHORT,
  482. OT_NONE,
  483. OT_BITS128,
  484. OT_BITS256
  485. ),
  486. (OT_NONE,
  487. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,
  488. OT_BITS16,OT_BITS32,OT_BITS64,
  489. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  490. OT_BITS64,
  491. OT_NEAR,OT_FAR,OT_SHORT,
  492. OT_NONE,
  493. OT_BITS128,
  494. OT_BITS256
  495. ),
  496. (OT_NONE,
  497. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,
  498. OT_BITS16,OT_BITS32,OT_BITS64,
  499. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  500. OT_BITS64,
  501. OT_NEAR,OT_FAR,OT_SHORT,
  502. OT_NONE,
  503. OT_BITS128,
  504. OT_BITS256
  505. )
  506. );
  507. reg_ot_table : array[tregisterindex] of longint = (
  508. {$i r386ot.inc}
  509. );
  510. {$elseif defined(i8086)}
  511. { Intel style operands ! }
  512. opsize_2_type:array[0..2,topsize] of longint=(
  513. (OT_NONE,
  514. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,
  515. OT_BITS16,OT_BITS32,OT_BITS64,
  516. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  517. OT_BITS64,
  518. OT_NEAR,OT_FAR,OT_SHORT,
  519. OT_NONE,
  520. OT_BITS128,
  521. OT_BITS256
  522. ),
  523. (OT_NONE,
  524. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,
  525. OT_BITS16,OT_BITS32,OT_BITS64,
  526. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  527. OT_BITS64,
  528. OT_NEAR,OT_FAR,OT_SHORT,
  529. OT_NONE,
  530. OT_BITS128,
  531. OT_BITS256
  532. ),
  533. (OT_NONE,
  534. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,
  535. OT_BITS16,OT_BITS32,OT_BITS64,
  536. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  537. OT_BITS64,
  538. OT_NEAR,OT_FAR,OT_SHORT,
  539. OT_NONE,
  540. OT_BITS128,
  541. OT_BITS256
  542. )
  543. );
  544. reg_ot_table : array[tregisterindex] of longint = (
  545. {$i r8086ot.inc}
  546. );
  547. {$endif}
  548. function MemRefInfo(aAsmop: TAsmOp): TInsTabMemRefSizeInfoRec;
  549. begin
  550. result := InsTabMemRefSizeInfoCache^[aAsmop];
  551. end;
  552. { Operation type for spilling code }
  553. type
  554. toperation_type_table=array[tasmop,0..Max_Operands] of topertype;
  555. var
  556. operation_type_table : ^toperation_type_table;
  557. {****************************************************************************
  558. TAI_ALIGN
  559. ****************************************************************************}
  560. constructor tai_align.create(b: byte);
  561. begin
  562. inherited create(b);
  563. reg:=NR_ECX;
  564. end;
  565. constructor tai_align.create_op(b: byte; _op: byte);
  566. begin
  567. inherited create_op(b,_op);
  568. reg:=NR_NO;
  569. end;
  570. function tai_align.calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;
  571. const
  572. { Updated according to
  573. Software Optimization Guide for AMD Family 15h Processors, Verison 3.08, January 2014
  574. and
  575. Intel 64 and IA-32 Architectures Software Developer’s Manual
  576. Volume 2B: Instruction Set Reference, N-Z, January 2015
  577. }
  578. alignarray_cmovcpus:array[0..10] of string[11]=(
  579. #$66#$66#$66#$0F#$1F#$84#$00#$00#$00#$00#$00,
  580. #$66#$66#$0F#$1F#$84#$00#$00#$00#$00#$00,
  581. #$66#$0F#$1F#$84#$00#$00#$00#$00#$00,
  582. #$0F#$1F#$84#$00#$00#$00#$00#$00,
  583. #$0F#$1F#$80#$00#$00#$00#$00,
  584. #$66#$0F#$1F#$44#$00#$00,
  585. #$0F#$1F#$44#$00#$00,
  586. #$0F#$1F#$40#$00,
  587. #$0F#$1F#$00,
  588. #$66#$90,
  589. #$90);
  590. {$ifdef i8086}
  591. alignarray:array[0..5] of string[8]=(
  592. #$90#$90#$90#$90#$90#$90#$90,
  593. #$90#$90#$90#$90#$90#$90,
  594. #$90#$90#$90#$90,
  595. #$90#$90#$90,
  596. #$90#$90,
  597. #$90);
  598. {$else i8086}
  599. alignarray:array[0..5] of string[8]=(
  600. #$8D#$B4#$26#$00#$00#$00#$00,
  601. #$8D#$B6#$00#$00#$00#$00,
  602. #$8D#$74#$26#$00,
  603. #$8D#$76#$00,
  604. #$89#$F6,
  605. #$90);
  606. {$endif i8086}
  607. var
  608. bufptr : pchar;
  609. j : longint;
  610. localsize: byte;
  611. begin
  612. inherited calculatefillbuf(buf,executable);
  613. if not(use_op) and executable then
  614. begin
  615. bufptr:=pchar(@buf);
  616. { fillsize may still be used afterwards, so don't modify }
  617. { e.g. writebytes(hp.calculatefillbuf(buf)^,hp.fillsize) }
  618. localsize:=fillsize;
  619. while (localsize>0) do
  620. begin
  621. {$ifndef i8086}
  622. if CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype] then
  623. begin
  624. for j:=low(alignarray_cmovcpus) to high(alignarray_cmovcpus) do
  625. if (localsize>=length(alignarray_cmovcpus[j])) then
  626. break;
  627. move(alignarray_cmovcpus[j][1],bufptr^,length(alignarray_cmovcpus[j]));
  628. inc(bufptr,length(alignarray_cmovcpus[j]));
  629. dec(localsize,length(alignarray_cmovcpus[j]));
  630. end
  631. else
  632. {$endif not i8086}
  633. begin
  634. for j:=low(alignarray) to high(alignarray) do
  635. if (localsize>=length(alignarray[j])) then
  636. break;
  637. move(alignarray[j][1],bufptr^,length(alignarray[j]));
  638. inc(bufptr,length(alignarray[j]));
  639. dec(localsize,length(alignarray[j]));
  640. end
  641. end;
  642. end;
  643. calculatefillbuf:=pchar(@buf);
  644. end;
  645. {*****************************************************************************
  646. Taicpu Constructors
  647. *****************************************************************************}
  648. procedure taicpu.changeopsize(siz:topsize);
  649. begin
  650. opsize:=siz;
  651. end;
  652. procedure taicpu.init(_size : topsize);
  653. begin
  654. { default order is att }
  655. FOperandOrder:=op_att;
  656. segprefix:=NR_NO;
  657. opsize:=_size;
  658. insentry:=nil;
  659. LastInsOffset:=-1;
  660. InsOffset:=0;
  661. InsSize:=0;
  662. end;
  663. constructor taicpu.op_none(op : tasmop);
  664. begin
  665. inherited create(op);
  666. init(S_NO);
  667. end;
  668. constructor taicpu.op_none(op : tasmop;_size : topsize);
  669. begin
  670. inherited create(op);
  671. init(_size);
  672. end;
  673. constructor taicpu.op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  674. begin
  675. inherited create(op);
  676. init(_size);
  677. ops:=1;
  678. loadreg(0,_op1);
  679. end;
  680. constructor taicpu.op_const(op : tasmop;_size : topsize;_op1 : aint);
  681. begin
  682. inherited create(op);
  683. init(_size);
  684. ops:=1;
  685. loadconst(0,_op1);
  686. end;
  687. constructor taicpu.op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  688. begin
  689. inherited create(op);
  690. init(_size);
  691. ops:=1;
  692. loadref(0,_op1);
  693. end;
  694. constructor taicpu.op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  695. begin
  696. inherited create(op);
  697. init(_size);
  698. ops:=2;
  699. loadreg(0,_op1);
  700. loadreg(1,_op2);
  701. end;
  702. constructor taicpu.op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  703. begin
  704. inherited create(op);
  705. init(_size);
  706. ops:=2;
  707. loadreg(0,_op1);
  708. loadconst(1,_op2);
  709. end;
  710. constructor taicpu.op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  711. begin
  712. inherited create(op);
  713. init(_size);
  714. ops:=2;
  715. loadreg(0,_op1);
  716. loadref(1,_op2);
  717. end;
  718. constructor taicpu.op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  719. begin
  720. inherited create(op);
  721. init(_size);
  722. ops:=2;
  723. loadconst(0,_op1);
  724. loadreg(1,_op2);
  725. end;
  726. constructor taicpu.op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  727. begin
  728. inherited create(op);
  729. init(_size);
  730. ops:=2;
  731. loadconst(0,_op1);
  732. loadconst(1,_op2);
  733. end;
  734. constructor taicpu.op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  735. begin
  736. inherited create(op);
  737. init(_size);
  738. ops:=2;
  739. loadconst(0,_op1);
  740. loadref(1,_op2);
  741. end;
  742. constructor taicpu.op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  743. begin
  744. inherited create(op);
  745. init(_size);
  746. ops:=2;
  747. loadref(0,_op1);
  748. loadreg(1,_op2);
  749. end;
  750. constructor taicpu.op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  751. begin
  752. inherited create(op);
  753. init(_size);
  754. ops:=3;
  755. loadreg(0,_op1);
  756. loadreg(1,_op2);
  757. loadreg(2,_op3);
  758. end;
  759. constructor taicpu.op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  760. begin
  761. inherited create(op);
  762. init(_size);
  763. ops:=3;
  764. loadconst(0,_op1);
  765. loadreg(1,_op2);
  766. loadreg(2,_op3);
  767. end;
  768. constructor taicpu.op_ref_reg_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2,_op3 : tregister);
  769. begin
  770. inherited create(op);
  771. init(_size);
  772. ops:=3;
  773. loadref(0,_op1);
  774. loadreg(1,_op2);
  775. loadreg(2,_op3);
  776. end;
  777. constructor taicpu.op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  778. begin
  779. inherited create(op);
  780. init(_size);
  781. ops:=3;
  782. loadconst(0,_op1);
  783. loadref(1,_op2);
  784. loadreg(2,_op3);
  785. end;
  786. constructor taicpu.op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  787. begin
  788. inherited create(op);
  789. init(_size);
  790. ops:=3;
  791. loadconst(0,_op1);
  792. loadreg(1,_op2);
  793. loadref(2,_op3);
  794. end;
  795. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  796. begin
  797. inherited create(op);
  798. init(_size);
  799. condition:=cond;
  800. ops:=1;
  801. loadsymbol(0,_op1,0);
  802. end;
  803. constructor taicpu.op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  804. begin
  805. inherited create(op);
  806. init(_size);
  807. ops:=1;
  808. loadsymbol(0,_op1,0);
  809. end;
  810. constructor taicpu.op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  811. begin
  812. inherited create(op);
  813. init(_size);
  814. ops:=1;
  815. loadsymbol(0,_op1,_op1ofs);
  816. end;
  817. constructor taicpu.op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  818. begin
  819. inherited create(op);
  820. init(_size);
  821. ops:=2;
  822. loadsymbol(0,_op1,_op1ofs);
  823. loadreg(1,_op2);
  824. end;
  825. constructor taicpu.op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  826. begin
  827. inherited create(op);
  828. init(_size);
  829. ops:=2;
  830. loadsymbol(0,_op1,_op1ofs);
  831. loadref(1,_op2);
  832. end;
  833. function taicpu.GetString:string;
  834. var
  835. i : longint;
  836. s : string;
  837. addsize : boolean;
  838. begin
  839. s:='['+std_op2str[opcode];
  840. for i:=0 to ops-1 do
  841. begin
  842. with oper[i]^ do
  843. begin
  844. if i=0 then
  845. s:=s+' '
  846. else
  847. s:=s+',';
  848. { type }
  849. addsize:=false;
  850. if (ot and OT_XMMREG)=OT_XMMREG then
  851. s:=s+'xmmreg'
  852. else
  853. if (ot and OT_YMMREG)=OT_YMMREG then
  854. s:=s+'ymmreg'
  855. else
  856. if (ot and OT_MMXREG)=OT_MMXREG then
  857. s:=s+'mmxreg'
  858. else
  859. if (ot and OT_FPUREG)=OT_FPUREG then
  860. s:=s+'fpureg'
  861. else
  862. if (ot and OT_REGISTER)=OT_REGISTER then
  863. begin
  864. s:=s+'reg';
  865. addsize:=true;
  866. end
  867. else
  868. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  869. begin
  870. s:=s+'imm';
  871. addsize:=true;
  872. end
  873. else
  874. if (ot and OT_MEMORY)=OT_MEMORY then
  875. begin
  876. s:=s+'mem';
  877. addsize:=true;
  878. end
  879. else
  880. s:=s+'???';
  881. { size }
  882. if addsize then
  883. begin
  884. if (ot and OT_BITS8)<>0 then
  885. s:=s+'8'
  886. else
  887. if (ot and OT_BITS16)<>0 then
  888. s:=s+'16'
  889. else
  890. if (ot and OT_BITS32)<>0 then
  891. s:=s+'32'
  892. else
  893. if (ot and OT_BITS64)<>0 then
  894. s:=s+'64'
  895. else
  896. if (ot and OT_BITS128)<>0 then
  897. s:=s+'128'
  898. else
  899. if (ot and OT_BITS256)<>0 then
  900. s:=s+'256'
  901. else
  902. s:=s+'??';
  903. { signed }
  904. if (ot and OT_SIGNED)<>0 then
  905. s:=s+'s';
  906. end;
  907. end;
  908. end;
  909. GetString:=s+']';
  910. end;
  911. procedure taicpu.Swapoperands;
  912. var
  913. p : POper;
  914. begin
  915. { Fix the operands which are in AT&T style and we need them in Intel style }
  916. case ops of
  917. 0,1:
  918. ;
  919. 2 : begin
  920. { 0,1 -> 1,0 }
  921. p:=oper[0];
  922. oper[0]:=oper[1];
  923. oper[1]:=p;
  924. end;
  925. 3 : begin
  926. { 0,1,2 -> 2,1,0 }
  927. p:=oper[0];
  928. oper[0]:=oper[2];
  929. oper[2]:=p;
  930. end;
  931. 4 : begin
  932. { 0,1,2,3 -> 3,2,1,0 }
  933. p:=oper[0];
  934. oper[0]:=oper[3];
  935. oper[3]:=p;
  936. p:=oper[1];
  937. oper[1]:=oper[2];
  938. oper[2]:=p;
  939. end;
  940. else
  941. internalerror(201108141);
  942. end;
  943. end;
  944. procedure taicpu.SetOperandOrder(order:TOperandOrder);
  945. begin
  946. if FOperandOrder<>order then
  947. begin
  948. Swapoperands;
  949. FOperandOrder:=order;
  950. end;
  951. end;
  952. function taicpu.FixNonCommutativeOpcodes: tasmop;
  953. begin
  954. result:=opcode;
  955. { we need ATT order }
  956. SetOperandOrder(op_att);
  957. if (
  958. (ops=2) and
  959. (oper[0]^.typ=top_reg) and
  960. (oper[1]^.typ=top_reg) and
  961. { if the first is ST and the second is also a register
  962. it is necessarily ST1 .. ST7 }
  963. ((oper[0]^.reg=NR_ST) or
  964. (oper[0]^.reg=NR_ST0))
  965. ) or
  966. { ((ops=1) and
  967. (oper[0]^.typ=top_reg) and
  968. (oper[0]^.reg in [R_ST1..R_ST7])) or}
  969. (ops=0) then
  970. begin
  971. if opcode=A_FSUBR then
  972. result:=A_FSUB
  973. else if opcode=A_FSUB then
  974. result:=A_FSUBR
  975. else if opcode=A_FDIVR then
  976. result:=A_FDIV
  977. else if opcode=A_FDIV then
  978. result:=A_FDIVR
  979. else if opcode=A_FSUBRP then
  980. result:=A_FSUBP
  981. else if opcode=A_FSUBP then
  982. result:=A_FSUBRP
  983. else if opcode=A_FDIVRP then
  984. result:=A_FDIVP
  985. else if opcode=A_FDIVP then
  986. result:=A_FDIVRP;
  987. end;
  988. if (
  989. (ops=1) and
  990. (oper[0]^.typ=top_reg) and
  991. (getregtype(oper[0]^.reg)=R_FPUREGISTER) and
  992. (oper[0]^.reg<>NR_ST)
  993. ) then
  994. begin
  995. if opcode=A_FSUBRP then
  996. result:=A_FSUBP
  997. else if opcode=A_FSUBP then
  998. result:=A_FSUBRP
  999. else if opcode=A_FDIVRP then
  1000. result:=A_FDIVP
  1001. else if opcode=A_FDIVP then
  1002. result:=A_FDIVRP;
  1003. end;
  1004. end;
  1005. {*****************************************************************************
  1006. Assembler
  1007. *****************************************************************************}
  1008. type
  1009. ea = packed record
  1010. sib_present : boolean;
  1011. bytes : byte;
  1012. size : byte;
  1013. modrm : byte;
  1014. sib : byte;
  1015. {$ifdef x86_64}
  1016. rex : byte;
  1017. {$endif x86_64}
  1018. end;
  1019. procedure taicpu.create_ot(objdata:TObjData);
  1020. {
  1021. this function will also fix some other fields which only needs to be once
  1022. }
  1023. var
  1024. i,l,relsize : longint;
  1025. currsym : TObjSymbol;
  1026. begin
  1027. if ops=0 then
  1028. exit;
  1029. { update oper[].ot field }
  1030. for i:=0 to ops-1 do
  1031. with oper[i]^ do
  1032. begin
  1033. case typ of
  1034. top_reg :
  1035. begin
  1036. ot:=reg_ot_table[findreg_by_number(reg)];
  1037. end;
  1038. top_ref :
  1039. begin
  1040. if (ref^.refaddr=addr_no)
  1041. {$ifdef i386}
  1042. or (
  1043. (ref^.refaddr in [addr_pic]) and
  1044. (ref^.base<>NR_NO)
  1045. )
  1046. {$endif i386}
  1047. {$ifdef x86_64}
  1048. or (
  1049. (ref^.refaddr in [addr_pic,addr_pic_no_got]) and
  1050. (ref^.base<>NR_NO)
  1051. )
  1052. {$endif x86_64}
  1053. then
  1054. begin
  1055. { create ot field }
  1056. if (reg_ot_table[findreg_by_number(ref^.base)] and OT_REG_GPR = OT_REG_GPR) and
  1057. ((reg_ot_table[findreg_by_number(ref^.index)] = OT_XMMREG) or
  1058. (reg_ot_table[findreg_by_number(ref^.index)] = OT_YMMREG)
  1059. ) then
  1060. // AVX2 - vector-memory-referenz (e.g. vgatherdpd xmm0, [rax xmm1], xmm2)
  1061. ot := (reg_ot_table[findreg_by_number(ref^.base)] and OT_REG_GPR) or
  1062. (reg_ot_table[findreg_by_number(ref^.index)])
  1063. else if (ref^.base = NR_NO) and
  1064. ((reg_ot_table[findreg_by_number(ref^.index)] = OT_XMMREG) or
  1065. (reg_ot_table[findreg_by_number(ref^.index)] = OT_YMMREG)
  1066. ) then
  1067. // AVX2 - vector-memory-referenz without base-register (e.g. vgatherdpd xmm0, [xmm1], xmm2)
  1068. ot := (OT_REG_GPR) or
  1069. (reg_ot_table[findreg_by_number(ref^.index)])
  1070. else if (ot and OT_SIZE_MASK)=0 then
  1071. ot:=OT_MEMORY_ANY or opsize_2_type[i,opsize]
  1072. else
  1073. ot:=OT_MEMORY_ANY or (ot and OT_SIZE_MASK);
  1074. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  1075. ot:=ot or OT_MEM_OFFS;
  1076. { fix scalefactor }
  1077. if (ref^.index=NR_NO) then
  1078. ref^.scalefactor:=0
  1079. else
  1080. if (ref^.scalefactor=0) then
  1081. ref^.scalefactor:=1;
  1082. end
  1083. else
  1084. begin
  1085. { Jumps use a relative offset which can be 8bit,
  1086. for other opcodes we always need to generate the full
  1087. 32bit address }
  1088. if assigned(objdata) and
  1089. is_jmp then
  1090. begin
  1091. currsym:=objdata.symbolref(ref^.symbol);
  1092. l:=ref^.offset;
  1093. {$push}
  1094. {$r-,q-} { disable also overflow as address returns a qword for x86_64 }
  1095. if assigned(currsym) then
  1096. inc(l,currsym.address);
  1097. {$pop}
  1098. { when it is a forward jump we need to compensate the
  1099. offset of the instruction since the previous time,
  1100. because the symbol address is then still using the
  1101. 'old-style' addressing.
  1102. For backwards jumps this is not required because the
  1103. address of the symbol is already adjusted to the
  1104. new offset }
  1105. if (l>InsOffset) and (LastInsOffset<>-1) then
  1106. inc(l,InsOffset-LastInsOffset);
  1107. { instruction size will then always become 2 (PFV) }
  1108. relsize:=(InsOffset+2)-l;
  1109. if (relsize>=-128) and (relsize<=127) and
  1110. (
  1111. not assigned(currsym) or
  1112. (currsym.objsection=objdata.currobjsec)
  1113. ) then
  1114. ot:=OT_IMM8 or OT_SHORT
  1115. else
  1116. {$ifdef i8086}
  1117. ot:=OT_IMM16 or OT_NEAR;
  1118. {$else i8086}
  1119. ot:=OT_IMM32 or OT_NEAR;
  1120. {$endif i8086}
  1121. end
  1122. else
  1123. {$ifdef i8086}
  1124. if opsize=S_FAR then
  1125. ot:=OT_IMM16 or OT_FAR
  1126. else
  1127. ot:=OT_IMM16 or OT_NEAR;
  1128. {$else i8086}
  1129. ot:=OT_IMM32 or OT_NEAR;
  1130. {$endif i8086}
  1131. end;
  1132. end;
  1133. top_local :
  1134. begin
  1135. if (ot and OT_SIZE_MASK)=0 then
  1136. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  1137. else
  1138. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  1139. end;
  1140. top_const :
  1141. begin
  1142. // if opcode is a SSE or AVX-instruction then we need a
  1143. // special handling (opsize can different from const-size)
  1144. // (e.g. "pextrw reg/m16, xmmreg, imm8" =>> opsize (16 bit), const-size (8 bit)
  1145. if (InsTabMemRefSizeInfoCache^[opcode].ExistsSSEAVX) and
  1146. (not(InsTabMemRefSizeInfoCache^[opcode].ConstSize in [csiMultiple, csiUnkown])) then
  1147. begin
  1148. case InsTabMemRefSizeInfoCache^[opcode].ConstSize of
  1149. csiNoSize: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE;
  1150. csiMem8: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS8;
  1151. csiMem16: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS16;
  1152. csiMem32: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS32;
  1153. csiMem64: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS64;
  1154. end;
  1155. end
  1156. else
  1157. begin
  1158. { allow 2nd, 3rd or 4th operand being a constant and expect no size for shuf* etc. }
  1159. { further, allow AAD and AAM with imm. operand }
  1160. if (opsize=S_NO) and not((i in [1,2,3])
  1161. {$ifndef x86_64}
  1162. or ((i=0) and (opcode in [A_AAD,A_AAM]))
  1163. {$endif x86_64}
  1164. ) then
  1165. message(asmr_e_invalid_opcode_and_operand);
  1166. if
  1167. {$ifndef i8086}
  1168. (opsize<>S_W) and
  1169. {$endif not i8086}
  1170. (aint(val)>=-128) and (val<=127) then
  1171. ot:=OT_IMM8 or OT_SIGNED
  1172. else
  1173. ot:=OT_IMMEDIATE or opsize_2_type[i,opsize];
  1174. if (val=1) and (i=1) then
  1175. ot := ot or OT_ONENESS;
  1176. end;
  1177. end;
  1178. top_none :
  1179. begin
  1180. { generated when there was an error in the
  1181. assembler reader. It never happends when generating
  1182. assembler }
  1183. end;
  1184. else
  1185. internalerror(200402266);
  1186. end;
  1187. end;
  1188. end;
  1189. function taicpu.InsEnd:longint;
  1190. begin
  1191. InsEnd:=InsOffset+InsSize;
  1192. end;
  1193. function taicpu.Matches(p:PInsEntry):boolean;
  1194. { * IF_SM stands for Size Match: any operand whose size is not
  1195. * explicitly specified by the template is `really' intended to be
  1196. * the same size as the first size-specified operand.
  1197. * Non-specification is tolerated in the input instruction, but
  1198. * _wrong_ specification is not.
  1199. *
  1200. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  1201. * three-operand instructions such as SHLD: it implies that the
  1202. * first two operands must match in size, but that the third is
  1203. * required to be _unspecified_.
  1204. *
  1205. * IF_SB invokes Size Byte: operands with unspecified size in the
  1206. * template are really bytes, and so no non-byte specification in
  1207. * the input instruction will be tolerated. IF_SW similarly invokes
  1208. * Size Word, and IF_SD invokes Size Doubleword.
  1209. *
  1210. * (The default state if neither IF_SM nor IF_SM2 is specified is
  1211. * that any operand with unspecified size in the template is
  1212. * required to have unspecified size in the instruction too...)
  1213. }
  1214. var
  1215. insot,
  1216. currot,
  1217. i,j,asize,oprs : longint;
  1218. insflags:cardinal;
  1219. siz : array[0..max_operands-1] of longint;
  1220. begin
  1221. result:=false;
  1222. { Check the opcode and operands }
  1223. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  1224. exit;
  1225. {$ifdef i8086}
  1226. { On i8086, we need to skip the i386+ version of Jcc near, if the target
  1227. cpu is earlier than 386. There's another entry, later in the table for
  1228. i8086, which simulates it with i8086 instructions:
  1229. JNcc short +3
  1230. JMP near target }
  1231. if (p^.opcode=A_Jcc) and (current_settings.cputype<cpu_386) and
  1232. ((p^.flags and IF_386)<>0) then
  1233. exit;
  1234. {$endif i8086}
  1235. for i:=0 to p^.ops-1 do
  1236. begin
  1237. insot:=p^.optypes[i];
  1238. currot:=oper[i]^.ot;
  1239. { Check the operand flags }
  1240. if (insot and (not currot) and OT_NON_SIZE)<>0 then
  1241. exit;
  1242. { Check if the passed operand size matches with one of
  1243. the supported operand sizes }
  1244. if ((insot and OT_SIZE_MASK)<>0) and
  1245. ((insot and currot and OT_SIZE_MASK)<>(currot and OT_SIZE_MASK)) then
  1246. exit;
  1247. { "far" matches only with "far" }
  1248. if (insot and OT_FAR)<>(currot and OT_FAR) then
  1249. exit;
  1250. end;
  1251. { Check operand sizes }
  1252. insflags:=p^.flags;
  1253. if insflags and IF_SMASK<>0 then
  1254. begin
  1255. { as default an untyped size can get all the sizes, this is different
  1256. from nasm, but else we need to do a lot checking which opcodes want
  1257. size or not with the automatic size generation }
  1258. asize:=-1;
  1259. if (insflags and IF_SB)<>0 then
  1260. asize:=OT_BITS8
  1261. else if (insflags and IF_SW)<>0 then
  1262. asize:=OT_BITS16
  1263. else if (insflags and IF_SD)<>0 then
  1264. asize:=OT_BITS32;
  1265. if (insflags and IF_ARMASK)<>0 then
  1266. begin
  1267. siz[0]:=-1;
  1268. siz[1]:=-1;
  1269. siz[2]:=-1;
  1270. siz[((insflags and IF_ARMASK) shr IF_ARSHIFT)-1]:=asize;
  1271. end
  1272. else
  1273. begin
  1274. siz[0]:=asize;
  1275. siz[1]:=asize;
  1276. siz[2]:=asize;
  1277. end;
  1278. if (insflags and (IF_SM or IF_SM2))<>0 then
  1279. begin
  1280. if (insflags and IF_SM2)<>0 then
  1281. oprs:=2
  1282. else
  1283. oprs:=p^.ops;
  1284. for i:=0 to oprs-1 do
  1285. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  1286. begin
  1287. for j:=0 to oprs-1 do
  1288. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  1289. break;
  1290. end;
  1291. end
  1292. else
  1293. oprs:=2;
  1294. { Check operand sizes }
  1295. for i:=0 to p^.ops-1 do
  1296. begin
  1297. insot:=p^.optypes[i];
  1298. currot:=oper[i]^.ot;
  1299. if ((insot and OT_SIZE_MASK)=0) and
  1300. ((currot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1301. { Immediates can always include smaller size }
  1302. ((currot and OT_IMMEDIATE)=0) and
  1303. (((insot and OT_SIZE_MASK) or siz[i])<(currot and OT_SIZE_MASK)) then
  1304. exit;
  1305. if (insot and OT_FAR)<>(currot and OT_FAR) then
  1306. exit;
  1307. end;
  1308. end;
  1309. if (InsTabMemRefSizeInfoCache^[opcode].MemRefSize in MemRefMultiples) and
  1310. (InsTabMemRefSizeInfoCache^[opcode].ExistsSSEAVX) then
  1311. begin
  1312. for i:=0 to p^.ops-1 do
  1313. begin
  1314. insot:=p^.optypes[i];
  1315. if ((insot and OT_XMMRM) = OT_XMMRM) OR
  1316. ((insot and OT_YMMRM) = OT_YMMRM) then
  1317. begin
  1318. if (insot and OT_SIZE_MASK) = 0 then
  1319. begin
  1320. case insot and (OT_XMMRM or OT_YMMRM) of
  1321. OT_XMMRM: insot := insot or OT_BITS128;
  1322. OT_YMMRM: insot := insot or OT_BITS256;
  1323. end;
  1324. end;
  1325. end;
  1326. currot:=oper[i]^.ot;
  1327. { Check the operand flags }
  1328. if (insot and (not currot) and OT_NON_SIZE)<>0 then
  1329. exit;
  1330. { Check if the passed operand size matches with one of
  1331. the supported operand sizes }
  1332. if ((insot and OT_SIZE_MASK)<>0) and
  1333. ((insot and currot and OT_SIZE_MASK)<>(currot and OT_SIZE_MASK)) then
  1334. exit;
  1335. end;
  1336. end;
  1337. result:=true;
  1338. end;
  1339. procedure taicpu.ResetPass1;
  1340. begin
  1341. { we need to reset everything here, because the choosen insentry
  1342. can be invalid for a new situation where the previously optimized
  1343. insentry is not correct }
  1344. InsEntry:=nil;
  1345. InsSize:=0;
  1346. LastInsOffset:=-1;
  1347. end;
  1348. procedure taicpu.ResetPass2;
  1349. begin
  1350. { we are here in a second pass, check if the instruction can be optimized }
  1351. if assigned(InsEntry) and
  1352. ((InsEntry^.flags and IF_PASS2)<>0) then
  1353. begin
  1354. InsEntry:=nil;
  1355. InsSize:=0;
  1356. end;
  1357. LastInsOffset:=-1;
  1358. end;
  1359. function taicpu.CheckIfValid:boolean;
  1360. begin
  1361. result:=FindInsEntry(nil);
  1362. end;
  1363. function taicpu.FindInsentry(objdata:TObjData):boolean;
  1364. var
  1365. i : longint;
  1366. begin
  1367. result:=false;
  1368. { Things which may only be done once, not when a second pass is done to
  1369. optimize }
  1370. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1371. begin
  1372. current_filepos:=fileinfo;
  1373. { We need intel style operands }
  1374. SetOperandOrder(op_intel);
  1375. { create the .ot fields }
  1376. create_ot(objdata);
  1377. { set the file postion }
  1378. end
  1379. else
  1380. begin
  1381. { we've already an insentry so it's valid }
  1382. result:=true;
  1383. exit;
  1384. end;
  1385. { Lookup opcode in the table }
  1386. InsSize:=-1;
  1387. i:=instabcache^[opcode];
  1388. if i=-1 then
  1389. begin
  1390. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1391. exit;
  1392. end;
  1393. insentry:=@instab[i];
  1394. while (insentry^.opcode=opcode) do
  1395. begin
  1396. if matches(insentry) then
  1397. begin
  1398. result:=true;
  1399. exit;
  1400. end;
  1401. inc(insentry);
  1402. end;
  1403. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1404. { No instruction found, set insentry to nil and inssize to -1 }
  1405. insentry:=nil;
  1406. inssize:=-1;
  1407. end;
  1408. function taicpu.Pass1(objdata:TObjData):longint;
  1409. begin
  1410. Pass1:=0;
  1411. { Save the old offset and set the new offset }
  1412. InsOffset:=ObjData.CurrObjSec.Size;
  1413. { Error? }
  1414. if (Insentry=nil) and (InsSize=-1) then
  1415. exit;
  1416. { set the file postion }
  1417. current_filepos:=fileinfo;
  1418. { Get InsEntry }
  1419. if FindInsEntry(ObjData) then
  1420. begin
  1421. { Calculate instruction size }
  1422. InsSize:=calcsize(insentry);
  1423. if segprefix<>NR_NO then
  1424. inc(InsSize);
  1425. { Fix opsize if size if forced }
  1426. if (insentry^.flags and (IF_SB or IF_SW or IF_SD))<>0 then
  1427. begin
  1428. if (insentry^.flags and IF_ARMASK)=0 then
  1429. begin
  1430. if (insentry^.flags and IF_SB)<>0 then
  1431. begin
  1432. if opsize=S_NO then
  1433. opsize:=S_B;
  1434. end
  1435. else if (insentry^.flags and IF_SW)<>0 then
  1436. begin
  1437. if opsize=S_NO then
  1438. opsize:=S_W;
  1439. end
  1440. else if (insentry^.flags and IF_SD)<>0 then
  1441. begin
  1442. if opsize=S_NO then
  1443. opsize:=S_L;
  1444. end;
  1445. end;
  1446. end;
  1447. LastInsOffset:=InsOffset;
  1448. Pass1:=InsSize;
  1449. exit;
  1450. end;
  1451. LastInsOffset:=-1;
  1452. end;
  1453. const
  1454. segprefixes: array[NR_ES..NR_GS] of Byte=(
  1455. // es cs ss ds fs gs
  1456. $26, $2E, $36, $3E, $64, $65
  1457. );
  1458. procedure taicpu.Pass2(objdata:TObjData);
  1459. begin
  1460. { error in pass1 ? }
  1461. if insentry=nil then
  1462. exit;
  1463. current_filepos:=fileinfo;
  1464. { Segment override }
  1465. if (segprefix>=NR_ES) and (segprefix<=NR_GS) then
  1466. begin
  1467. {$ifdef i8086}
  1468. if (objdata.CPUType<>cpu_none) and (objdata.CPUType<cpu_386) and
  1469. ((segprefix=NR_FS) or (segprefix=NR_GS)) then
  1470. Message(asmw_e_instruction_not_supported_by_cpu);
  1471. {$endif i8086}
  1472. objdata.writebytes(segprefixes[segprefix],1);
  1473. { fix the offset for GenNode }
  1474. inc(InsOffset);
  1475. end
  1476. else if segprefix<>NR_NO then
  1477. InternalError(201001071);
  1478. { Generate the instruction }
  1479. GenCode(objdata);
  1480. end;
  1481. function taicpu.needaddrprefix(opidx:byte):boolean;
  1482. begin
  1483. result:=(oper[opidx]^.typ=top_ref) and
  1484. (oper[opidx]^.ref^.refaddr=addr_no) and
  1485. {$ifdef x86_64}
  1486. (oper[opidx]^.ref^.base<>NR_RIP) and
  1487. {$endif x86_64}
  1488. (
  1489. (
  1490. (oper[opidx]^.ref^.index<>NR_NO) and
  1491. (getsubreg(oper[opidx]^.ref^.index)<>R_SUBADDR)
  1492. ) or
  1493. (
  1494. (oper[opidx]^.ref^.base<>NR_NO) and
  1495. (getsubreg(oper[opidx]^.ref^.base)<>R_SUBADDR)
  1496. )
  1497. );
  1498. end;
  1499. procedure badreg(r:Tregister);
  1500. begin
  1501. Message1(asmw_e_invalid_register,generic_regname(r));
  1502. end;
  1503. function regval(r:Tregister):byte;
  1504. const
  1505. intsupreg2opcode: array[0..7] of byte=
  1506. // ax cx dx bx si di bp sp -- in x86reg.dat
  1507. // ax cx dx bx sp bp si di -- needed order
  1508. (0, 1, 2, 3, 6, 7, 5, 4);
  1509. maxsupreg: array[tregistertype] of tsuperregister=
  1510. {$ifdef x86_64}
  1511. (0, 16, 9, 8, 16, 32, 0, 0);
  1512. {$else x86_64}
  1513. (0, 8, 9, 8, 8, 32, 0, 0);
  1514. {$endif x86_64}
  1515. var
  1516. rs: tsuperregister;
  1517. rt: tregistertype;
  1518. begin
  1519. rs:=getsupreg(r);
  1520. rt:=getregtype(r);
  1521. if (rs>=maxsupreg[rt]) then
  1522. badreg(r);
  1523. result:=rs and 7;
  1524. if (rt=R_INTREGISTER) then
  1525. begin
  1526. if (rs<8) then
  1527. result:=intsupreg2opcode[rs];
  1528. if getsubreg(r)=R_SUBH then
  1529. inc(result,4);
  1530. end;
  1531. end;
  1532. {$if defined(x86_64)}
  1533. function rexbits(r: tregister): byte;
  1534. begin
  1535. result:=0;
  1536. case getregtype(r) of
  1537. R_INTREGISTER:
  1538. if (getsupreg(r)>=RS_R8) then
  1539. { Either B,X or R bits can be set, depending on register role in instruction.
  1540. Set all three bits here, caller will discard unnecessary ones. }
  1541. result:=result or $47
  1542. else if (getsubreg(r)=R_SUBL) and
  1543. (getsupreg(r) in [RS_RDI,RS_RSI,RS_RBP,RS_RSP]) then
  1544. result:=result or $40
  1545. else if (getsubreg(r)=R_SUBH) then
  1546. { Not an actual REX bit, used to detect incompatible usage of
  1547. AH/BH/CH/DH }
  1548. result:=result or $80;
  1549. R_MMREGISTER:
  1550. if getsupreg(r)>=RS_XMM8 then
  1551. result:=result or $47;
  1552. end;
  1553. end;
  1554. function process_ea_ref(const input:toper;var output:ea;rfield:longint):boolean;
  1555. var
  1556. sym : tasmsymbol;
  1557. md,s : byte;
  1558. base,index,scalefactor,
  1559. o : longint;
  1560. ir,br : Tregister;
  1561. isub,bsub : tsubregister;
  1562. begin
  1563. result:=false;
  1564. ir:=input.ref^.index;
  1565. br:=input.ref^.base;
  1566. isub:=getsubreg(ir);
  1567. bsub:=getsubreg(br);
  1568. s:=input.ref^.scalefactor;
  1569. o:=input.ref^.offset;
  1570. sym:=input.ref^.symbol;
  1571. //if ((ir<>NR_NO) and (getregtype(ir)<>R_INTREGISTER)) or
  1572. // ((br<>NR_NO) and (br<>NR_RIP) and (getregtype(br)<>R_INTREGISTER)) then
  1573. if ((ir<>NR_NO) and (getregtype(ir)=R_MMREGISTER) and (br<>NR_NO) and (getregtype(br)<>R_INTREGISTER)) or // vector memory (AVX2)
  1574. ((ir<>NR_NO) and (getregtype(ir)<>R_INTREGISTER) and (getregtype(ir)<>R_MMREGISTER)) or
  1575. ((br<>NR_NO) and (br<>NR_RIP) and (getregtype(br)<>R_INTREGISTER)) then
  1576. internalerror(200301081);
  1577. { it's direct address }
  1578. if (br=NR_NO) and (ir=NR_NO) then
  1579. begin
  1580. output.sib_present:=true;
  1581. output.bytes:=4;
  1582. output.modrm:=4 or (rfield shl 3);
  1583. output.sib:=$25;
  1584. end
  1585. else if (br=NR_RIP) and (ir=NR_NO) then
  1586. begin
  1587. { rip based }
  1588. output.sib_present:=false;
  1589. output.bytes:=4;
  1590. output.modrm:=5 or (rfield shl 3);
  1591. end
  1592. else
  1593. { it's an indirection }
  1594. begin
  1595. { 16 bit? }
  1596. if ((ir<>NR_NO) and (isub in [R_SUBMMX,R_SUBMMY]) and
  1597. (br<>NR_NO) and (bsub=R_SUBADDR)
  1598. ) then
  1599. begin
  1600. // vector memory (AVX2) =>> ignore
  1601. end
  1602. else if ((ir<>NR_NO) and (isub<>R_SUBADDR) and (isub<>R_SUBD)) or
  1603. ((br<>NR_NO) and (bsub<>R_SUBADDR) and (bsub<>R_SUBD)) then
  1604. begin
  1605. message(asmw_e_16bit_32bit_not_supported);
  1606. end;
  1607. { wrong, for various reasons }
  1608. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1609. exit;
  1610. output.rex:=output.rex or (rexbits(br) and $F1) or (rexbits(ir) and $F2);
  1611. result:=true;
  1612. { base }
  1613. case br of
  1614. NR_R8D,
  1615. NR_EAX,
  1616. NR_R8,
  1617. NR_RAX : base:=0;
  1618. NR_R9D,
  1619. NR_ECX,
  1620. NR_R9,
  1621. NR_RCX : base:=1;
  1622. NR_R10D,
  1623. NR_EDX,
  1624. NR_R10,
  1625. NR_RDX : base:=2;
  1626. NR_R11D,
  1627. NR_EBX,
  1628. NR_R11,
  1629. NR_RBX : base:=3;
  1630. NR_R12D,
  1631. NR_ESP,
  1632. NR_R12,
  1633. NR_RSP : base:=4;
  1634. NR_R13D,
  1635. NR_EBP,
  1636. NR_R13,
  1637. NR_NO,
  1638. NR_RBP : base:=5;
  1639. NR_R14D,
  1640. NR_ESI,
  1641. NR_R14,
  1642. NR_RSI : base:=6;
  1643. NR_R15D,
  1644. NR_EDI,
  1645. NR_R15,
  1646. NR_RDI : base:=7;
  1647. else
  1648. exit;
  1649. end;
  1650. { index }
  1651. case ir of
  1652. NR_R8D,
  1653. NR_EAX,
  1654. NR_R8,
  1655. NR_RAX,
  1656. NR_XMM0,
  1657. NR_XMM8,
  1658. NR_YMM0,
  1659. NR_YMM8 : index:=0;
  1660. NR_R9D,
  1661. NR_ECX,
  1662. NR_R9,
  1663. NR_RCX,
  1664. NR_XMM1,
  1665. NR_XMM9,
  1666. NR_YMM1,
  1667. NR_YMM9 : index:=1;
  1668. NR_R10D,
  1669. NR_EDX,
  1670. NR_R10,
  1671. NR_RDX,
  1672. NR_XMM2,
  1673. NR_XMM10,
  1674. NR_YMM2,
  1675. NR_YMM10 : index:=2;
  1676. NR_R11D,
  1677. NR_EBX,
  1678. NR_R11,
  1679. NR_RBX,
  1680. NR_XMM3,
  1681. NR_XMM11,
  1682. NR_YMM3,
  1683. NR_YMM11 : index:=3;
  1684. NR_R12D,
  1685. NR_ESP,
  1686. NR_R12,
  1687. NR_NO,
  1688. NR_XMM4,
  1689. NR_XMM12,
  1690. NR_YMM4,
  1691. NR_YMM12 : index:=4;
  1692. NR_R13D,
  1693. NR_EBP,
  1694. NR_R13,
  1695. NR_RBP,
  1696. NR_XMM5,
  1697. NR_XMM13,
  1698. NR_YMM5,
  1699. NR_YMM13: index:=5;
  1700. NR_R14D,
  1701. NR_ESI,
  1702. NR_R14,
  1703. NR_RSI,
  1704. NR_XMM6,
  1705. NR_XMM14,
  1706. NR_YMM6,
  1707. NR_YMM14: index:=6;
  1708. NR_R15D,
  1709. NR_EDI,
  1710. NR_R15,
  1711. NR_RDI,
  1712. NR_XMM7,
  1713. NR_XMM15,
  1714. NR_YMM7,
  1715. NR_YMM15: index:=7;
  1716. else
  1717. exit;
  1718. end;
  1719. case s of
  1720. 0,
  1721. 1 : scalefactor:=0;
  1722. 2 : scalefactor:=1;
  1723. 4 : scalefactor:=2;
  1724. 8 : scalefactor:=3;
  1725. else
  1726. exit;
  1727. end;
  1728. { If rbp or r13 is used we must always include an offset }
  1729. if (br=NR_NO) or
  1730. ((br<>NR_RBP) and (br<>NR_R13) and (br<>NR_EBP) and (br<>NR_R13D) and (o=0) and (sym=nil)) then
  1731. md:=0
  1732. else
  1733. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1734. md:=1
  1735. else
  1736. md:=2;
  1737. if (br=NR_NO) or (md=2) then
  1738. output.bytes:=4
  1739. else
  1740. output.bytes:=md;
  1741. { SIB needed ? }
  1742. if (ir=NR_NO) and (br<>NR_RSP) and (br<>NR_R12) and (br<>NR_ESP) and (br<>NR_R12D) then
  1743. begin
  1744. output.sib_present:=false;
  1745. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1746. end
  1747. else
  1748. begin
  1749. output.sib_present:=true;
  1750. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1751. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1752. end;
  1753. end;
  1754. output.size:=1+ord(output.sib_present)+output.bytes;
  1755. result:=true;
  1756. end;
  1757. {$elseif defined(i386)}
  1758. function process_ea_ref(const input:toper;out output:ea;rfield:longint):boolean;
  1759. var
  1760. sym : tasmsymbol;
  1761. md,s : byte;
  1762. base,index,scalefactor,
  1763. o : longint;
  1764. ir,br : Tregister;
  1765. isub,bsub : tsubregister;
  1766. begin
  1767. result:=false;
  1768. if ((input.ref^.index<>NR_NO) and (getregtype(input.ref^.index)=R_MMREGISTER) and (input.ref^.base<>NR_NO) and (getregtype(input.ref^.base)<>R_INTREGISTER)) or // vector memory (AVX2)
  1769. ((input.ref^.index<>NR_NO) and (getregtype(input.ref^.index)<>R_INTREGISTER) and (getregtype(input.ref^.index)<>R_MMREGISTER)) or
  1770. ((input.ref^.base<>NR_NO) and (getregtype(input.ref^.base)<>R_INTREGISTER)) then
  1771. internalerror(200301081);
  1772. ir:=input.ref^.index;
  1773. br:=input.ref^.base;
  1774. isub:=getsubreg(ir);
  1775. bsub:=getsubreg(br);
  1776. s:=input.ref^.scalefactor;
  1777. o:=input.ref^.offset;
  1778. sym:=input.ref^.symbol;
  1779. { it's direct address }
  1780. if (br=NR_NO) and (ir=NR_NO) then
  1781. begin
  1782. { it's a pure offset }
  1783. output.sib_present:=false;
  1784. output.bytes:=4;
  1785. output.modrm:=5 or (rfield shl 3);
  1786. end
  1787. else
  1788. { it's an indirection }
  1789. begin
  1790. { 16 bit address? }
  1791. if ((ir<>NR_NO) and (isub in [R_SUBMMX,R_SUBMMY]) and
  1792. (br<>NR_NO) and (bsub=R_SUBADDR)
  1793. ) then
  1794. begin
  1795. // vector memory (AVX2) =>> ignore
  1796. end
  1797. else if ((ir<>NR_NO) and (isub<>R_SUBADDR)) or
  1798. ((br<>NR_NO) and (bsub<>R_SUBADDR)) then
  1799. message(asmw_e_16bit_not_supported);
  1800. {$ifdef OPTEA}
  1801. { make single reg base }
  1802. if (br=NR_NO) and (s=1) then
  1803. begin
  1804. br:=ir;
  1805. ir:=NR_NO;
  1806. end;
  1807. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  1808. if (br=NR_NO) and
  1809. (((s=2) and (ir<>NR_ESP)) or
  1810. (s=3) or (s=5) or (s=9)) then
  1811. begin
  1812. br:=ir;
  1813. dec(s);
  1814. end;
  1815. { swap ESP into base if scalefactor is 1 }
  1816. if (s=1) and (ir=NR_ESP) then
  1817. begin
  1818. ir:=br;
  1819. br:=NR_ESP;
  1820. end;
  1821. {$endif OPTEA}
  1822. { wrong, for various reasons }
  1823. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1824. exit;
  1825. { base }
  1826. case br of
  1827. NR_EAX : base:=0;
  1828. NR_ECX : base:=1;
  1829. NR_EDX : base:=2;
  1830. NR_EBX : base:=3;
  1831. NR_ESP : base:=4;
  1832. NR_NO,
  1833. NR_EBP : base:=5;
  1834. NR_ESI : base:=6;
  1835. NR_EDI : base:=7;
  1836. else
  1837. exit;
  1838. end;
  1839. { index }
  1840. case ir of
  1841. NR_EAX,
  1842. NR_XMM0,
  1843. NR_YMM0: index:=0;
  1844. NR_ECX,
  1845. NR_XMM1,
  1846. NR_YMM1: index:=1;
  1847. NR_EDX,
  1848. NR_XMM2,
  1849. NR_YMM2: index:=2;
  1850. NR_EBX,
  1851. NR_XMM3,
  1852. NR_YMM3: index:=3;
  1853. NR_NO,
  1854. NR_XMM4,
  1855. NR_YMM4: index:=4;
  1856. NR_EBP,
  1857. NR_XMM5,
  1858. NR_YMM5: index:=5;
  1859. NR_ESI,
  1860. NR_XMM6,
  1861. NR_YMM6: index:=6;
  1862. NR_EDI,
  1863. NR_XMM7,
  1864. NR_YMM7: index:=7;
  1865. else
  1866. exit;
  1867. end;
  1868. case s of
  1869. 0,
  1870. 1 : scalefactor:=0;
  1871. 2 : scalefactor:=1;
  1872. 4 : scalefactor:=2;
  1873. 8 : scalefactor:=3;
  1874. else
  1875. exit;
  1876. end;
  1877. if (br=NR_NO) or
  1878. ((br<>NR_EBP) and (o=0) and (sym=nil)) then
  1879. md:=0
  1880. else
  1881. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1882. md:=1
  1883. else
  1884. md:=2;
  1885. if (br=NR_NO) or (md=2) then
  1886. output.bytes:=4
  1887. else
  1888. output.bytes:=md;
  1889. { SIB needed ? }
  1890. if (ir=NR_NO) and (br<>NR_ESP) then
  1891. begin
  1892. output.sib_present:=false;
  1893. output.modrm:=(longint(md) shl 6) or (rfield shl 3) or base;
  1894. end
  1895. else
  1896. begin
  1897. output.sib_present:=true;
  1898. output.modrm:=(longint(md) shl 6) or (rfield shl 3) or 4;
  1899. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1900. end;
  1901. end;
  1902. if output.sib_present then
  1903. output.size:=2+output.bytes
  1904. else
  1905. output.size:=1+output.bytes;
  1906. result:=true;
  1907. end;
  1908. {$elseif defined(i8086)}
  1909. procedure maybe_swap_index_base(var br,ir:Tregister);
  1910. var
  1911. tmpreg: Tregister;
  1912. begin
  1913. if ((br=NR_NO) or (br=NR_SI) or (br=NR_DI)) and
  1914. ((ir=NR_NO) or (ir=NR_BP) or (ir=NR_BX)) then
  1915. begin
  1916. tmpreg:=br;
  1917. br:=ir;
  1918. ir:=tmpreg;
  1919. end;
  1920. end;
  1921. function process_ea_ref(const input:toper;out output:ea;rfield:longint):boolean;
  1922. var
  1923. sym : tasmsymbol;
  1924. md,s,rv : byte;
  1925. base,
  1926. o : longint;
  1927. ir,br : Tregister;
  1928. isub,bsub : tsubregister;
  1929. begin
  1930. result:=false;
  1931. if ((input.ref^.index<>NR_NO) and (getregtype(input.ref^.index)<>R_INTREGISTER)) or
  1932. ((input.ref^.base<>NR_NO) and (getregtype(input.ref^.base)<>R_INTREGISTER)) then
  1933. internalerror(200301081);
  1934. ir:=input.ref^.index;
  1935. br:=input.ref^.base;
  1936. isub:=getsubreg(ir);
  1937. bsub:=getsubreg(br);
  1938. s:=input.ref^.scalefactor;
  1939. o:=input.ref^.offset;
  1940. sym:=input.ref^.symbol;
  1941. { it's a direct address }
  1942. if (br=NR_NO) and (ir=NR_NO) then
  1943. begin
  1944. { it's a pure offset }
  1945. output.bytes:=2;
  1946. output.modrm:=6 or (rfield shl 3);
  1947. end
  1948. else
  1949. { it's an indirection }
  1950. begin
  1951. { 32 bit address? }
  1952. if ((ir<>NR_NO) and (isub<>R_SUBADDR)) or
  1953. ((br<>NR_NO) and (bsub<>R_SUBADDR)) then
  1954. message(asmw_e_32bit_not_supported);
  1955. { scalefactor can only be 1 in 16-bit addresses }
  1956. if (s<>1) and (ir<>NR_NO) then
  1957. exit;
  1958. maybe_swap_index_base(br,ir);
  1959. if (br=NR_BX) and (ir=NR_SI) then
  1960. base:=0
  1961. else if (br=NR_BX) and (ir=NR_DI) then
  1962. base:=1
  1963. else if (br=NR_BP) and (ir=NR_SI) then
  1964. base:=2
  1965. else if (br=NR_BP) and (ir=NR_DI) then
  1966. base:=3
  1967. else if (br=NR_NO) and (ir=NR_SI) then
  1968. base:=4
  1969. else if (br=NR_NO) and (ir=NR_DI) then
  1970. base:=5
  1971. else if (br=NR_BP) and (ir=NR_NO) then
  1972. base:=6
  1973. else if (br=NR_BX) and (ir=NR_NO) then
  1974. base:=7
  1975. else
  1976. exit;
  1977. if (base<>6) and (o=0) and (sym=nil) then
  1978. md:=0
  1979. else if ((o>=-128) and (o<=127) and (sym=nil)) then
  1980. md:=1
  1981. else
  1982. md:=2;
  1983. output.bytes:=md;
  1984. output.modrm:=(longint(md) shl 6) or (rfield shl 3) or base;
  1985. end;
  1986. output.size:=1+output.bytes;
  1987. output.sib_present:=false;
  1988. result:=true;
  1989. end;
  1990. {$endif}
  1991. function process_ea(const input:toper;out output:ea;rfield:longint):boolean;
  1992. var
  1993. rv : byte;
  1994. begin
  1995. result:=false;
  1996. fillchar(output,sizeof(output),0);
  1997. {Register ?}
  1998. if (input.typ=top_reg) then
  1999. begin
  2000. rv:=regval(input.reg);
  2001. output.modrm:=$c0 or (rfield shl 3) or rv;
  2002. output.size:=1;
  2003. {$ifdef x86_64}
  2004. output.rex:=output.rex or (rexbits(input.reg) and $F1);
  2005. {$endif x86_64}
  2006. result:=true;
  2007. exit;
  2008. end;
  2009. {No register, so memory reference.}
  2010. if input.typ<>top_ref then
  2011. internalerror(200409263);
  2012. result:=process_ea_ref(input,output,rfield);
  2013. end;
  2014. function taicpu.calcsize(p:PInsEntry):shortint;
  2015. var
  2016. codes : pchar;
  2017. c : byte;
  2018. len : shortint;
  2019. ea_data : ea;
  2020. exists_vex: boolean;
  2021. exists_vex_extension: boolean;
  2022. exists_prefix_66: boolean;
  2023. exists_prefix_F2: boolean;
  2024. exists_prefix_F3: boolean;
  2025. {$ifdef x86_64}
  2026. omit_rexw : boolean;
  2027. {$endif x86_64}
  2028. begin
  2029. len:=0;
  2030. codes:=@p^.code[0];
  2031. exists_vex := false;
  2032. exists_vex_extension := false;
  2033. exists_prefix_66 := false;
  2034. exists_prefix_F2 := false;
  2035. exists_prefix_F3 := false;
  2036. {$ifdef x86_64}
  2037. rex:=0;
  2038. omit_rexw:=false;
  2039. {$endif x86_64}
  2040. repeat
  2041. c:=ord(codes^);
  2042. inc(codes);
  2043. case c of
  2044. &0 :
  2045. break;
  2046. &1,&2,&3 :
  2047. begin
  2048. inc(codes,c);
  2049. inc(len,c);
  2050. end;
  2051. &10,&11,&12 :
  2052. begin
  2053. {$ifdef x86_64}
  2054. rex:=rex or (rexbits(oper[c-&10]^.reg) and $F1);
  2055. {$endif x86_64}
  2056. inc(codes);
  2057. inc(len);
  2058. end;
  2059. &13,&23 :
  2060. begin
  2061. inc(codes);
  2062. inc(len);
  2063. end;
  2064. &4,&5,&6,&7 :
  2065. begin
  2066. if opsize={$ifdef i8086}S_L{$else}S_W{$endif} then
  2067. inc(len,2)
  2068. else
  2069. inc(len);
  2070. end;
  2071. &14,&15,&16,
  2072. &20,&21,&22,
  2073. &24,&25,&26,&27,
  2074. &50,&51,&52 :
  2075. inc(len);
  2076. &30,&31,&32,
  2077. &37,
  2078. &60,&61,&62 :
  2079. inc(len,2);
  2080. &34,&35,&36:
  2081. begin
  2082. {$ifdef i8086}
  2083. inc(len,2);
  2084. {$else i8086}
  2085. if opsize=S_Q then
  2086. inc(len,8)
  2087. else
  2088. inc(len,4);
  2089. {$endif i8086}
  2090. end;
  2091. &44,&45,&46:
  2092. inc(len,sizeof(pint));
  2093. &54,&55,&56:
  2094. inc(len,8);
  2095. &40,&41,&42,
  2096. &70,&71,&72,
  2097. &254,&255,&256 :
  2098. inc(len,4);
  2099. &64,&65,&66:
  2100. {$ifdef i8086}
  2101. inc(len,2);
  2102. {$else i8086}
  2103. inc(len,4);
  2104. {$endif i8086}
  2105. &74,&75,&76,&77: ; // ignore vex-coded operand-idx
  2106. &320,&321,&322 :
  2107. begin
  2108. case (oper[c-&320]^.ot and OT_SIZE_MASK) of
  2109. {$if defined(i386) or defined(x86_64)}
  2110. OT_BITS16 :
  2111. {$elseif defined(i8086)}
  2112. OT_BITS32 :
  2113. {$endif}
  2114. inc(len);
  2115. {$ifdef x86_64}
  2116. OT_BITS64:
  2117. begin
  2118. rex:=rex or $48;
  2119. end;
  2120. {$endif x86_64}
  2121. end;
  2122. end;
  2123. &310 :
  2124. {$if defined(x86_64)}
  2125. { every insentry with code 0310 must be marked with NOX86_64 }
  2126. InternalError(2011051301);
  2127. {$elseif defined(i386)}
  2128. inc(len);
  2129. {$elseif defined(i8086)}
  2130. {nothing};
  2131. {$endif}
  2132. &311 :
  2133. {$if defined(x86_64) or defined(i8086)}
  2134. inc(len)
  2135. {$endif x86_64 or i8086}
  2136. ;
  2137. &324 :
  2138. {$ifndef i8086}
  2139. inc(len)
  2140. {$endif not i8086}
  2141. ;
  2142. &326 :
  2143. begin
  2144. {$ifdef x86_64}
  2145. rex:=rex or $48;
  2146. {$endif x86_64}
  2147. end;
  2148. &312,
  2149. &323,
  2150. &327,
  2151. &331,&332: ;
  2152. &325:
  2153. {$ifdef i8086}
  2154. inc(len)
  2155. {$endif i8086}
  2156. ;
  2157. &333:
  2158. begin
  2159. inc(len);
  2160. exists_prefix_F2 := true;
  2161. end;
  2162. &334:
  2163. begin
  2164. inc(len);
  2165. exists_prefix_F3 := true;
  2166. end;
  2167. &361:
  2168. begin
  2169. {$ifndef i8086}
  2170. inc(len);
  2171. exists_prefix_66 := true;
  2172. {$endif not i8086}
  2173. end;
  2174. &335:
  2175. {$ifdef x86_64}
  2176. omit_rexw:=true
  2177. {$endif x86_64}
  2178. ;
  2179. &100..&227 :
  2180. begin
  2181. {$ifdef x86_64}
  2182. if (c<&177) then
  2183. begin
  2184. if (oper[c and 7]^.typ=top_reg) then
  2185. begin
  2186. rex:=rex or (rexbits(oper[c and 7]^.reg) and $F4);
  2187. end;
  2188. end;
  2189. {$endif x86_64}
  2190. if not process_ea(oper[(c shr 3) and 7]^, ea_data, 0) then
  2191. Message(asmw_e_invalid_effective_address)
  2192. else
  2193. inc(len,ea_data.size);
  2194. {$ifdef x86_64}
  2195. rex:=rex or ea_data.rex;
  2196. {$endif x86_64}
  2197. end;
  2198. &362: // VEX prefix for AVX (length = 2 or 3 bytes, dependens on REX.XBW or opcode-prefix ($0F38 or $0F3A))
  2199. // =>> DEFAULT = 2 Bytes
  2200. begin
  2201. if not(exists_vex) then
  2202. begin
  2203. inc(len, 2);
  2204. exists_vex := true;
  2205. end;
  2206. end;
  2207. &363: // REX.W = 1
  2208. // =>> VEX prefix length = 3
  2209. begin
  2210. if not(exists_vex_extension) then
  2211. begin
  2212. inc(len);
  2213. exists_vex_extension := true;
  2214. end;
  2215. end;
  2216. &364: ; // VEX length bit
  2217. &366, // operand 2 (ymmreg) encoded immediate byte (bit 4-7)
  2218. &367: inc(len); // operand 3 (ymmreg) encoded immediate byte (bit 4-7)
  2219. &370: // VEX-Extension prefix $0F
  2220. // ignore for calculating length
  2221. ;
  2222. &371, // VEX-Extension prefix $0F38
  2223. &372: // VEX-Extension prefix $0F3A
  2224. begin
  2225. if not(exists_vex_extension) then
  2226. begin
  2227. inc(len);
  2228. exists_vex_extension := true;
  2229. end;
  2230. end;
  2231. &300,&301,&302:
  2232. begin
  2233. {$if defined(x86_64) or defined(i8086)}
  2234. if (oper[c and 3]^.ot and OT_SIZE_MASK)=OT_BITS32 then
  2235. inc(len);
  2236. {$endif x86_64 or i8086}
  2237. end;
  2238. else
  2239. InternalError(200603141);
  2240. end;
  2241. until false;
  2242. {$ifdef x86_64}
  2243. if ((rex and $80)<>0) and ((rex and $4F)<>0) then
  2244. Message(asmw_e_bad_reg_with_rex);
  2245. rex:=rex and $4F; { reset extra bits in upper nibble }
  2246. if omit_rexw then
  2247. begin
  2248. if rex=$48 then { remove rex entirely? }
  2249. rex:=0
  2250. else
  2251. rex:=rex and $F7;
  2252. end;
  2253. if not(exists_vex) then
  2254. begin
  2255. if rex<>0 then
  2256. Inc(len);
  2257. end;
  2258. {$endif}
  2259. if exists_vex then
  2260. begin
  2261. if exists_prefix_66 then dec(len);
  2262. if exists_prefix_F2 then dec(len);
  2263. if exists_prefix_F3 then dec(len);
  2264. {$ifdef x86_64}
  2265. if not(exists_vex_extension) then
  2266. if rex and $0B <> 0 then inc(len); // REX.WXB <> 0 =>> needed VEX-Extension
  2267. {$endif x86_64}
  2268. end;
  2269. calcsize:=len;
  2270. end;
  2271. procedure taicpu.GenCode(objdata:TObjData);
  2272. {
  2273. * the actual codes (C syntax, i.e. octal):
  2274. * \0 - terminates the code. (Unless it's a literal of course.)
  2275. * \1, \2, \3 - that many literal bytes follow in the code stream
  2276. * \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
  2277. * (POP is never used for CS) depending on operand 0
  2278. * \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
  2279. * on operand 0
  2280. * \10, \11, \12 - a literal byte follows in the code stream, to be added
  2281. * to the register value of operand 0, 1 or 2
  2282. * \13 - a literal byte follows in the code stream, to be added
  2283. * to the condition code value of the instruction.
  2284. * \14, \15, \16 - a signed byte immediate operand, from operand 0, 1 or 2
  2285. * \20, \21, \22 - a byte immediate operand, from operand 0, 1 or 2
  2286. * \23 - a literal byte follows in the code stream, to be added
  2287. * to the inverted condition code value of the instruction
  2288. * (inverted version of \13).
  2289. * \24, \25, \26, \27 - an unsigned byte immediate operand, from operand 0, 1, 2 or 3
  2290. * \30, \31, \32 - a word immediate operand, from operand 0, 1 or 2
  2291. * \34, \35, \36 - select between \3[012] and \4[012] depending on 16/32 bit
  2292. * assembly mode or the address-size override on the operand
  2293. * \37 - a word constant, from the _segment_ part of operand 0
  2294. * \40, \41, \42 - a long immediate operand, from operand 0, 1 or 2
  2295. * \44, \45, \46 - select between \3[012], \4[012] or \5[456] depending
  2296. on the address size of instruction
  2297. * \50, \51, \52 - a byte relative operand, from operand 0, 1 or 2
  2298. * \54, \55, \56 - a qword immediate, from operand 0, 1 or 2
  2299. * \60, \61, \62 - a word relative operand, from operand 0, 1 or 2
  2300. * \64, \65, \66 - select between \6[012] and \7[012] depending on 16/32 bit
  2301. * assembly mode or the address-size override on the operand
  2302. * \70, \71, \72 - a long relative operand, from operand 0, 1 or 2
  2303. * \74, \75, \76 - a vex-coded vector operand, from operand 0, 1 or 2
  2304. * \1ab - a ModRM, calculated on EA in operand a, with the spare
  2305. * field the register value of operand b.
  2306. * \2ab - a ModRM, calculated on EA in operand a, with the spare
  2307. * field equal to digit b.
  2308. * \254,\255,\256 - a signed 32-bit immediate to be extended to 64 bits
  2309. * \300,\301,\302 - might be an 0x67, depending on the address size of
  2310. * the memory reference in operand x.
  2311. * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
  2312. * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
  2313. * \312 - (disassembler only) invalid with non-default address size.
  2314. * \320,\321,\322 - might be an 0x66 or 0x48 byte, depending on the operand
  2315. * size of operand x.
  2316. * \324 - indicates fixed 16-bit operand size, i.e. optional 0x66.
  2317. * \325 - indicates fixed 32-bit operand size, i.e. optional 0x66.
  2318. * \326 - indicates fixed 64-bit operand size, i.e. optional 0x48.
  2319. * \327 - indicates that this instruction is only valid when the
  2320. * operand size is the default (instruction to disassembler,
  2321. * generates no code in the assembler)
  2322. * \331 - instruction not valid with REP prefix. Hint for
  2323. * disassembler only; for SSE instructions.
  2324. * \332 - disassemble a rep (0xF3 byte) prefix as repe not rep.
  2325. * \333 - 0xF3 prefix for SSE instructions
  2326. * \334 - 0xF2 prefix for SSE instructions
  2327. * \335 - Indicates 64-bit operand size with REX.W not necessary
  2328. * \361 - 0x66 prefix for SSE instructions
  2329. * \362 - VEX prefix for AVX instructions
  2330. * \363 - VEX W1
  2331. * \364 - VEX Vector length 256
  2332. * \366 - operand 2 (ymmreg) encoded in bit 4-7 of the immediate byte
  2333. * \367 - operand 3 (ymmreg) encoded in bit 4-7 of the immediate byte
  2334. * \370 - VEX 0F-FLAG
  2335. * \371 - VEX 0F38-FLAG
  2336. * \372 - VEX 0F3A-FLAG
  2337. }
  2338. var
  2339. currval : aint;
  2340. currsym : tobjsymbol;
  2341. currrelreloc,
  2342. currabsreloc,
  2343. currabsreloc32 : TObjRelocationType;
  2344. {$ifdef x86_64}
  2345. rexwritten : boolean;
  2346. {$endif x86_64}
  2347. procedure getvalsym(opidx:longint);
  2348. begin
  2349. case oper[opidx]^.typ of
  2350. top_ref :
  2351. begin
  2352. currval:=oper[opidx]^.ref^.offset;
  2353. currsym:=ObjData.symbolref(oper[opidx]^.ref^.symbol);
  2354. {$ifdef i8086}
  2355. if oper[opidx]^.ref^.refaddr=addr_seg then
  2356. begin
  2357. currrelreloc:=RELOC_SEGREL;
  2358. currabsreloc:=RELOC_SEG;
  2359. currabsreloc32:=RELOC_SEG;
  2360. end
  2361. else if oper[opidx]^.ref^.refaddr=addr_dgroup then
  2362. begin
  2363. currrelreloc:=RELOC_DGROUPREL;
  2364. currabsreloc:=RELOC_DGROUP;
  2365. currabsreloc32:=RELOC_DGROUP;
  2366. end
  2367. else if oper[opidx]^.ref^.refaddr=addr_fardataseg then
  2368. begin
  2369. currrelreloc:=RELOC_FARDATASEGREL;
  2370. currabsreloc:=RELOC_FARDATASEG;
  2371. currabsreloc32:=RELOC_FARDATASEG;
  2372. end
  2373. else
  2374. {$endif i8086}
  2375. {$ifdef i386}
  2376. if (oper[opidx]^.ref^.refaddr=addr_pic) and
  2377. (tf_pic_uses_got in target_info.flags) then
  2378. begin
  2379. currrelreloc:=RELOC_PLT32;
  2380. currabsreloc:=RELOC_GOT32;
  2381. currabsreloc32:=RELOC_GOT32;
  2382. end
  2383. else
  2384. {$endif i386}
  2385. {$ifdef x86_64}
  2386. if oper[opidx]^.ref^.refaddr=addr_pic then
  2387. begin
  2388. currrelreloc:=RELOC_PLT32;
  2389. currabsreloc:=RELOC_GOTPCREL;
  2390. currabsreloc32:=RELOC_GOTPCREL;
  2391. end
  2392. else if oper[opidx]^.ref^.refaddr=addr_pic_no_got then
  2393. begin
  2394. currrelreloc:=RELOC_RELATIVE;
  2395. currabsreloc:=RELOC_RELATIVE;
  2396. currabsreloc32:=RELOC_RELATIVE;
  2397. end
  2398. else
  2399. {$endif x86_64}
  2400. begin
  2401. currrelreloc:=RELOC_RELATIVE;
  2402. currabsreloc:=RELOC_ABSOLUTE;
  2403. currabsreloc32:=RELOC_ABSOLUTE32;
  2404. end;
  2405. end;
  2406. top_const :
  2407. begin
  2408. currval:=aint(oper[opidx]^.val);
  2409. currsym:=nil;
  2410. currabsreloc:=RELOC_ABSOLUTE;
  2411. currabsreloc32:=RELOC_ABSOLUTE32;
  2412. end;
  2413. else
  2414. Message(asmw_e_immediate_or_reference_expected);
  2415. end;
  2416. end;
  2417. {$ifdef x86_64}
  2418. procedure maybewriterex;
  2419. begin
  2420. if (rex<>0) and not(rexwritten) then
  2421. begin
  2422. rexwritten:=true;
  2423. objdata.writebytes(rex,1);
  2424. end;
  2425. end;
  2426. {$endif x86_64}
  2427. procedure write0x66prefix;
  2428. const
  2429. b66: Byte=$66;
  2430. begin
  2431. {$ifdef i8086}
  2432. if (objdata.CPUType<>cpu_none) and (objdata.CPUType<cpu_386) then
  2433. Message(asmw_e_instruction_not_supported_by_cpu);
  2434. {$endif i8086}
  2435. objdata.writebytes(b66,1);
  2436. end;
  2437. procedure write0x67prefix;
  2438. const
  2439. b67: Byte=$67;
  2440. begin
  2441. {$ifdef i8086}
  2442. if (objdata.CPUType<>cpu_none) and (objdata.CPUType<cpu_386) then
  2443. Message(asmw_e_instruction_not_supported_by_cpu);
  2444. {$endif i8086}
  2445. objdata.writebytes(b67,1);
  2446. end;
  2447. procedure objdata_writereloc(Data:TRelocDataInt;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);
  2448. begin
  2449. {$ifdef i386}
  2450. { Special case of '_GLOBAL_OFFSET_TABLE_'
  2451. which needs a special relocation type R_386_GOTPC }
  2452. if assigned (p) and
  2453. (p.name='_GLOBAL_OFFSET_TABLE_') and
  2454. (tf_pic_uses_got in target_info.flags) then
  2455. begin
  2456. { nothing else than a 4 byte relocation should occur
  2457. for GOT }
  2458. if len<>4 then
  2459. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2460. Reloctype:=RELOC_GOTPC;
  2461. { We need to add the offset of the relocation
  2462. of _GLOBAL_OFFSET_TABLE symbol within
  2463. the current instruction }
  2464. inc(data,objdata.currobjsec.size-insoffset);
  2465. end;
  2466. {$endif i386}
  2467. objdata.writereloc(data,len,p,Reloctype);
  2468. end;
  2469. const
  2470. CondVal:array[TAsmCond] of byte=($0,
  2471. $7, $3, $2, $6, $2, $4, $F, $D, $C, $E, $6, $2,
  2472. $3, $7, $3, $5, $E, $C, $D, $F, $1, $B, $9, $5,
  2473. $0, $A, $A, $B, $8, $4);
  2474. var
  2475. c : byte;
  2476. pb : pbyte;
  2477. codes : pchar;
  2478. bytes : array[0..3] of byte;
  2479. rfield,
  2480. data,s,opidx : longint;
  2481. ea_data : ea;
  2482. relsym : TObjSymbol;
  2483. needed_VEX_Extension: boolean;
  2484. needed_VEX: boolean;
  2485. opmode: integer;
  2486. VEXvvvv: byte;
  2487. VEXmmmmm: byte;
  2488. begin
  2489. { safety check }
  2490. if objdata.currobjsec.size<>longword(insoffset) then
  2491. internalerror(200130121);
  2492. { those variables are initialized inside local procedures, the dfa cannot handle this yet }
  2493. currsym:=nil;
  2494. currabsreloc:=RELOC_NONE;
  2495. currabsreloc32:=RELOC_NONE;
  2496. currrelreloc:=RELOC_NONE;
  2497. currval:=0;
  2498. { check instruction's processor level }
  2499. { todo: maybe adapt and enable this code for i386 and x86_64 as well }
  2500. {$ifdef i8086}
  2501. if objdata.CPUType<>cpu_none then
  2502. begin
  2503. case insentry^.flags and IF_PLEVEL of
  2504. IF_8086:
  2505. ;
  2506. IF_186:
  2507. if objdata.CPUType<cpu_186 then
  2508. Message(asmw_e_instruction_not_supported_by_cpu);
  2509. IF_286:
  2510. if objdata.CPUType<cpu_286 then
  2511. Message(asmw_e_instruction_not_supported_by_cpu);
  2512. IF_386:
  2513. if objdata.CPUType<cpu_386 then
  2514. Message(asmw_e_instruction_not_supported_by_cpu);
  2515. IF_486:
  2516. if objdata.CPUType<cpu_486 then
  2517. Message(asmw_e_instruction_not_supported_by_cpu);
  2518. IF_PENT:
  2519. if objdata.CPUType<cpu_Pentium then
  2520. Message(asmw_e_instruction_not_supported_by_cpu);
  2521. IF_P6:
  2522. if objdata.CPUType<cpu_Pentium2 then
  2523. Message(asmw_e_instruction_not_supported_by_cpu);
  2524. IF_KATMAI:
  2525. if objdata.CPUType<cpu_Pentium3 then
  2526. Message(asmw_e_instruction_not_supported_by_cpu);
  2527. IF_WILLAMETTE,
  2528. IF_PRESCOTT:
  2529. if objdata.CPUType<cpu_Pentium4 then
  2530. Message(asmw_e_instruction_not_supported_by_cpu);
  2531. { the NEC V20/V30 extensions are incompatible with 386+, due to overlapping opcodes }
  2532. IF_NEC:
  2533. if objdata.CPUType>=cpu_386 then
  2534. Message(asmw_e_instruction_not_supported_by_cpu);
  2535. { todo: handle these properly }
  2536. IF_SANDYBRIDGE:
  2537. ;
  2538. end;
  2539. end;
  2540. {$endif i8086}
  2541. { load data to write }
  2542. codes:=insentry^.code;
  2543. {$ifdef x86_64}
  2544. rexwritten:=false;
  2545. {$endif x86_64}
  2546. { Force word push/pop for registers }
  2547. if (opsize={$ifdef i8086}S_L{$else}S_W{$endif}) and ((codes[0]=#4) or (codes[0]=#6) or
  2548. ((codes[0]=#1) and ((codes[2]=#5) or (codes[2]=#7)))) then
  2549. write0x66prefix;
  2550. // needed VEX Prefix (for AVX etc.)
  2551. needed_VEX := false;
  2552. needed_VEX_Extension := false;
  2553. opmode := -1;
  2554. VEXvvvv := 0;
  2555. VEXmmmmm := 0;
  2556. repeat
  2557. c:=ord(codes^);
  2558. inc(codes);
  2559. case c of
  2560. &0: break;
  2561. &1,
  2562. &2,
  2563. &3: inc(codes,c);
  2564. &74: opmode := 0;
  2565. &75: opmode := 1;
  2566. &76: opmode := 2;
  2567. &333: VEXvvvv := VEXvvvv OR $02; // set SIMD-prefix $F3
  2568. &334: VEXvvvv := VEXvvvv OR $03; // set SIMD-prefix $F2
  2569. &361: VEXvvvv := VEXvvvv OR $01; // set SIMD-prefix $66
  2570. &362: needed_VEX := true;
  2571. &363: begin
  2572. needed_VEX_Extension := true;
  2573. VEXvvvv := VEXvvvv OR (1 shl 7); // set REX.W
  2574. end;
  2575. &364: VEXvvvv := VEXvvvv OR $04; // vectorlength = 256 bits AND no scalar
  2576. &370: VEXmmmmm := VEXmmmmm OR $01; // set leading opcode byte $0F
  2577. &371: begin
  2578. needed_VEX_Extension := true;
  2579. VEXmmmmm := VEXmmmmm OR $02; // set leading opcode byte $0F38
  2580. end;
  2581. &372: begin
  2582. needed_VEX_Extension := true;
  2583. VEXmmmmm := VEXmmmmm OR $03; // set leading opcode byte $0F3A
  2584. end;
  2585. end;
  2586. until false;
  2587. if needed_VEX then
  2588. begin
  2589. if (opmode > ops) or
  2590. (opmode < -1) then
  2591. begin
  2592. Internalerror(777100);
  2593. end
  2594. else if opmode = -1 then
  2595. begin
  2596. VEXvvvv := VEXvvvv or ($0F shl 3); // set VEXvvvv bits (bits 6-3) to 1
  2597. end
  2598. else if oper[opmode]^.typ = top_reg then
  2599. begin
  2600. VEXvvvv := VEXvvvv or ((not(regval(oper[opmode]^.reg)) and $07) shl 3);
  2601. {$ifdef x86_64}
  2602. if rexbits(oper[opmode]^.reg) = 0 then VEXvvvv := VEXvvvv or (1 shl 6);
  2603. {$else}
  2604. VEXvvvv := VEXvvvv or (1 shl 6);
  2605. {$endif x86_64}
  2606. end
  2607. else Internalerror(777101);
  2608. if not(needed_VEX_Extension) then
  2609. begin
  2610. {$ifdef x86_64}
  2611. if rex and $0B <> 0 then needed_VEX_Extension := true;
  2612. {$endif x86_64}
  2613. end;
  2614. if needed_VEX_Extension then
  2615. begin
  2616. // VEX-Prefix-Length = 3 Bytes
  2617. bytes[0]:=$C4;
  2618. objdata.writebytes(bytes,1);
  2619. {$ifdef x86_64}
  2620. VEXmmmmm := VEXmmmmm or ((not(rex) and $07) shl 5); // set REX.rxb
  2621. {$else}
  2622. VEXmmmmm := VEXmmmmm or (7 shl 5); //
  2623. {$endif x86_64}
  2624. bytes[0] := VEXmmmmm;
  2625. objdata.writebytes(bytes,1);
  2626. {$ifdef x86_64}
  2627. VEXvvvv := VEXvvvv OR ((rex and $08) shl 7); // set REX.w
  2628. {$endif x86_64}
  2629. bytes[0] := VEXvvvv;
  2630. objdata.writebytes(bytes,1);
  2631. end
  2632. else
  2633. begin
  2634. // VEX-Prefix-Length = 2 Bytes
  2635. bytes[0]:=$C5;
  2636. objdata.writebytes(bytes,1);
  2637. {$ifdef x86_64}
  2638. if rex and $04 = 0 then
  2639. {$endif x86_64}
  2640. begin
  2641. VEXvvvv := VEXvvvv or (1 shl 7);
  2642. end;
  2643. bytes[0] := VEXvvvv;
  2644. objdata.writebytes(bytes,1);
  2645. end;
  2646. end
  2647. else
  2648. begin
  2649. needed_VEX_Extension := false;
  2650. opmode := -1;
  2651. end;
  2652. { load data to write }
  2653. codes:=insentry^.code;
  2654. repeat
  2655. c:=ord(codes^);
  2656. inc(codes);
  2657. case c of
  2658. &0 :
  2659. break;
  2660. &1,&2,&3 :
  2661. begin
  2662. {$ifdef x86_64}
  2663. if not(needed_VEX) then // TG
  2664. maybewriterex;
  2665. {$endif x86_64}
  2666. objdata.writebytes(codes^,c);
  2667. inc(codes,c);
  2668. end;
  2669. &4,&6 :
  2670. begin
  2671. case oper[0]^.reg of
  2672. NR_CS:
  2673. bytes[0]:=$e;
  2674. NR_NO,
  2675. NR_DS:
  2676. bytes[0]:=$1e;
  2677. NR_ES:
  2678. bytes[0]:=$6;
  2679. NR_SS:
  2680. bytes[0]:=$16;
  2681. else
  2682. internalerror(777004);
  2683. end;
  2684. if c=&4 then
  2685. inc(bytes[0]);
  2686. objdata.writebytes(bytes,1);
  2687. end;
  2688. &5,&7 :
  2689. begin
  2690. case oper[0]^.reg of
  2691. NR_FS:
  2692. bytes[0]:=$a0;
  2693. NR_GS:
  2694. bytes[0]:=$a8;
  2695. else
  2696. internalerror(777005);
  2697. end;
  2698. if c=&5 then
  2699. inc(bytes[0]);
  2700. objdata.writebytes(bytes,1);
  2701. end;
  2702. &10,&11,&12 :
  2703. begin
  2704. {$ifdef x86_64}
  2705. if not(needed_VEX) then // TG
  2706. maybewriterex;
  2707. {$endif x86_64}
  2708. bytes[0]:=ord(codes^)+regval(oper[c-&10]^.reg);
  2709. inc(codes);
  2710. objdata.writebytes(bytes,1);
  2711. end;
  2712. &13 :
  2713. begin
  2714. bytes[0]:=ord(codes^)+condval[condition];
  2715. inc(codes);
  2716. objdata.writebytes(bytes,1);
  2717. end;
  2718. &14,&15,&16 :
  2719. begin
  2720. getvalsym(c-&14);
  2721. if (currval<-128) or (currval>127) then
  2722. Message2(asmw_e_value_exceeds_bounds,'signed byte',tostr(currval));
  2723. if assigned(currsym) then
  2724. objdata_writereloc(currval,1,currsym,currabsreloc)
  2725. else
  2726. objdata.writebytes(currval,1);
  2727. end;
  2728. &20,&21,&22 :
  2729. begin
  2730. getvalsym(c-&20);
  2731. if (currval<-256) or (currval>255) then
  2732. Message2(asmw_e_value_exceeds_bounds,'byte',tostr(currval));
  2733. if assigned(currsym) then
  2734. objdata_writereloc(currval,1,currsym,currabsreloc)
  2735. else
  2736. objdata.writebytes(currval,1);
  2737. end;
  2738. &23 :
  2739. begin
  2740. bytes[0]:=ord(codes^)+condval[inverse_cond(condition)];
  2741. inc(codes);
  2742. objdata.writebytes(bytes,1);
  2743. end;
  2744. &24,&25,&26,&27 :
  2745. begin
  2746. getvalsym(c-&24);
  2747. if (insentry^.flags and IF_IMM3)<>0 then
  2748. begin
  2749. if (currval<0) or (currval>7) then
  2750. Message2(asmw_e_value_exceeds_bounds,'unsigned triad',tostr(currval));
  2751. end
  2752. else if (insentry^.flags and IF_IMM4)<>0 then
  2753. begin
  2754. if (currval<0) or (currval>15) then
  2755. Message2(asmw_e_value_exceeds_bounds,'unsigned nibble',tostr(currval));
  2756. end
  2757. else
  2758. if (currval<0) or (currval>255) then
  2759. Message2(asmw_e_value_exceeds_bounds,'unsigned byte',tostr(currval));
  2760. if assigned(currsym) then
  2761. objdata_writereloc(currval,1,currsym,currabsreloc)
  2762. else
  2763. objdata.writebytes(currval,1);
  2764. end;
  2765. &30,&31,&32 : // 030..032
  2766. begin
  2767. getvalsym(c-&30);
  2768. {$ifndef i8086}
  2769. { currval is an aint so this cannot happen on i8086 and causes only a warning }
  2770. if (currval<-65536) or (currval>65535) then
  2771. Message2(asmw_e_value_exceeds_bounds,'word',tostr(currval));
  2772. {$endif i8086}
  2773. if assigned(currsym)
  2774. {$ifdef i8086}
  2775. or (currabsreloc in [RELOC_DGROUP,RELOC_FARDATASEG])
  2776. {$endif i8086}
  2777. then
  2778. objdata_writereloc(currval,2,currsym,currabsreloc)
  2779. else
  2780. objdata.writebytes(currval,2);
  2781. end;
  2782. &34,&35,&36 : // 034..036
  2783. { !!! These are intended (and used in opcode table) to select depending
  2784. on address size, *not* operand size. Works by coincidence only. }
  2785. begin
  2786. getvalsym(c-&34);
  2787. {$ifdef i8086}
  2788. if assigned(currsym) then
  2789. objdata_writereloc(currval,2,currsym,currabsreloc)
  2790. else
  2791. objdata.writebytes(currval,2);
  2792. {$else i8086}
  2793. if opsize=S_Q then
  2794. begin
  2795. if assigned(currsym) then
  2796. objdata_writereloc(currval,8,currsym,currabsreloc)
  2797. else
  2798. objdata.writebytes(currval,8);
  2799. end
  2800. else
  2801. begin
  2802. if assigned(currsym) then
  2803. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2804. else
  2805. objdata.writebytes(currval,4);
  2806. end
  2807. {$endif i8086}
  2808. end;
  2809. &40,&41,&42 : // 040..042
  2810. begin
  2811. getvalsym(c-&40);
  2812. if assigned(currsym) then
  2813. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2814. else
  2815. objdata.writebytes(currval,4);
  2816. end;
  2817. &44,&45,&46 :// 044..046 - select between word/dword/qword depending on
  2818. begin // address size (we support only default address sizes).
  2819. getvalsym(c-&44);
  2820. {$if defined(x86_64)}
  2821. if assigned(currsym) then
  2822. objdata_writereloc(currval,8,currsym,currabsreloc)
  2823. else
  2824. objdata.writebytes(currval,8);
  2825. {$elseif defined(i386)}
  2826. if assigned(currsym) then
  2827. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2828. else
  2829. objdata.writebytes(currval,4);
  2830. {$elseif defined(i8086)}
  2831. if assigned(currsym) then
  2832. objdata_writereloc(currval,2,currsym,currabsreloc)
  2833. else
  2834. objdata.writebytes(currval,2);
  2835. {$endif}
  2836. end;
  2837. &50,&51,&52 : // 050..052 - byte relative operand
  2838. begin
  2839. getvalsym(c-&50);
  2840. data:=currval-insend;
  2841. {$push}
  2842. {$r-,q-} { disable also overflow as address returns a qword for x86_64 }
  2843. if assigned(currsym) then
  2844. inc(data,currsym.address);
  2845. {$pop}
  2846. if (data>127) or (data<-128) then
  2847. Message1(asmw_e_short_jmp_out_of_range,tostr(data));
  2848. objdata.writebytes(data,1);
  2849. end;
  2850. &54,&55,&56: // 054..056 - qword immediate operand
  2851. begin
  2852. getvalsym(c-&54);
  2853. if assigned(currsym) then
  2854. objdata_writereloc(currval,8,currsym,currabsreloc)
  2855. else
  2856. objdata.writebytes(currval,8);
  2857. end;
  2858. &60,&61,&62 :
  2859. begin
  2860. getvalsym(c-&60);
  2861. {$ifdef i8086}
  2862. if assigned(currsym) then
  2863. objdata_writereloc(currval,2,currsym,currrelreloc)
  2864. else
  2865. objdata_writereloc(currval-insend,2,nil,currabsreloc)
  2866. {$else i8086}
  2867. InternalError(777006);
  2868. {$endif i8086}
  2869. end;
  2870. &64,&65,&66 : // 064..066 - select between 16/32 address mode, but we support only 32 (only 16 on i8086)
  2871. begin
  2872. getvalsym(c-&64);
  2873. {$ifdef i8086}
  2874. if assigned(currsym) then
  2875. objdata_writereloc(currval,2,currsym,currrelreloc)
  2876. else
  2877. objdata_writereloc(currval-insend,2,nil,currabsreloc)
  2878. {$else i8086}
  2879. if assigned(currsym) then
  2880. objdata_writereloc(currval,4,currsym,currrelreloc)
  2881. else
  2882. objdata_writereloc(currval-insend,4,nil,currabsreloc32)
  2883. {$endif i8086}
  2884. end;
  2885. &70,&71,&72 : // 070..072 - long relative operand
  2886. begin
  2887. getvalsym(c-&70);
  2888. if assigned(currsym) then
  2889. objdata_writereloc(currval,4,currsym,currrelreloc)
  2890. else
  2891. objdata_writereloc(currval-insend,4,nil,currabsreloc32)
  2892. end;
  2893. &74,&75,&76 : ; // 074..076 - vex-coded vector operand
  2894. // ignore
  2895. &254,&255,&256 : // 0254..0256 - dword implicitly sign-extended to 64-bit (x86_64 only)
  2896. begin
  2897. getvalsym(c-&254);
  2898. {$ifdef x86_64}
  2899. { for i386 as aint type is longint the
  2900. following test is useless }
  2901. if (currval<low(longint)) or (currval>high(longint)) then
  2902. Message2(asmw_e_value_exceeds_bounds,'signed dword',tostr(currval));
  2903. {$endif x86_64}
  2904. if assigned(currsym) then
  2905. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2906. else
  2907. objdata.writebytes(currval,4);
  2908. end;
  2909. &300,&301,&302:
  2910. begin
  2911. {$if defined(x86_64) or defined(i8086)}
  2912. if (oper[c and 3]^.ot and OT_SIZE_MASK)=OT_BITS32 then
  2913. write0x67prefix;
  2914. {$endif x86_64 or i8086}
  2915. end;
  2916. &310 : { fixed 16-bit addr }
  2917. {$if defined(x86_64)}
  2918. { every insentry having code 0310 must be marked with NOX86_64 }
  2919. InternalError(2011051302);
  2920. {$elseif defined(i386)}
  2921. write0x67prefix;
  2922. {$elseif defined(i8086)}
  2923. {nothing};
  2924. {$endif}
  2925. &311 : { fixed 32-bit addr }
  2926. {$if defined(x86_64) or defined(i8086)}
  2927. write0x67prefix
  2928. {$endif x86_64 or i8086}
  2929. ;
  2930. &320,&321,&322 :
  2931. begin
  2932. case oper[c-&320]^.ot and OT_SIZE_MASK of
  2933. {$if defined(i386) or defined(x86_64)}
  2934. OT_BITS16 :
  2935. {$elseif defined(i8086)}
  2936. OT_BITS32 :
  2937. {$endif}
  2938. write0x66prefix;
  2939. {$ifndef x86_64}
  2940. OT_BITS64 :
  2941. Message(asmw_e_64bit_not_supported);
  2942. {$endif x86_64}
  2943. end;
  2944. end;
  2945. &323 : {no action needed};
  2946. &325:
  2947. {$ifdef i8086}
  2948. write0x66prefix;
  2949. {$else i8086}
  2950. {no action needed};
  2951. {$endif i8086}
  2952. &324,
  2953. &361:
  2954. begin
  2955. {$ifndef i8086}
  2956. if not(needed_VEX) then
  2957. write0x66prefix;
  2958. {$endif not i8086}
  2959. end;
  2960. &326 :
  2961. begin
  2962. {$ifndef x86_64}
  2963. Message(asmw_e_64bit_not_supported);
  2964. {$endif x86_64}
  2965. end;
  2966. &333 :
  2967. begin
  2968. if not(needed_VEX) then
  2969. begin
  2970. bytes[0]:=$f3;
  2971. objdata.writebytes(bytes,1);
  2972. end;
  2973. end;
  2974. &334 :
  2975. begin
  2976. if not(needed_VEX) then
  2977. begin
  2978. bytes[0]:=$f2;
  2979. objdata.writebytes(bytes,1);
  2980. end;
  2981. end;
  2982. &335:
  2983. ;
  2984. &312,
  2985. &327,
  2986. &331,&332 :
  2987. begin
  2988. { these are dissambler hints or 32 bit prefixes which
  2989. are not needed }
  2990. end;
  2991. &362..&364: ; // VEX flags =>> nothing todo
  2992. &366: begin
  2993. if needed_VEX then
  2994. begin
  2995. if ops = 4 then
  2996. begin
  2997. if (oper[2]^.typ=top_reg) then
  2998. begin
  2999. if (oper[2]^.ot and otf_reg_xmm <> 0) or
  3000. (oper[2]^.ot and otf_reg_ymm <> 0) then
  3001. begin
  3002. bytes[0] := ((getsupreg(oper[2]^.reg) and 15) shl 4);
  3003. objdata.writebytes(bytes,1);
  3004. end
  3005. else Internalerror(2014032001);
  3006. end
  3007. else Internalerror(2014032002);
  3008. end
  3009. else Internalerror(2014032003);
  3010. end
  3011. else Internalerror(2014032004);
  3012. end;
  3013. &367: begin
  3014. if needed_VEX then
  3015. begin
  3016. if ops = 4 then
  3017. begin
  3018. if (oper[3]^.typ=top_reg) then
  3019. begin
  3020. if (oper[3]^.ot and otf_reg_xmm <> 0) or
  3021. (oper[3]^.ot and otf_reg_ymm <> 0) then
  3022. begin
  3023. bytes[0] := ((getsupreg(oper[3]^.reg) and 15) shl 4);
  3024. objdata.writebytes(bytes,1);
  3025. end
  3026. else Internalerror(2014032005);
  3027. end
  3028. else Internalerror(2014032006);
  3029. end
  3030. else Internalerror(2014032007);
  3031. end
  3032. else Internalerror(2014032008);
  3033. end;
  3034. &370..&372: ; // VEX flags =>> nothing todo
  3035. &37:
  3036. begin
  3037. {$ifdef i8086}
  3038. if assigned(currsym) then
  3039. objdata_writereloc(0,2,currsym,RELOC_SEG)
  3040. else
  3041. InternalError(2015041503);
  3042. {$else i8086}
  3043. InternalError(777006);
  3044. {$endif i8086}
  3045. end;
  3046. else
  3047. begin
  3048. { rex should be written at this point }
  3049. {$ifdef x86_64}
  3050. if not(needed_VEX) then // TG
  3051. if (rex<>0) and not(rexwritten) then
  3052. internalerror(200603191);
  3053. {$endif x86_64}
  3054. if (c>=&100) and (c<=&227) then // 0100..0227
  3055. begin
  3056. if (c<&177) then // 0177
  3057. begin
  3058. if (oper[c and 7]^.typ=top_reg) then
  3059. rfield:=regval(oper[c and 7]^.reg)
  3060. else
  3061. rfield:=regval(oper[c and 7]^.ref^.base);
  3062. end
  3063. else
  3064. rfield:=c and 7;
  3065. opidx:=(c shr 3) and 7;
  3066. if not process_ea(oper[opidx]^,ea_data,rfield) then
  3067. Message(asmw_e_invalid_effective_address);
  3068. pb:=@bytes[0];
  3069. pb^:=ea_data.modrm;
  3070. inc(pb);
  3071. if ea_data.sib_present then
  3072. begin
  3073. pb^:=ea_data.sib;
  3074. inc(pb);
  3075. end;
  3076. s:=pb-@bytes[0];
  3077. objdata.writebytes(bytes,s);
  3078. case ea_data.bytes of
  3079. 0 : ;
  3080. 1 :
  3081. begin
  3082. if (oper[opidx]^.ot and OT_MEMORY)=OT_MEMORY then
  3083. begin
  3084. currsym:=objdata.symbolref(oper[opidx]^.ref^.symbol);
  3085. {$ifdef i386}
  3086. if (oper[opidx]^.ref^.refaddr=addr_pic) and
  3087. (tf_pic_uses_got in target_info.flags) then
  3088. currabsreloc:=RELOC_GOT32
  3089. else
  3090. {$endif i386}
  3091. {$ifdef x86_64}
  3092. if oper[opidx]^.ref^.refaddr=addr_pic then
  3093. currabsreloc:=RELOC_GOTPCREL
  3094. else
  3095. {$endif x86_64}
  3096. currabsreloc:=RELOC_ABSOLUTE;
  3097. objdata_writereloc(oper[opidx]^.ref^.offset,1,currsym,currabsreloc);
  3098. end
  3099. else
  3100. begin
  3101. bytes[0]:=oper[opidx]^.ref^.offset;
  3102. objdata.writebytes(bytes,1);
  3103. end;
  3104. inc(s);
  3105. end;
  3106. 2,4 :
  3107. begin
  3108. currsym:=objdata.symbolref(oper[opidx]^.ref^.symbol);
  3109. currval:=oper[opidx]^.ref^.offset;
  3110. {$ifdef x86_64}
  3111. if oper[opidx]^.ref^.refaddr=addr_pic then
  3112. currabsreloc:=RELOC_GOTPCREL
  3113. else
  3114. if oper[opidx]^.ref^.base=NR_RIP then
  3115. begin
  3116. currabsreloc:=RELOC_RELATIVE;
  3117. { Adjust reloc value by number of bytes following the displacement,
  3118. but not if displacement is specified by literal constant }
  3119. if Assigned(currsym) then
  3120. Dec(currval,InsEnd-objdata.CurrObjSec.Size-ea_data.bytes);
  3121. end
  3122. else
  3123. {$endif x86_64}
  3124. {$ifdef i386}
  3125. if (oper[opidx]^.ref^.refaddr=addr_pic) and
  3126. (tf_pic_uses_got in target_info.flags) then
  3127. currabsreloc:=RELOC_GOT32
  3128. else
  3129. {$endif i386}
  3130. {$ifdef i8086}
  3131. if ea_data.bytes=2 then
  3132. currabsreloc:=RELOC_ABSOLUTE
  3133. else
  3134. {$endif i8086}
  3135. currabsreloc:=RELOC_ABSOLUTE32;
  3136. if (currabsreloc in [RELOC_ABSOLUTE32{$ifdef i8086},RELOC_ABSOLUTE{$endif}]) and
  3137. (Assigned(oper[opidx]^.ref^.relsymbol)) then
  3138. begin
  3139. relsym:=objdata.symbolref(oper[opidx]^.ref^.relsymbol);
  3140. if relsym.objsection=objdata.CurrObjSec then
  3141. begin
  3142. currval:=objdata.CurrObjSec.size+ea_data.bytes-relsym.offset+currval;
  3143. {$ifdef i8086}
  3144. if ea_data.bytes=4 then
  3145. currabsreloc:=RELOC_RELATIVE32
  3146. else
  3147. {$endif i8086}
  3148. currabsreloc:=RELOC_RELATIVE;
  3149. end
  3150. else
  3151. begin
  3152. currabsreloc:=RELOC_PIC_PAIR;
  3153. currval:=relsym.offset;
  3154. end;
  3155. end;
  3156. objdata_writereloc(currval,ea_data.bytes,currsym,currabsreloc);
  3157. inc(s,ea_data.bytes);
  3158. end;
  3159. end;
  3160. end
  3161. else
  3162. InternalError(777007);
  3163. end;
  3164. end;
  3165. until false;
  3166. end;
  3167. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  3168. begin
  3169. result:=(((opcode=A_MOV) or (opcode=A_XCHG)) and
  3170. (regtype = R_INTREGISTER) and
  3171. (ops=2) and
  3172. (oper[0]^.typ=top_reg) and
  3173. (oper[1]^.typ=top_reg) and
  3174. (oper[0]^.reg=oper[1]^.reg)
  3175. ) or
  3176. (((opcode=A_MOVSS) or (opcode=A_MOVSD) or (opcode=A_MOVQ) or
  3177. (opcode=A_MOVAPS) or (OPCODE=A_MOVAPD) or
  3178. (opcode=A_VMOVSS) or (opcode=A_VMOVSD) or (opcode=A_VMOVQ) or
  3179. (opcode=A_VMOVAPS) or (OPCODE=A_VMOVAPD)) and
  3180. (regtype = R_MMREGISTER) and
  3181. (ops=2) and
  3182. (oper[0]^.typ=top_reg) and
  3183. (oper[1]^.typ=top_reg) and
  3184. (oper[0]^.reg=oper[1]^.reg)
  3185. );
  3186. end;
  3187. procedure build_spilling_operation_type_table;
  3188. var
  3189. opcode : tasmop;
  3190. i : integer;
  3191. begin
  3192. new(operation_type_table);
  3193. fillchar(operation_type_table^,sizeof(toperation_type_table),byte(operand_read));
  3194. for opcode:=low(tasmop) to high(tasmop) do
  3195. begin
  3196. for i:=1 to MaxInsChanges do
  3197. begin
  3198. case InsProp[opcode].Ch[i] of
  3199. Ch_Rop1 :
  3200. operation_type_table^[opcode,0]:=operand_read;
  3201. Ch_Wop1 :
  3202. operation_type_table^[opcode,0]:=operand_write;
  3203. Ch_RWop1,
  3204. Ch_Mop1 :
  3205. operation_type_table^[opcode,0]:=operand_readwrite;
  3206. Ch_Rop2 :
  3207. operation_type_table^[opcode,1]:=operand_read;
  3208. Ch_Wop2 :
  3209. operation_type_table^[opcode,1]:=operand_write;
  3210. Ch_RWop2,
  3211. Ch_Mop2 :
  3212. operation_type_table^[opcode,1]:=operand_readwrite;
  3213. Ch_Rop3 :
  3214. operation_type_table^[opcode,2]:=operand_read;
  3215. Ch_Wop3 :
  3216. operation_type_table^[opcode,2]:=operand_write;
  3217. Ch_RWop3,
  3218. Ch_Mop3 :
  3219. operation_type_table^[opcode,2]:=operand_readwrite;
  3220. end;
  3221. end;
  3222. end;
  3223. end;
  3224. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  3225. begin
  3226. { the information in the instruction table is made for the string copy
  3227. operation MOVSD so hack here (FK)
  3228. VMOVSS and VMOVSD has two and three operand flavours, this cannot modelled by x86ins.dat
  3229. so fix it here (FK)
  3230. }
  3231. if ((opcode=A_MOVSD) or (opcode=A_VMOVSS) or (opcode=A_VMOVSD)) and (ops=2) then
  3232. begin
  3233. case opnr of
  3234. 0:
  3235. result:=operand_read;
  3236. 1:
  3237. result:=operand_write;
  3238. else
  3239. internalerror(200506055);
  3240. end
  3241. end
  3242. { IMUL has 1, 2 and 3-operand forms }
  3243. else if opcode=A_IMUL then
  3244. begin
  3245. case ops of
  3246. 1:
  3247. if opnr=0 then
  3248. result:=operand_read
  3249. else
  3250. internalerror(2014011802);
  3251. 2:
  3252. begin
  3253. case opnr of
  3254. 0:
  3255. result:=operand_read;
  3256. 1:
  3257. result:=operand_readwrite;
  3258. else
  3259. internalerror(2014011803);
  3260. end;
  3261. end;
  3262. 3:
  3263. begin
  3264. case opnr of
  3265. 0,1:
  3266. result:=operand_read;
  3267. 2:
  3268. result:=operand_write;
  3269. else
  3270. internalerror(2014011804);
  3271. end;
  3272. end;
  3273. else
  3274. internalerror(2014011805);
  3275. end;
  3276. end
  3277. else
  3278. result:=operation_type_table^[opcode,opnr];
  3279. end;
  3280. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  3281. var
  3282. tmpref: treference;
  3283. begin
  3284. tmpref:=ref;
  3285. {$ifdef i8086}
  3286. if tmpref.segment=NR_SS then
  3287. tmpref.segment:=NR_NO;
  3288. {$endif i8086}
  3289. case getregtype(r) of
  3290. R_INTREGISTER :
  3291. begin
  3292. if getsubreg(r)=R_SUBH then
  3293. inc(tmpref.offset);
  3294. { we don't need special code here for 32 bit loads on x86_64, since
  3295. those will automatically zero-extend the upper 32 bits. }
  3296. result:=taicpu.op_ref_reg(A_MOV,reg2opsize(r),tmpref,r);
  3297. end;
  3298. R_MMREGISTER :
  3299. if current_settings.fputype in fpu_avx_instructionsets then
  3300. case getsubreg(r) of
  3301. R_SUBMMD:
  3302. result:=taicpu.op_ref_reg(A_VMOVSD,reg2opsize(r),tmpref,r);
  3303. R_SUBMMS:
  3304. result:=taicpu.op_ref_reg(A_VMOVSS,reg2opsize(r),tmpref,r);
  3305. R_SUBQ,
  3306. R_SUBMMWHOLE:
  3307. result:=taicpu.op_ref_reg(A_VMOVQ,S_NO,tmpref,r);
  3308. else
  3309. internalerror(200506043);
  3310. end
  3311. else
  3312. case getsubreg(r) of
  3313. R_SUBMMD:
  3314. result:=taicpu.op_ref_reg(A_MOVSD,reg2opsize(r),tmpref,r);
  3315. R_SUBMMS:
  3316. result:=taicpu.op_ref_reg(A_MOVSS,reg2opsize(r),tmpref,r);
  3317. R_SUBQ,
  3318. R_SUBMMWHOLE:
  3319. result:=taicpu.op_ref_reg(A_MOVQ,S_NO,tmpref,r);
  3320. else
  3321. internalerror(200506043);
  3322. end;
  3323. else
  3324. internalerror(200401041);
  3325. end;
  3326. end;
  3327. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  3328. var
  3329. size: topsize;
  3330. tmpref: treference;
  3331. begin
  3332. tmpref:=ref;
  3333. {$ifdef i8086}
  3334. if tmpref.segment=NR_SS then
  3335. tmpref.segment:=NR_NO;
  3336. {$endif i8086}
  3337. case getregtype(r) of
  3338. R_INTREGISTER :
  3339. begin
  3340. if getsubreg(r)=R_SUBH then
  3341. inc(tmpref.offset);
  3342. size:=reg2opsize(r);
  3343. {$ifdef x86_64}
  3344. { even if it's a 32 bit reg, we still have to spill 64 bits
  3345. because we often perform 64 bit operations on them }
  3346. if (size=S_L) then
  3347. begin
  3348. size:=S_Q;
  3349. r:=newreg(getregtype(r),getsupreg(r),R_SUBWHOLE);
  3350. end;
  3351. {$endif x86_64}
  3352. result:=taicpu.op_reg_ref(A_MOV,size,r,tmpref);
  3353. end;
  3354. R_MMREGISTER :
  3355. if current_settings.fputype in fpu_avx_instructionsets then
  3356. case getsubreg(r) of
  3357. R_SUBMMD:
  3358. result:=taicpu.op_reg_ref(A_VMOVSD,reg2opsize(r),r,tmpref);
  3359. R_SUBMMS:
  3360. result:=taicpu.op_reg_ref(A_VMOVSS,reg2opsize(r),r,tmpref);
  3361. R_SUBQ,
  3362. R_SUBMMWHOLE:
  3363. result:=taicpu.op_reg_ref(A_VMOVQ,S_NO,r,tmpref);
  3364. else
  3365. internalerror(200506042);
  3366. end
  3367. else
  3368. case getsubreg(r) of
  3369. R_SUBMMD:
  3370. result:=taicpu.op_reg_ref(A_MOVSD,reg2opsize(r),r,tmpref);
  3371. R_SUBMMS:
  3372. result:=taicpu.op_reg_ref(A_MOVSS,reg2opsize(r),r,tmpref);
  3373. R_SUBQ,
  3374. R_SUBMMWHOLE:
  3375. result:=taicpu.op_reg_ref(A_MOVQ,S_NO,r,tmpref);
  3376. else
  3377. internalerror(200506042);
  3378. end;
  3379. else
  3380. internalerror(200401041);
  3381. end;
  3382. end;
  3383. {$ifdef i8086}
  3384. procedure taicpu.loadsegsymbol(opidx:longint;s:tasmsymbol);
  3385. var
  3386. r: treference;
  3387. begin
  3388. reference_reset_symbol(r,s,0,1);
  3389. r.refaddr:=addr_seg;
  3390. loadref(opidx,r);
  3391. end;
  3392. {$endif i8086}
  3393. {*****************************************************************************
  3394. Instruction table
  3395. *****************************************************************************}
  3396. procedure BuildInsTabCache;
  3397. var
  3398. i : longint;
  3399. begin
  3400. new(instabcache);
  3401. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  3402. i:=0;
  3403. while (i<InsTabEntries) do
  3404. begin
  3405. if InsTabCache^[InsTab[i].OPcode]=-1 then
  3406. InsTabCache^[InsTab[i].OPcode]:=i;
  3407. inc(i);
  3408. end;
  3409. end;
  3410. procedure BuildInsTabMemRefSizeInfoCache;
  3411. var
  3412. AsmOp: TasmOp;
  3413. i,j: longint;
  3414. insentry : PInsEntry;
  3415. MRefInfo: TMemRefSizeInfo;
  3416. SConstInfo: TConstSizeInfo;
  3417. actRegSize: int64;
  3418. actMemSize: int64;
  3419. actConstSize: int64;
  3420. actRegCount: integer;
  3421. actMemCount: integer;
  3422. actConstCount: integer;
  3423. actRegTypes : int64;
  3424. actRegMemTypes: int64;
  3425. NewRegSize: int64;
  3426. actVMemCount : integer;
  3427. actVMemTypes : int64;
  3428. RegMMXSizeMask: int64;
  3429. RegXMMSizeMask: int64;
  3430. RegYMMSizeMask: int64;
  3431. bitcount: integer;
  3432. function bitcnt(aValue: int64): integer;
  3433. var
  3434. i: integer;
  3435. begin
  3436. result := 0;
  3437. for i := 0 to 63 do
  3438. begin
  3439. if (aValue mod 2) = 1 then
  3440. begin
  3441. inc(result);
  3442. end;
  3443. aValue := aValue shr 1;
  3444. end;
  3445. end;
  3446. begin
  3447. new(InsTabMemRefSizeInfoCache);
  3448. FillChar(InsTabMemRefSizeInfoCache^,sizeof(TInsTabMemRefSizeInfoCache),0);
  3449. for AsmOp := low(TAsmOp) to high(TAsmOp) do
  3450. begin
  3451. i := InsTabCache^[AsmOp];
  3452. if i >= 0 then
  3453. begin
  3454. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiUnkown;
  3455. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := csiUnkown;
  3456. InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := false;
  3457. insentry:=@instab[i];
  3458. RegMMXSizeMask := 0;
  3459. RegXMMSizeMask := 0;
  3460. RegYMMSizeMask := 0;
  3461. while (insentry^.opcode=AsmOp) do
  3462. begin
  3463. MRefInfo := msiUnkown;
  3464. actRegSize := 0;
  3465. actRegCount := 0;
  3466. actRegTypes := 0;
  3467. NewRegSize := 0;
  3468. actMemSize := 0;
  3469. actMemCount := 0;
  3470. actRegMemTypes := 0;
  3471. actVMemCount := 0;
  3472. actVMemTypes := 0;
  3473. actConstSize := 0;
  3474. actConstCount := 0;
  3475. for j := 0 to insentry^.ops -1 do
  3476. begin
  3477. if ((insentry^.optypes[j] and OT_XMEM32) = OT_XMEM32) OR
  3478. ((insentry^.optypes[j] and OT_XMEM64) = OT_XMEM64) OR
  3479. ((insentry^.optypes[j] and OT_YMEM32) = OT_YMEM32) OR
  3480. ((insentry^.optypes[j] and OT_YMEM64) = OT_YMEM64) then
  3481. begin
  3482. inc(actVMemCount);
  3483. case insentry^.optypes[j] and (OT_XMEM32 OR OT_XMEM64 OR OT_YMEM32 OR OT_YMEM64) of
  3484. OT_XMEM32: actVMemTypes := actVMemTypes or OT_XMEM32;
  3485. OT_XMEM64: actVMemTypes := actVMemTypes or OT_XMEM64;
  3486. OT_YMEM32: actVMemTypes := actVMemTypes or OT_YMEM32;
  3487. OT_YMEM64: actVMemTypes := actVMemTypes or OT_YMEM64;
  3488. else InternalError(777206);
  3489. end;
  3490. end
  3491. else if (insentry^.optypes[j] and OT_REGISTER) = OT_REGISTER then
  3492. begin
  3493. inc(actRegCount);
  3494. NewRegSize := (insentry^.optypes[j] and OT_SIZE_MASK);
  3495. if NewRegSize = 0 then
  3496. begin
  3497. case insentry^.optypes[j] and (OT_MMXREG OR OT_XMMREG OR OT_YMMREG) of
  3498. OT_MMXREG: begin
  3499. NewRegSize := OT_BITS64;
  3500. end;
  3501. OT_XMMREG: begin
  3502. NewRegSize := OT_BITS128;
  3503. InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := true;
  3504. end;
  3505. OT_YMMREG: begin
  3506. NewRegSize := OT_BITS256;
  3507. InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := true;
  3508. end;
  3509. else NewRegSize := not(0);
  3510. end;
  3511. end;
  3512. actRegSize := actRegSize or NewRegSize;
  3513. actRegTypes := actRegTypes or (insentry^.optypes[j] and (OT_MMXREG OR OT_XMMREG OR OT_YMMREG));
  3514. end
  3515. else if ((insentry^.optypes[j] and OT_MEMORY) <> 0) then
  3516. begin
  3517. inc(actMemCount);
  3518. actMemSize:=actMemSize or (insentry^.optypes[j] and OT_SIZE_MASK);
  3519. if (insentry^.optypes[j] and OT_REGMEM) = OT_REGMEM then
  3520. begin
  3521. actRegMemTypes := actRegMemTypes or insentry^.optypes[j];
  3522. end;
  3523. end
  3524. else if ((insentry^.optypes[j] and OT_IMMEDIATE) = OT_IMMEDIATE) then
  3525. begin
  3526. inc(actConstCount);
  3527. actConstSize := actConstSize or (insentry^.optypes[j] and OT_SIZE_MASK);
  3528. end
  3529. end;
  3530. if actConstCount > 0 then
  3531. begin
  3532. case actConstSize of
  3533. 0: SConstInfo := csiNoSize;
  3534. OT_BITS8: SConstInfo := csiMem8;
  3535. OT_BITS16: SConstInfo := csiMem16;
  3536. OT_BITS32: SConstInfo := csiMem32;
  3537. OT_BITS64: SConstInfo := csiMem64;
  3538. else SConstInfo := csiMultiple;
  3539. end;
  3540. if InsTabMemRefSizeInfoCache^[AsmOp].ConstSize = csiUnkown then
  3541. begin
  3542. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := SConstInfo;
  3543. end
  3544. else if InsTabMemRefSizeInfoCache^[AsmOp].ConstSize <> SConstInfo then
  3545. begin
  3546. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := csiMultiple;
  3547. end;
  3548. end;
  3549. if actVMemCount > 0 then
  3550. begin
  3551. if actVMemCount = 1 then
  3552. begin
  3553. if actVMemTypes > 0 then
  3554. begin
  3555. case actVMemTypes of
  3556. OT_XMEM32: MRefInfo := msiXMem32;
  3557. OT_XMEM64: MRefInfo := msiXMem64;
  3558. OT_YMEM32: MRefInfo := msiYMem32;
  3559. OT_YMEM64: MRefInfo := msiYMem64;
  3560. else InternalError(777208);
  3561. end;
  3562. case actRegTypes of
  3563. OT_XMMREG: case MRefInfo of
  3564. msiXMem32,
  3565. msiXMem64: RegXMMSizeMask := RegXMMSizeMask or OT_BITS128;
  3566. msiYMem32,
  3567. msiYMem64: RegXMMSizeMask := RegXMMSizeMask or OT_BITS256;
  3568. else InternalError(777210);
  3569. end;
  3570. OT_YMMREG: case MRefInfo of
  3571. msiXMem32,
  3572. msiXMem64: RegYMMSizeMask := RegYMMSizeMask or OT_BITS128;
  3573. msiYMem32,
  3574. msiYMem64: RegYMMSizeMask := RegYMMSizeMask or OT_BITS256;
  3575. else InternalError(777211);
  3576. end;
  3577. //else InternalError(777209);
  3578. end;
  3579. if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize = msiUnkown then
  3580. begin
  3581. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := MRefInfo;
  3582. end
  3583. else if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize <> MRefInfo then
  3584. begin
  3585. if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize in [msiXMem32, msiXMem64, msiYMem32, msiYMem64] then
  3586. begin
  3587. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiVMemMultiple;
  3588. end
  3589. else InternalError(777212);
  3590. end;
  3591. end;
  3592. end
  3593. else InternalError(777207);
  3594. end
  3595. else
  3596. case actMemCount of
  3597. 0: ; // nothing todo
  3598. 1: begin
  3599. MRefInfo := msiUnkown;
  3600. case actRegMemTypes and (OT_MMXRM OR OT_XMMRM OR OT_YMMRM) of
  3601. OT_MMXRM: actMemSize := actMemSize or OT_BITS64;
  3602. OT_XMMRM: actMemSize := actMemSize or OT_BITS128;
  3603. OT_YMMRM: actMemSize := actMemSize or OT_BITS256;
  3604. end;
  3605. case actMemSize of
  3606. 0: MRefInfo := msiNoSize;
  3607. OT_BITS8: MRefInfo := msiMem8;
  3608. OT_BITS16: MRefInfo := msiMem16;
  3609. OT_BITS32: MRefInfo := msiMem32;
  3610. OT_BITS64: MRefInfo := msiMem64;
  3611. OT_BITS128: MRefInfo := msiMem128;
  3612. OT_BITS256: MRefInfo := msiMem256;
  3613. OT_BITS80,
  3614. OT_FAR,
  3615. OT_NEAR,
  3616. OT_SHORT: ; // ignore
  3617. else
  3618. begin
  3619. bitcount := bitcnt(actMemSize);
  3620. if bitcount > 1 then MRefInfo := msiMultiple
  3621. else InternalError(777203);
  3622. end;
  3623. end;
  3624. if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize = msiUnkown then
  3625. begin
  3626. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := MRefInfo;
  3627. end
  3628. else if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize <> MRefInfo then
  3629. begin
  3630. with InsTabMemRefSizeInfoCache^[AsmOp] do
  3631. begin
  3632. if ((MemRefSize = msiMem8) OR (MRefInfo = msiMem8)) then MemRefSize := msiMultiple8
  3633. else if ((MemRefSize = msiMem16) OR (MRefInfo = msiMem16)) then MemRefSize := msiMultiple16
  3634. else if ((MemRefSize = msiMem32) OR (MRefInfo = msiMem32)) then MemRefSize := msiMultiple32
  3635. else if ((MemRefSize = msiMem64) OR (MRefInfo = msiMem64)) then MemRefSize := msiMultiple64
  3636. else if ((MemRefSize = msiMem128) OR (MRefInfo = msiMem128)) then MemRefSize := msiMultiple128
  3637. else if ((MemRefSize = msiMem256) OR (MRefInfo = msiMem256)) then MemRefSize := msiMultiple256
  3638. else MemRefSize := msiMultiple;
  3639. end;
  3640. end;
  3641. if actRegCount > 0 then
  3642. begin
  3643. case actRegTypes and (OT_MMXREG or OT_XMMREG or OT_YMMREG) of
  3644. OT_MMXREG: RegMMXSizeMask := RegMMXSizeMask or actMemSize;
  3645. OT_XMMREG: RegXMMSizeMask := RegXMMSizeMask or actMemSize;
  3646. OT_YMMREG: RegYMMSizeMask := RegYMMSizeMask or actMemSize;
  3647. else begin
  3648. RegMMXSizeMask := not(0);
  3649. RegXMMSizeMask := not(0);
  3650. RegYMMSizeMask := not(0);
  3651. end;
  3652. end;
  3653. end;
  3654. end;
  3655. else InternalError(777202);
  3656. end;
  3657. inc(insentry);
  3658. end;
  3659. if (InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize in MemRefMultiples) and
  3660. (InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX)then
  3661. begin
  3662. case RegXMMSizeMask of
  3663. OT_BITS16: case RegYMMSizeMask of
  3664. OT_BITS32: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx16y32;
  3665. end;
  3666. OT_BITS32: case RegYMMSizeMask of
  3667. OT_BITS64: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx32y64;
  3668. end;
  3669. OT_BITS64: case RegYMMSizeMask of
  3670. OT_BITS128: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx64y128;
  3671. OT_BITS256: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx64y256;
  3672. end;
  3673. OT_BITS128: begin
  3674. if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize = msiVMemMultiple then
  3675. begin
  3676. // vector-memory-operand AVX2 (e.g. VGATHER..)
  3677. case RegYMMSizeMask of
  3678. OT_BITS256: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiVMemRegSize;
  3679. end;
  3680. end
  3681. else if RegMMXSizeMask = 0 then
  3682. begin
  3683. case RegYMMSizeMask of
  3684. OT_BITS128: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx64y128;
  3685. OT_BITS256: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegSize;
  3686. end;
  3687. end
  3688. else if RegYMMSizeMask = 0 then
  3689. begin
  3690. case RegMMXSizeMask of
  3691. OT_BITS64: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegSize;
  3692. end;
  3693. end
  3694. else InternalError(777205);
  3695. end;
  3696. end;
  3697. end;
  3698. end;
  3699. end;
  3700. for AsmOp := low(TAsmOp) to high(TAsmOp) do
  3701. begin
  3702. // only supported intructiones with SSE- or AVX-operands
  3703. if not(InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX) then
  3704. begin
  3705. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiUnkown;
  3706. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := csiUnkown;
  3707. end;
  3708. end;
  3709. end;
  3710. procedure InitAsm;
  3711. begin
  3712. build_spilling_operation_type_table;
  3713. if not assigned(instabcache) then
  3714. BuildInsTabCache;
  3715. if not assigned(InsTabMemRefSizeInfoCache) then
  3716. BuildInsTabMemRefSizeInfoCache;
  3717. end;
  3718. procedure DoneAsm;
  3719. begin
  3720. if assigned(operation_type_table) then
  3721. begin
  3722. dispose(operation_type_table);
  3723. operation_type_table:=nil;
  3724. end;
  3725. if assigned(instabcache) then
  3726. begin
  3727. dispose(instabcache);
  3728. instabcache:=nil;
  3729. end;
  3730. if assigned(InsTabMemRefSizeInfoCache) then
  3731. begin
  3732. dispose(InsTabMemRefSizeInfoCache);
  3733. InsTabMemRefSizeInfoCache:=nil;
  3734. end;
  3735. end;
  3736. begin
  3737. cai_align:=tai_align;
  3738. cai_cpu:=taicpu;
  3739. end.