aasmcpu.pas 138 KB

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