aasmcpu.pas 114 KB

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