aasmcpu.pas 137 KB

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