aasmcpu.pas 140 KB

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