aasmcpu.pas 149 KB

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