aasmcpu.pas 114 KB

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