aasmcpu.pas 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl and Peter Vreman
  4. Contains the abstract assembler implementation for the i386
  5. * Portions of this code was inspired by the NASM sources
  6. The Netwide Assembler is Copyright (c) 1996 Simon Tatham and
  7. Julian Hall. All rights reserved.
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. ****************************************************************************
  20. }
  21. unit aasmcpu;
  22. {$i fpcdefs.inc}
  23. interface
  24. uses
  25. cclasses,globals,verbose,
  26. cpuinfo,cpubase,
  27. symppu,
  28. aasmbase,aasmtai;
  29. const
  30. { "mov reg,reg" source operand number }
  31. O_MOV_SOURCE = 0;
  32. { "mov reg,reg" destination operand number }
  33. O_MOV_DEST = 1;
  34. { Operand types }
  35. OT_NONE = $00000000;
  36. OT_BITS8 = $00000001; { size, and other attributes, of the operand }
  37. OT_BITS16 = $00000002;
  38. OT_BITS32 = $00000004;
  39. OT_BITS64 = $00000008; { FPU only }
  40. OT_BITS80 = $00000010;
  41. OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
  42. OT_NEAR = $00000040;
  43. OT_SHORT = $00000080;
  44. OT_SIZE_MASK = $000000FF; { all the size attributes }
  45. OT_NON_SIZE = longint(not OT_SIZE_MASK);
  46. OT_SIGNED = $00000100; { the operand need to be signed -128-127 }
  47. OT_TO = $00000200; { operand is followed by a colon }
  48. { reverse effect in FADD, FSUB &c }
  49. OT_COLON = $00000400;
  50. OT_REGISTER = $00001000;
  51. OT_IMMEDIATE = $00002000;
  52. OT_IMM8 = $00002001;
  53. OT_IMM16 = $00002002;
  54. OT_IMM32 = $00002004;
  55. OT_IMM64 = $00002008;
  56. OT_IMM80 = $00002010;
  57. OT_REGMEM = $00200000; { for r/m, ie EA, operands }
  58. OT_REGNORM = $00201000; { 'normal' reg, qualifies as EA }
  59. OT_REG8 = $00201001;
  60. OT_REG16 = $00201002;
  61. OT_REG32 = $00201004;
  62. {$ifdef x86_64}
  63. OT_REG64 = $00201008;
  64. {$endif x86_64}
  65. OT_MMXREG = $00201008; { MMX registers }
  66. OT_XMMREG = $00201010; { Katmai registers }
  67. OT_MEMORY = $00204000; { register number in 'basereg' }
  68. OT_MEM8 = $00204001;
  69. OT_MEM16 = $00204002;
  70. OT_MEM32 = $00204004;
  71. OT_MEM64 = $00204008;
  72. OT_MEM80 = $00204010;
  73. OT_FPUREG = $01000000; { floating point stack registers }
  74. OT_FPU0 = $01000800; { FPU stack register zero }
  75. OT_REG_SMASK = $00070000; { special register operands: these may be treated differently }
  76. { a mask for the following }
  77. OT_REG_ACCUM = $00211000; { FUNCTION_RETURN_REG: AL, AX or EAX }
  78. OT_REG_AL = $00211001; { REG_ACCUM | BITSxx }
  79. OT_REG_AX = $00211002; { ditto }
  80. OT_REG_EAX = $00211004; { and again }
  81. {$ifdef x86_64}
  82. OT_REG_RAX = $00211008;
  83. {$endif x86_64}
  84. OT_REG_COUNT = $00221000; { counter: CL, CX or ECX }
  85. OT_REG_CL = $00221001; { REG_COUNT | BITSxx }
  86. OT_REG_CX = $00221002; { ditto }
  87. OT_REG_ECX = $00221004; { another one }
  88. {$ifdef x86_64}
  89. OT_REG_RCX = $00221008;
  90. {$endif x86_64}
  91. OT_REG_DX = $00241002;
  92. OT_REG_SREG = $00081002; { any segment register }
  93. OT_REG_CS = $01081002; { CS }
  94. OT_REG_DESS = $02081002; { DS, ES, SS (non-CS 86 registers) }
  95. OT_REG_FSGS = $04081002; { FS, GS (386 extended registers) }
  96. OT_REG_CDT = $00101004; { CRn, DRn and TRn }
  97. OT_REG_CREG = $08101004; { CRn }
  98. OT_REG_CR4 = $08101404; { CR4 (Pentium only) }
  99. OT_REG_DREG = $10101004; { DRn }
  100. OT_REG_TREG = $20101004; { TRn }
  101. OT_MEM_OFFS = $00604000; { special type of EA }
  102. { simple [address] offset }
  103. OT_ONENESS = $00800000; { special type of immediate operand }
  104. { so UNITY == IMMEDIATE | ONENESS }
  105. OT_UNITY = $00802000; { for shift/rotate instructions }
  106. { Size of the instruction table converted by nasmconv.pas }
  107. {$ifdef x86_64}
  108. instabentries = {$i x86_64no.inc}
  109. {$else x86_64}
  110. instabentries = {$i i386nop.inc}
  111. {$endif x86_64}
  112. maxinfolen = 8;
  113. type
  114. TOperandOrder = (op_intel,op_att);
  115. tinsentry=packed record
  116. opcode : tasmop;
  117. ops : byte;
  118. optypes : array[0..2] of longint;
  119. code : array[0..maxinfolen] of char;
  120. flags : longint;
  121. end;
  122. pinsentry=^tinsentry;
  123. { alignment for operator }
  124. tai_align = class(tai_align_abstract)
  125. reg : tregister;
  126. constructor create(b:byte);
  127. constructor create_op(b: byte; _op: byte);
  128. function calculatefillbuf(var buf : tfillbuffer):pchar;override;
  129. end;
  130. taicpu = class(taicpu_abstract)
  131. opsize : topsize;
  132. constructor op_none(op : tasmop;_size : topsize);
  133. constructor op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  134. constructor op_const(op : tasmop;_size : topsize;_op1 : aword);
  135. constructor op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  136. constructor op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  137. constructor op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  138. constructor op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aword);
  139. constructor op_const_reg(op : tasmop;_size : topsize;_op1 : aword;_op2 : tregister);
  140. constructor op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aword);
  141. constructor op_const_ref(op : tasmop;_size : topsize;_op1 : aword;const _op2 : treference);
  142. constructor op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  143. constructor op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  144. constructor op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aword;_op2 : tregister;_op3 : tregister);
  145. constructor op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aword;const _op2 : treference;_op3 : tregister);
  146. constructor op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister; const _op3 : treference);
  147. constructor op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aword;_op2 : tregister;const _op3 : treference);
  148. { this is for Jmp instructions }
  149. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  150. constructor op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  151. constructor op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  152. constructor op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  153. constructor op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  154. procedure changeopsize(siz:topsize);
  155. function GetString:string;
  156. procedure CheckNonCommutativeOpcodes;
  157. private
  158. FOperandOrder : TOperandOrder;
  159. procedure init(_size : topsize); { this need to be called by all constructor }
  160. {$ifndef NOAG386BIN}
  161. public
  162. { the next will reset all instructions that can change in pass 2 }
  163. procedure ResetPass1;
  164. procedure ResetPass2;
  165. function CheckIfValid:boolean;
  166. function Pass1(offset:longint):longint;virtual;
  167. procedure Pass2(sec:TAsmObjectdata);virtual;
  168. procedure SetOperandOrder(order:TOperandOrder);
  169. function is_nop:boolean;override;
  170. function is_move:boolean;override;
  171. {$ifdef NEWRA}
  172. function spill_registers(list:Taasmoutput;
  173. rgget:Trggetproc;
  174. rgunget:Trgungetproc;
  175. r:Tsupregset;
  176. var unusedregsint:Tsupregset;
  177. const spilltemplist:Tspill_temp_list):boolean;override;
  178. {$endif}
  179. protected
  180. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);override;
  181. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);override;
  182. procedure ppuderefoper(var o:toper);override;
  183. private
  184. { next fields are filled in pass1, so pass2 is faster }
  185. insentry : PInsEntry;
  186. insoffset,
  187. inssize : longint;
  188. LastInsOffset : longint; { need to be public to be reset }
  189. function InsEnd:longint;
  190. procedure create_ot;
  191. function Matches(p:PInsEntry):longint;
  192. function calcsize(p:PInsEntry):longint;
  193. procedure gencode(sec:TAsmObjectData);
  194. function NeedAddrPrefix(opidx:byte):boolean;
  195. procedure Swapoperands;
  196. {$endif NOAG386BIN}
  197. end;
  198. procedure InitAsm;
  199. procedure DoneAsm;
  200. implementation
  201. uses
  202. cutils,
  203. itx86att;
  204. {*****************************************************************************
  205. Instruction table
  206. *****************************************************************************}
  207. const
  208. {Instruction flags }
  209. IF_NONE = $00000000;
  210. IF_SM = $00000001; { size match first two operands }
  211. IF_SM2 = $00000002;
  212. IF_SB = $00000004; { unsized operands can't be non-byte }
  213. IF_SW = $00000008; { unsized operands can't be non-word }
  214. IF_SD = $00000010; { unsized operands can't be nondword }
  215. IF_AR0 = $00000020; { SB, SW, SD applies to argument 0 }
  216. IF_AR1 = $00000040; { SB, SW, SD applies to argument 1 }
  217. IF_AR2 = $00000060; { SB, SW, SD applies to argument 2 }
  218. IF_ARMASK = $00000060; { mask for unsized argument spec }
  219. IF_PRIV = $00000100; { it's a privileged instruction }
  220. IF_SMM = $00000200; { it's only valid in SMM }
  221. IF_PROT = $00000400; { it's protected mode only }
  222. IF_UNDOC = $00001000; { it's an undocumented instruction }
  223. IF_FPU = $00002000; { it's an FPU instruction }
  224. IF_MMX = $00004000; { it's an MMX instruction }
  225. { it's a 3DNow! instruction }
  226. IF_3DNOW = $00008000;
  227. { it's a SSE (KNI, MMX2) instruction }
  228. IF_SSE = $00010000;
  229. { SSE2 instructions }
  230. IF_SSE2 = $00020000;
  231. { the mask for processor types }
  232. {IF_PMASK = longint($FF000000);}
  233. { the mask for disassembly "prefer" }
  234. {IF_PFMASK = longint($F001FF00);}
  235. IF_8086 = $00000000; { 8086 instruction }
  236. IF_186 = $01000000; { 186+ instruction }
  237. IF_286 = $02000000; { 286+ instruction }
  238. IF_386 = $03000000; { 386+ instruction }
  239. IF_486 = $04000000; { 486+ instruction }
  240. IF_PENT = $05000000; { Pentium instruction }
  241. IF_P6 = $06000000; { P6 instruction }
  242. IF_KATMAI = $07000000; { Katmai instructions }
  243. { Willamette instructions }
  244. IF_WILLAMETTE = $08000000;
  245. IF_CYRIX = $10000000; { Cyrix-specific instruction }
  246. IF_AMD = $20000000; { AMD-specific instruction }
  247. { added flags }
  248. IF_PRE = $40000000; { it's a prefix instruction }
  249. IF_PASS2 = longint($80000000); { if the instruction can change in a second pass }
  250. type
  251. TInsTabCache=array[TasmOp] of longint;
  252. PInsTabCache=^TInsTabCache;
  253. const
  254. {$ifdef x86_64}
  255. InsTab:array[0..instabentries-1] of TInsEntry={$i x86_64ta.inc}
  256. {$else x86_64}
  257. InsTab:array[0..instabentries-1] of TInsEntry={$i i386tab.inc}
  258. {$endif x86_64}
  259. var
  260. InsTabCache : PInsTabCache;
  261. const
  262. {$ifdef x86_64}
  263. { Intel style operands ! }
  264. opsize_2_type:array[0..2,topsize] of longint=(
  265. (OT_NONE,
  266. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS16,OT_BITS32,OT_BITS32,OT_BITS64,OT_BITS64,OT_BITS64,
  267. OT_BITS16,OT_BITS32,OT_BITS64,
  268. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_BITS64,OT_BITS64,OT_NONE,
  269. OT_NEAR,OT_FAR,OT_SHORT
  270. ),
  271. (OT_NONE,
  272. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS8,OT_BITS8,OT_BITS16,OT_BITS8,OT_BITS16,OT_BITS32,
  273. OT_BITS16,OT_BITS32,OT_BITS64,
  274. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_BITS64,OT_BITS64,OT_NONE,
  275. OT_NEAR,OT_FAR,OT_SHORT
  276. ),
  277. (OT_NONE,
  278. OT_BITS8,OT_BITS16,OT_BITS32,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,
  279. OT_BITS16,OT_BITS32,OT_BITS64,
  280. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_BITS64,OT_BITS64,OT_NONE,
  281. OT_NEAR,OT_FAR,OT_SHORT
  282. )
  283. );
  284. { Convert reg to operand type }
  285. reg2type : array[firstreg..lastreg] of longint = (OT_NONE,
  286. OT_REG_RAX,OT_REG_RCX,OT_REG64,OT_REG64,OT_REG64,OT_REG64,OT_REG64,OT_REG64,
  287. OT_REG64,OT_REG64,OT_REG64,OT_REG64,OT_REG64,OT_REG64,OT_REG64,OT_REG64,OT_REG64,
  288. OT_REG_EAX,OT_REG_ECX,OT_REG32,OT_REG32,OT_REG32,OT_REG32,OT_REG32,OT_REG32,
  289. OT_REG32,OT_REG32,OT_REG32,OT_REG32,OT_REG32,OT_REG32,OT_REG32,OT_REG32,
  290. OT_REG_AX,OT_REG_CX,OT_REG_DX,OT_REG16,OT_REG16,OT_REG16,OT_REG16,OT_REG16,
  291. OT_REG16,OT_REG16,OT_REG16,OT_REG16,OT_REG16,OT_REG16,OT_REG16,OT_REG16,
  292. OT_REG_AL,OT_REG_CL,OT_REG8,OT_REG8,OT_REG8,OT_REG8,OT_REG8,OT_REG8,
  293. OT_REG8,OT_REG8,OT_REG8,OT_REG8,OT_REG8,OT_REG8,OT_REG8,OT_REG8,
  294. OT_REG8,OT_REG8,OT_REG8,OT_REG8,
  295. OT_REG_CS,OT_REG_DESS,OT_REG_DESS,OT_REG_DESS,OT_REG_FSGS,OT_REG_FSGS,
  296. OT_FPU0,OT_FPU0,OT_FPUREG,OT_FPUREG,OT_FPUREG,OT_FPUREG,OT_FPUREG,OT_FPUREG,OT_FPUREG,
  297. OT_REG_DREG,OT_REG_DREG,OT_REG_DREG,OT_REG_DREG,OT_REG_DREG,OT_REG_DREG,
  298. OT_REG_CREG,OT_REG_CREG,OT_REG_CREG,OT_REG_CR4,
  299. OT_REG_TREG,OT_REG_TREG,OT_REG_TREG,OT_REG_TREG,OT_REG_TREG,
  300. OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,
  301. OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,
  302. OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG
  303. );
  304. subreg2type:array[R_SUBL..R_SUBQ] of longint = (
  305. OT_REG8,OT_REG8,OT_REG16,OT_REG32,OT_REG64
  306. );
  307. {$else x86_64}
  308. { Intel style operands ! }
  309. opsize_2_type:array[0..2,topsize] of longint=(
  310. (OT_NONE,
  311. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS16,OT_BITS32,OT_BITS32,
  312. OT_BITS16,OT_BITS32,OT_BITS64,
  313. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_BITS64,OT_BITS64,OT_NONE,
  314. OT_NEAR,OT_FAR,OT_SHORT
  315. ),
  316. (OT_NONE,
  317. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS8,OT_BITS8,OT_BITS16,
  318. OT_BITS16,OT_BITS32,OT_BITS64,
  319. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_BITS64,OT_BITS64,OT_NONE,
  320. OT_NEAR,OT_FAR,OT_SHORT
  321. ),
  322. (OT_NONE,
  323. OT_BITS8,OT_BITS16,OT_BITS32,OT_NONE,OT_NONE,OT_NONE,
  324. OT_BITS16,OT_BITS32,OT_BITS64,
  325. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_BITS64,OT_BITS64,OT_NONE,
  326. OT_NEAR,OT_FAR,OT_SHORT
  327. )
  328. );
  329. subreg2type:array[R_SUBL..R_SUBD] of longint = (
  330. OT_REG8,OT_REG8,OT_REG16,OT_REG32
  331. );
  332. { Convert reg to operand type }
  333. reg2type : array[firstreg..lastreg] of longint = (OT_NONE,
  334. OT_REG_EAX,OT_REG_ECX,OT_REG32,OT_REG32,OT_REG32,OT_REG32,OT_REG32,OT_REG32,
  335. OT_REG_AX,OT_REG_CX,OT_REG_DX,OT_REG16,OT_REG16,OT_REG16,OT_REG16,OT_REG16,
  336. OT_REG_AL,OT_REG_CL,OT_REG8,OT_REG8,OT_REG8,OT_REG8,OT_REG8,OT_REG8,
  337. OT_REG_CS,OT_REG_DESS,OT_REG_DESS,OT_REG_DESS,OT_REG_FSGS,OT_REG_FSGS,
  338. OT_FPU0,OT_FPU0,OT_FPUREG,OT_FPUREG,OT_FPUREG,OT_FPUREG,OT_FPUREG,OT_FPUREG,OT_FPUREG,
  339. OT_REG_DREG,OT_REG_DREG,OT_REG_DREG,OT_REG_DREG,OT_REG_DREG,OT_REG_DREG,
  340. OT_REG_CREG,OT_REG_CREG,OT_REG_CREG,OT_REG_CR4,
  341. OT_REG_TREG,OT_REG_TREG,OT_REG_TREG,OT_REG_TREG,OT_REG_TREG,
  342. OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,
  343. OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG
  344. );
  345. {$endif x86_64}
  346. {****************************************************************************
  347. TAI_ALIGN
  348. ****************************************************************************}
  349. constructor tai_align.create(b: byte);
  350. begin
  351. inherited create(b);
  352. reg.enum := R_ECX;
  353. end;
  354. constructor tai_align.create_op(b: byte; _op: byte);
  355. begin
  356. inherited create_op(b,_op);
  357. reg.enum := R_NO;
  358. end;
  359. function tai_align.calculatefillbuf(var buf : tfillbuffer):pchar;
  360. const
  361. alignarray:array[0..5] of string[8]=(
  362. #$8D#$B4#$26#$00#$00#$00#$00,
  363. #$8D#$B6#$00#$00#$00#$00,
  364. #$8D#$74#$26#$00,
  365. #$8D#$76#$00,
  366. #$89#$F6,
  367. #$90
  368. );
  369. var
  370. bufptr : pchar;
  371. j : longint;
  372. begin
  373. inherited calculatefillbuf(buf);
  374. if not use_op then
  375. begin
  376. bufptr:=pchar(@buf);
  377. while (fillsize>0) do
  378. begin
  379. for j:=0 to 5 do
  380. if (fillsize>=length(alignarray[j])) then
  381. break;
  382. move(alignarray[j][1],bufptr^,length(alignarray[j]));
  383. inc(bufptr,length(alignarray[j]));
  384. dec(fillsize,length(alignarray[j]));
  385. end;
  386. end;
  387. calculatefillbuf:=pchar(@buf);
  388. end;
  389. {*****************************************************************************
  390. Taicpu Constructors
  391. *****************************************************************************}
  392. procedure taicpu.changeopsize(siz:topsize);
  393. begin
  394. opsize:=siz;
  395. end;
  396. procedure taicpu.init(_size : topsize);
  397. begin
  398. { default order is att }
  399. FOperandOrder:=op_att;
  400. segprefix.enum:=R_NO;
  401. opsize:=_size;
  402. {$ifndef NOAG386BIN}
  403. insentry:=nil;
  404. LastInsOffset:=-1;
  405. InsOffset:=0;
  406. InsSize:=0;
  407. {$endif}
  408. end;
  409. constructor taicpu.op_none(op : tasmop;_size : topsize);
  410. begin
  411. inherited create(op);
  412. init(_size);
  413. end;
  414. constructor taicpu.op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  415. begin
  416. inherited create(op);
  417. init(_size);
  418. ops:=1;
  419. loadreg(0,_op1);
  420. end;
  421. constructor taicpu.op_const(op : tasmop;_size : topsize;_op1 : aword);
  422. begin
  423. inherited create(op);
  424. init(_size);
  425. ops:=1;
  426. loadconst(0,_op1);
  427. end;
  428. constructor taicpu.op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  429. begin
  430. inherited create(op);
  431. init(_size);
  432. ops:=1;
  433. loadref(0,_op1);
  434. end;
  435. constructor taicpu.op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  436. begin
  437. inherited create(op);
  438. init(_size);
  439. ops:=2;
  440. loadreg(0,_op1);
  441. loadreg(1,_op2);
  442. end;
  443. constructor taicpu.op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aword);
  444. begin
  445. inherited create(op);
  446. init(_size);
  447. ops:=2;
  448. loadreg(0,_op1);
  449. loadconst(1,_op2);
  450. end;
  451. constructor taicpu.op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  452. begin
  453. inherited create(op);
  454. init(_size);
  455. ops:=2;
  456. loadreg(0,_op1);
  457. loadref(1,_op2);
  458. end;
  459. constructor taicpu.op_const_reg(op : tasmop;_size : topsize;_op1 : aword;_op2 : tregister);
  460. begin
  461. inherited create(op);
  462. init(_size);
  463. ops:=2;
  464. loadconst(0,_op1);
  465. loadreg(1,_op2);
  466. end;
  467. constructor taicpu.op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aword);
  468. begin
  469. inherited create(op);
  470. init(_size);
  471. ops:=2;
  472. loadconst(0,_op1);
  473. loadconst(1,_op2);
  474. end;
  475. constructor taicpu.op_const_ref(op : tasmop;_size : topsize;_op1 : aword;const _op2 : treference);
  476. begin
  477. inherited create(op);
  478. init(_size);
  479. ops:=2;
  480. loadconst(0,_op1);
  481. loadref(1,_op2);
  482. end;
  483. constructor taicpu.op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  484. begin
  485. inherited create(op);
  486. init(_size);
  487. ops:=2;
  488. loadref(0,_op1);
  489. loadreg(1,_op2);
  490. end;
  491. constructor taicpu.op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  492. begin
  493. inherited create(op);
  494. init(_size);
  495. ops:=3;
  496. loadreg(0,_op1);
  497. loadreg(1,_op2);
  498. loadreg(2,_op3);
  499. end;
  500. constructor taicpu.op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aword;_op2 : tregister;_op3 : tregister);
  501. begin
  502. inherited create(op);
  503. init(_size);
  504. ops:=3;
  505. loadconst(0,_op1);
  506. loadreg(1,_op2);
  507. loadreg(2,_op3);
  508. end;
  509. constructor taicpu.op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister;const _op3 : treference);
  510. begin
  511. inherited create(op);
  512. init(_size);
  513. ops:=3;
  514. loadreg(0,_op1);
  515. loadreg(1,_op2);
  516. loadref(2,_op3);
  517. end;
  518. constructor taicpu.op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aword;const _op2 : treference;_op3 : tregister);
  519. begin
  520. inherited create(op);
  521. init(_size);
  522. ops:=3;
  523. loadconst(0,_op1);
  524. loadref(1,_op2);
  525. loadreg(2,_op3);
  526. end;
  527. constructor taicpu.op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aword;_op2 : tregister;const _op3 : treference);
  528. begin
  529. inherited create(op);
  530. init(_size);
  531. ops:=3;
  532. loadconst(0,_op1);
  533. loadreg(1,_op2);
  534. loadref(2,_op3);
  535. end;
  536. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  537. begin
  538. inherited create(op);
  539. init(_size);
  540. condition:=cond;
  541. ops:=1;
  542. loadsymbol(0,_op1,0);
  543. end;
  544. constructor taicpu.op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  545. begin
  546. inherited create(op);
  547. init(_size);
  548. ops:=1;
  549. loadsymbol(0,_op1,0);
  550. end;
  551. constructor taicpu.op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  552. begin
  553. inherited create(op);
  554. init(_size);
  555. ops:=1;
  556. loadsymbol(0,_op1,_op1ofs);
  557. end;
  558. constructor taicpu.op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  559. begin
  560. inherited create(op);
  561. init(_size);
  562. ops:=2;
  563. loadsymbol(0,_op1,_op1ofs);
  564. loadreg(1,_op2);
  565. end;
  566. constructor taicpu.op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  567. begin
  568. inherited create(op);
  569. init(_size);
  570. ops:=2;
  571. loadsymbol(0,_op1,_op1ofs);
  572. loadref(1,_op2);
  573. end;
  574. function taicpu.GetString:string;
  575. var
  576. i : longint;
  577. s : string;
  578. addsize : boolean;
  579. begin
  580. s:='['+std_op2str[opcode];
  581. for i:=1to ops do
  582. begin
  583. if i=1 then
  584. s:=s+' '
  585. else
  586. s:=s+',';
  587. { type }
  588. addsize:=false;
  589. if (oper[i-1].ot and OT_XMMREG)=OT_XMMREG then
  590. s:=s+'xmmreg'
  591. else
  592. if (oper[i-1].ot and OT_MMXREG)=OT_MMXREG then
  593. s:=s+'mmxreg'
  594. else
  595. if (oper[i-1].ot and OT_FPUREG)=OT_FPUREG then
  596. s:=s+'fpureg'
  597. else
  598. if (oper[i-1].ot and OT_REGISTER)=OT_REGISTER then
  599. begin
  600. s:=s+'reg';
  601. addsize:=true;
  602. end
  603. else
  604. if (oper[i-1].ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  605. begin
  606. s:=s+'imm';
  607. addsize:=true;
  608. end
  609. else
  610. if (oper[i-1].ot and OT_MEMORY)=OT_MEMORY then
  611. begin
  612. s:=s+'mem';
  613. addsize:=true;
  614. end
  615. else
  616. s:=s+'???';
  617. { size }
  618. if addsize then
  619. begin
  620. if (oper[i-1].ot and OT_BITS8)<>0 then
  621. s:=s+'8'
  622. else
  623. if (oper[i-1].ot and OT_BITS16)<>0 then
  624. s:=s+'16'
  625. else
  626. if (oper[i-1].ot and OT_BITS32)<>0 then
  627. s:=s+'32'
  628. else
  629. s:=s+'??';
  630. { signed }
  631. if (oper[i-1].ot and OT_SIGNED)<>0 then
  632. s:=s+'s';
  633. end;
  634. end;
  635. GetString:=s+']';
  636. end;
  637. procedure taicpu.Swapoperands;
  638. var
  639. p : TOper;
  640. begin
  641. { Fix the operands which are in AT&T style and we need them in Intel style }
  642. case ops of
  643. 2 : begin
  644. { 0,1 -> 1,0 }
  645. p:=oper[0];
  646. oper[0]:=oper[1];
  647. oper[1]:=p;
  648. end;
  649. 3 : begin
  650. { 0,1,2 -> 2,1,0 }
  651. p:=oper[0];
  652. oper[0]:=oper[2];
  653. oper[2]:=p;
  654. end;
  655. end;
  656. end;
  657. procedure taicpu.SetOperandOrder(order:TOperandOrder);
  658. begin
  659. if FOperandOrder<>order then
  660. begin
  661. Swapoperands;
  662. FOperandOrder:=order;
  663. end;
  664. end;
  665. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  666. begin
  667. o.typ:=toptype(ppufile.getbyte);
  668. o.ot:=ppufile.getlongint;
  669. case o.typ of
  670. top_reg :
  671. ppufile.getdata(o.reg,sizeof(Tregister));
  672. top_ref :
  673. begin
  674. new(o.ref);
  675. ppufile.getdata(o.ref^.segment,sizeof(Tregister));
  676. ppufile.getdata(o.ref^.base,sizeof(Tregister));
  677. ppufile.getdata(o.ref^.index,sizeof(Tregister));
  678. o.ref^.scalefactor:=ppufile.getbyte;
  679. o.ref^.offset:=ppufile.getlongint;
  680. o.ref^.symbol:=ppufile.getasmsymbol;
  681. o.ref^.offsetfixup:=ppufile.getlongint;
  682. o.ref^.options:=trefoptions(ppufile.getbyte);
  683. end;
  684. top_const :
  685. o.val:=aword(ppufile.getlongint);
  686. top_symbol :
  687. begin
  688. o.sym:=ppufile.getasmsymbol;
  689. o.symofs:=ppufile.getlongint;
  690. end;
  691. end;
  692. end;
  693. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  694. begin
  695. ppufile.putbyte(byte(o.typ));
  696. ppufile.putlongint(o.ot);
  697. case o.typ of
  698. top_reg :
  699. ppufile.putdata(o.reg,sizeof(Tregister));
  700. top_ref :
  701. begin
  702. ppufile.putdata(o.ref^.segment,sizeof(Tregister));
  703. ppufile.putdata(o.ref^.base,sizeof(Tregister));
  704. ppufile.putdata(o.ref^.index,sizeof(Tregister));
  705. ppufile.putbyte(o.ref^.scalefactor);
  706. ppufile.putlongint(o.ref^.offset);
  707. ppufile.putasmsymbol(o.ref^.symbol);
  708. ppufile.putlongint(o.ref^.offsetfixup);
  709. ppufile.putbyte(byte(o.ref^.options));
  710. end;
  711. top_const :
  712. ppufile.putlongint(longint(o.val));
  713. top_symbol :
  714. begin
  715. ppufile.putasmsymbol(o.sym);
  716. ppufile.putlongint(longint(o.symofs));
  717. end;
  718. end;
  719. end;
  720. procedure taicpu.ppuderefoper(var o:toper);
  721. begin
  722. case o.typ of
  723. top_ref :
  724. begin
  725. if assigned(o.ref^.symbol) then
  726. objectlibrary.derefasmsymbol(o.ref^.symbol);
  727. end;
  728. top_symbol :
  729. objectlibrary.derefasmsymbol(o.sym);
  730. end;
  731. end;
  732. procedure taicpu.CheckNonCommutativeOpcodes;
  733. begin
  734. { we need ATT order }
  735. SetOperandOrder(op_att);
  736. if ((ops=2) and
  737. (oper[0].typ=top_reg) and
  738. (oper[1].typ=top_reg) and
  739. { if the first is ST and the second is also a register
  740. it is necessarily ST1 .. ST7 }
  741. (oper[0].reg.enum in [R_ST..R_ST0])) or
  742. { ((ops=1) and
  743. (oper[0].typ=top_reg) and
  744. (oper[0].reg in [R_ST1..R_ST7])) or}
  745. (ops=0) then
  746. if opcode=A_FSUBR then
  747. opcode:=A_FSUB
  748. else if opcode=A_FSUB then
  749. opcode:=A_FSUBR
  750. else if opcode=A_FDIVR then
  751. opcode:=A_FDIV
  752. else if opcode=A_FDIV then
  753. opcode:=A_FDIVR
  754. else if opcode=A_FSUBRP then
  755. opcode:=A_FSUBP
  756. else if opcode=A_FSUBP then
  757. opcode:=A_FSUBRP
  758. else if opcode=A_FDIVRP then
  759. opcode:=A_FDIVP
  760. else if opcode=A_FDIVP then
  761. opcode:=A_FDIVRP;
  762. if ((ops=1) and
  763. (oper[0].typ=top_reg) and
  764. (oper[0].reg.enum in [R_ST1..R_ST7])) then
  765. if opcode=A_FSUBRP then
  766. opcode:=A_FSUBP
  767. else if opcode=A_FSUBP then
  768. opcode:=A_FSUBRP
  769. else if opcode=A_FDIVRP then
  770. opcode:=A_FDIVP
  771. else if opcode=A_FDIVP then
  772. opcode:=A_FDIVRP;
  773. end;
  774. {*****************************************************************************
  775. Assembler
  776. *****************************************************************************}
  777. {$ifndef NOAG386BIN}
  778. type
  779. ea=packed record
  780. sib_present : boolean;
  781. bytes : byte;
  782. size : byte;
  783. modrm : byte;
  784. sib : byte;
  785. end;
  786. procedure taicpu.create_ot;
  787. {
  788. this function will also fix some other fields which only needs to be once
  789. }
  790. var
  791. i,l,relsize : longint;
  792. nb,ni:boolean;
  793. begin
  794. if ops=0 then
  795. exit;
  796. { update oper[].ot field }
  797. for i:=0 to ops-1 do
  798. with oper[i] do
  799. begin
  800. case typ of
  801. top_reg :
  802. begin
  803. if reg.enum=R_INTREGISTER then
  804. case reg.number of
  805. NR_AL:
  806. ot:=OT_REG_AL;
  807. NR_AX:
  808. ot:=OT_REG_AX;
  809. NR_EAX:
  810. ot:=OT_REG_EAX;
  811. NR_CL:
  812. ot:=OT_REG_CL;
  813. NR_CX:
  814. ot:=OT_REG_CX;
  815. NR_ECX:
  816. ot:=OT_REG_ECX;
  817. NR_DX:
  818. ot:=OT_REG_DX;
  819. NR_CS:
  820. ot:=OT_REG_CS;
  821. NR_DS,NR_ES,NR_SS:
  822. ot:=OT_REG_DESS;
  823. NR_FS,NR_GS:
  824. ot:=OT_REG_FSGS;
  825. NR_DR0..NR_DR7:
  826. ot:=OT_REG_DREG;
  827. NR_CR0..NR_CR3:
  828. ot:=OT_REG_CREG;
  829. NR_CR4:
  830. ot:=OT_REG_CR4;
  831. NR_TR3..NR_TR7:
  832. ot:=OT_REG_TREG;
  833. else
  834. ot:=subreg2type[reg.number and $ff];
  835. end
  836. else
  837. ot:=reg2type[reg.enum];
  838. end;
  839. top_ref :
  840. begin
  841. nb:=(ref^.base.enum=R_NO) or
  842. ((ref^.base.enum=R_INTREGISTER) and (ref^.base.number=NR_NO));
  843. ni:=(ref^.index.enum=R_NO) or
  844. ((ref^.index.enum=R_INTREGISTER) and (ref^.index.number=NR_NO));
  845. { create ot field }
  846. if (ot and OT_SIZE_MASK)=0 then
  847. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  848. else
  849. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  850. if nb and ni then
  851. ot:=ot or OT_MEM_OFFS;
  852. { fix scalefactor }
  853. if ni then
  854. ref^.scalefactor:=0
  855. else
  856. if (ref^.scalefactor=0) then
  857. ref^.scalefactor:=1;
  858. end;
  859. top_const :
  860. begin
  861. if (opsize<>S_W) and (longint(val)>=-128) and (val<=127) then
  862. ot:=OT_IMM8 or OT_SIGNED
  863. else
  864. ot:=OT_IMMEDIATE or opsize_2_type[i,opsize];
  865. end;
  866. top_symbol :
  867. begin
  868. if LastInsOffset=-1 then
  869. l:=0
  870. else
  871. l:=InsOffset-LastInsOffset;
  872. inc(l,symofs);
  873. if assigned(sym) then
  874. inc(l,sym.address);
  875. { instruction size will then always become 2 (PFV) }
  876. relsize:=(InsOffset+2)-l;
  877. if (not assigned(sym) or
  878. ((sym.currbind<>AB_EXTERNAL) and (sym.address<>0))) and
  879. (relsize>=-128) and (relsize<=127) then
  880. ot:=OT_IMM32 or OT_SHORT
  881. else
  882. ot:=OT_IMM32 or OT_NEAR;
  883. end;
  884. end;
  885. end;
  886. end;
  887. function taicpu.InsEnd:longint;
  888. begin
  889. InsEnd:=InsOffset+InsSize;
  890. end;
  891. function taicpu.Matches(p:PInsEntry):longint;
  892. { * IF_SM stands for Size Match: any operand whose size is not
  893. * explicitly specified by the template is `really' intended to be
  894. * the same size as the first size-specified operand.
  895. * Non-specification is tolerated in the input instruction, but
  896. * _wrong_ specification is not.
  897. *
  898. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  899. * three-operand instructions such as SHLD: it implies that the
  900. * first two operands must match in size, but that the third is
  901. * required to be _unspecified_.
  902. *
  903. * IF_SB invokes Size Byte: operands with unspecified size in the
  904. * template are really bytes, and so no non-byte specification in
  905. * the input instruction will be tolerated. IF_SW similarly invokes
  906. * Size Word, and IF_SD invokes Size Doubleword.
  907. *
  908. * (The default state if neither IF_SM nor IF_SM2 is specified is
  909. * that any operand with unspecified size in the template is
  910. * required to have unspecified size in the instruction too...)
  911. }
  912. var
  913. i,j,asize,oprs : longint;
  914. siz : array[0..2] of longint;
  915. begin
  916. Matches:=100;
  917. { Check the opcode and operands }
  918. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  919. begin
  920. Matches:=0;
  921. exit;
  922. end;
  923. { Check that no spurious colons or TOs are present }
  924. for i:=0 to p^.ops-1 do
  925. if (oper[i].ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  926. begin
  927. Matches:=0;
  928. exit;
  929. end;
  930. { Check that the operand flags all match up }
  931. for i:=0 to p^.ops-1 do
  932. begin
  933. if ((p^.optypes[i] and (not oper[i].ot)) or
  934. ((p^.optypes[i] and OT_SIZE_MASK) and
  935. ((p^.optypes[i] xor oper[i].ot) and OT_SIZE_MASK)))<>0 then
  936. begin
  937. if ((p^.optypes[i] and (not oper[i].ot) and OT_NON_SIZE) or
  938. (oper[i].ot and OT_SIZE_MASK))<>0 then
  939. begin
  940. Matches:=0;
  941. exit;
  942. end
  943. else
  944. Matches:=1;
  945. end;
  946. end;
  947. { Check operand sizes }
  948. { as default an untyped size can get all the sizes, this is different
  949. from nasm, but else we need to do a lot checking which opcodes want
  950. size or not with the automatic size generation }
  951. asize:=longint($ffffffff);
  952. if (p^.flags and IF_SB)<>0 then
  953. asize:=OT_BITS8
  954. else if (p^.flags and IF_SW)<>0 then
  955. asize:=OT_BITS16
  956. else if (p^.flags and IF_SD)<>0 then
  957. asize:=OT_BITS32;
  958. if (p^.flags and IF_ARMASK)<>0 then
  959. begin
  960. siz[0]:=0;
  961. siz[1]:=0;
  962. siz[2]:=0;
  963. if (p^.flags and IF_AR0)<>0 then
  964. siz[0]:=asize
  965. else if (p^.flags and IF_AR1)<>0 then
  966. siz[1]:=asize
  967. else if (p^.flags and IF_AR2)<>0 then
  968. siz[2]:=asize;
  969. end
  970. else
  971. begin
  972. { we can leave because the size for all operands is forced to be
  973. the same
  974. but not if IF_SB IF_SW or IF_SD is set PM }
  975. if asize=-1 then
  976. exit;
  977. siz[0]:=asize;
  978. siz[1]:=asize;
  979. siz[2]:=asize;
  980. end;
  981. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  982. begin
  983. if (p^.flags and IF_SM2)<>0 then
  984. oprs:=2
  985. else
  986. oprs:=p^.ops;
  987. for i:=0 to oprs-1 do
  988. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  989. begin
  990. for j:=0 to oprs-1 do
  991. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  992. break;
  993. end;
  994. end
  995. else
  996. oprs:=2;
  997. { Check operand sizes }
  998. for i:=0 to p^.ops-1 do
  999. begin
  1000. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  1001. ((oper[i].ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1002. { Immediates can always include smaller size }
  1003. ((oper[i].ot and OT_IMMEDIATE)=0) and
  1004. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i].ot and OT_SIZE_MASK)) then
  1005. Matches:=2;
  1006. end;
  1007. end;
  1008. procedure taicpu.ResetPass1;
  1009. begin
  1010. { we need to reset everything here, because the choosen insentry
  1011. can be invalid for a new situation where the previously optimized
  1012. insentry is not correct }
  1013. InsEntry:=nil;
  1014. InsSize:=0;
  1015. LastInsOffset:=-1;
  1016. end;
  1017. procedure taicpu.ResetPass2;
  1018. begin
  1019. { we are here in a second pass, check if the instruction can be optimized }
  1020. if assigned(InsEntry) and
  1021. ((InsEntry^.flags and IF_PASS2)<>0) then
  1022. begin
  1023. InsEntry:=nil;
  1024. InsSize:=0;
  1025. end;
  1026. LastInsOffset:=-1;
  1027. end;
  1028. function taicpu.CheckIfValid:boolean;
  1029. var
  1030. m,i : longint;
  1031. begin
  1032. CheckIfValid:=false;
  1033. { Things which may only be done once, not when a second pass is done to
  1034. optimize }
  1035. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1036. begin
  1037. { We need intel style operands }
  1038. SetOperandOrder(op_intel);
  1039. { create the .ot fields }
  1040. create_ot;
  1041. { set the file postion }
  1042. aktfilepos:=fileinfo;
  1043. end
  1044. else
  1045. begin
  1046. { we've already an insentry so it's valid }
  1047. CheckIfValid:=true;
  1048. exit;
  1049. end;
  1050. { Lookup opcode in the table }
  1051. InsSize:=-1;
  1052. i:=instabcache^[opcode];
  1053. if i=-1 then
  1054. begin
  1055. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1056. exit;
  1057. end;
  1058. insentry:=@instab[i];
  1059. while (insentry^.opcode=opcode) do
  1060. begin
  1061. m:=matches(insentry);
  1062. if m=100 then
  1063. begin
  1064. InsSize:=calcsize(insentry);
  1065. if not((segprefix.enum=R_NO) or ((segprefix.enum=R_INTREGISTER) and (segprefix.number=NR_NO))) then
  1066. inc(InsSize);
  1067. { For opsize if size if forced }
  1068. if (insentry^.flags and (IF_SB or IF_SW or IF_SD))<>0 then
  1069. begin
  1070. if (insentry^.flags and IF_ARMASK)=0 then
  1071. begin
  1072. if (insentry^.flags and IF_SB)<>0 then
  1073. begin
  1074. if opsize=S_NO then
  1075. opsize:=S_B;
  1076. end
  1077. else if (insentry^.flags and IF_SW)<>0 then
  1078. begin
  1079. if opsize=S_NO then
  1080. opsize:=S_W;
  1081. end
  1082. else if (insentry^.flags and IF_SD)<>0 then
  1083. begin
  1084. if opsize=S_NO then
  1085. opsize:=S_L;
  1086. end;
  1087. end;
  1088. end;
  1089. CheckIfValid:=true;
  1090. exit;
  1091. end;
  1092. inc(i);
  1093. insentry:=@instab[i];
  1094. end;
  1095. if insentry^.opcode<>opcode then
  1096. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1097. { No instruction found, set insentry to nil and inssize to -1 }
  1098. insentry:=nil;
  1099. inssize:=-1;
  1100. end;
  1101. function taicpu.Pass1(offset:longint):longint;
  1102. begin
  1103. Pass1:=0;
  1104. { Save the old offset and set the new offset }
  1105. InsOffset:=Offset;
  1106. { Things which may only be done once, not when a second pass is done to
  1107. optimize }
  1108. if Insentry=nil then
  1109. begin
  1110. { Check if error last time then InsSize=-1 }
  1111. if InsSize=-1 then
  1112. exit;
  1113. { set the file postion }
  1114. aktfilepos:=fileinfo;
  1115. end
  1116. else
  1117. begin
  1118. {$ifdef PASS2FLAG}
  1119. { we are here in a second pass, check if the instruction can be optimized }
  1120. if (InsEntry^.flags and IF_PASS2)=0 then
  1121. begin
  1122. Pass1:=InsSize;
  1123. exit;
  1124. end;
  1125. { update the .ot fields, some top_const can be updated }
  1126. create_ot;
  1127. {$endif PASS2FLAG}
  1128. end;
  1129. { Check if it's a valid instruction }
  1130. if CheckIfValid then
  1131. begin
  1132. LastInsOffset:=InsOffset;
  1133. Pass1:=InsSize;
  1134. exit;
  1135. end;
  1136. LastInsOffset:=-1;
  1137. end;
  1138. procedure taicpu.Pass2(sec:TAsmObjectData);
  1139. var
  1140. c : longint;
  1141. begin
  1142. { error in pass1 ? }
  1143. if insentry=nil then
  1144. exit;
  1145. aktfilepos:=fileinfo;
  1146. { Segment override }
  1147. if segprefix.enum>lastreg then
  1148. internalerror(200201081);
  1149. if segprefix.enum=R_INTREGISTER then
  1150. begin
  1151. if segprefix.number<>NR_NO then
  1152. begin
  1153. case segprefix.number of
  1154. NR_CS: c:=$2e;
  1155. NR_DS: c:=$3e;
  1156. NR_ES: c:=$26;
  1157. NR_FS: c:=$64;
  1158. NR_GS: c:=$65;
  1159. NR_SS: c:=$36;
  1160. end;
  1161. sec.writebytes(c,1);
  1162. { fix the offset for GenNode }
  1163. inc(InsOffset);
  1164. end;
  1165. end
  1166. else
  1167. if (segprefix.enum<>R_NO) then
  1168. begin
  1169. case segprefix.enum of
  1170. R_CS : c:=$2e;
  1171. R_DS : c:=$3e;
  1172. R_ES : c:=$26;
  1173. R_FS : c:=$64;
  1174. R_GS : c:=$65;
  1175. R_SS : c:=$36;
  1176. end;
  1177. sec.writebytes(c,1);
  1178. { fix the offset for GenNode }
  1179. inc(InsOffset);
  1180. end;
  1181. { Generate the instruction }
  1182. GenCode(sec);
  1183. end;
  1184. function taicpu.needaddrprefix(opidx:byte):boolean;
  1185. var i,b:Tnewregister;
  1186. ia,ba:boolean;
  1187. begin
  1188. needaddrprefix:=false;
  1189. if (OT_MEMORY and (not oper[opidx].ot))=0 then
  1190. begin
  1191. if oper[opidx].ref^.index.enum=R_INTREGISTER then
  1192. begin
  1193. i:=oper[opidx].ref^.index.number;
  1194. ia:=(i<>NR_NO) and (i and $ff<>R_SUBD);
  1195. end
  1196. else
  1197. ia:=not(oper[opidx].ref^.index.enum in [R_NO,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI]);
  1198. if oper[opidx].ref^.base.enum=R_INTREGISTER then
  1199. begin
  1200. b:=oper[opidx].ref^.base.number;
  1201. ba:=(b<>NR_NO) and (b and $ff<>R_SUBD);
  1202. end
  1203. else
  1204. ba:=not(oper[opidx].ref^.base.enum in [R_NO,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI]);
  1205. b:=oper[opidx].ref^.base.number;
  1206. i:=oper[opidx].ref^.index.number;
  1207. if ia or ba then
  1208. needaddrprefix:=true;
  1209. end;
  1210. end;
  1211. function regval(r:Toldregister):byte;
  1212. begin
  1213. case r of
  1214. R_EAX,R_AX,R_AL,R_ES,R_CR0,R_DR0,R_ST,R_ST0,R_MM0,R_XMM0 :
  1215. regval:=0;
  1216. R_ECX,R_CX,R_CL,R_CS,R_DR1,R_ST1,R_MM1,R_XMM1 :
  1217. regval:=1;
  1218. R_EDX,R_DX,R_DL,R_SS,R_CR2,R_DR2,R_ST2,R_MM2,R_XMM2 :
  1219. regval:=2;
  1220. R_EBX,R_BX,R_BL,R_DS,R_CR3,R_DR3,R_TR3,R_ST3,R_MM3,R_XMM3 :
  1221. regval:=3;
  1222. R_ESP,R_SP,R_AH,R_FS,R_CR4,R_TR4,R_ST4,R_MM4,R_XMM4 :
  1223. regval:=4;
  1224. R_EBP,R_BP,R_CH,R_GS,R_TR5,R_ST5,R_MM5,R_XMM5 :
  1225. regval:=5;
  1226. R_ESI,R_SI,R_DH,R_DR6,R_TR6,R_ST6,R_MM6,R_XMM6 :
  1227. regval:=6;
  1228. R_EDI,R_DI,R_BH,R_DR7,R_TR7,R_ST7,R_MM7,R_XMM7 :
  1229. regval:=7;
  1230. else
  1231. begin
  1232. internalerror(777001);
  1233. regval:=0;
  1234. end;
  1235. end;
  1236. end;
  1237. function regval_new(r:Tnewregister):byte;
  1238. const count=45;
  1239. bsstart=32;
  1240. registers:array[0..count-1] of Tnewregister=(
  1241. NR_CS, NR_DS, NR_ES, NR_SS,
  1242. NR_FS, NR_GS, NR_DR0, NR_DR1,
  1243. NR_DR2, NR_DR3, NR_DR6, NR_DR7,
  1244. NR_CR0, NR_CR2, NR_CR3, NR_CR4,
  1245. NR_TR3, NR_TR4, NR_TR5, NR_TR6,
  1246. NR_TR7, NR_AL, NR_AH, NR_AX,
  1247. NR_EAX, NR_BL, NR_BH, NR_BX,
  1248. NR_EBX, NR_CL, NR_CH, NR_CX,
  1249. NR_ECX, NR_DL, NR_DH, NR_DX,
  1250. NR_EDX, NR_SI, NR_ESI, NR_DI,
  1251. NR_EDI, NR_BP, NR_EBP, NR_SP,
  1252. NR_ESP);
  1253. register_values:array[0..count-1] of byte=(
  1254. 1, 3, 0, 2,
  1255. 4, 5, 0, 1,
  1256. 2, 3, 6, 7,
  1257. 0, 2, 3, 4,
  1258. 3, 4, 5, 6,
  1259. 7, 0, 4, 0,
  1260. 0, 3, 7, 3,
  1261. 3, 1, 5, 1,
  1262. 1, 2, 6, 2,
  1263. 2, 6, 6, 7,
  1264. 7, 5, 5, 4,
  1265. 4);
  1266. var i,p:byte;
  1267. begin
  1268. {Binary search.}
  1269. p:=0;
  1270. i:=bsstart;
  1271. while i<>0 do
  1272. begin
  1273. if (p+i<count) and (registers[p+i]<=r) then
  1274. p:=p+i;
  1275. i:=i shr 1;
  1276. end;
  1277. if registers[p]=r then
  1278. regval_new:=register_values[p]
  1279. else
  1280. internalerror(777001);
  1281. end;
  1282. function process_ea(const input:toper;var output:ea;rfield:longint):boolean;
  1283. const
  1284. regs : array[0..63] of Toldregister=(
  1285. R_MM0, R_EAX, R_AX, R_AL, R_XMM0, R_NO, R_NO, R_NO,
  1286. R_MM1, R_ECX, R_CX, R_CL, R_XMM1, R_NO, R_NO, R_NO,
  1287. R_MM2, R_EDX, R_DX, R_DL, R_XMM2, R_NO, R_NO, R_NO,
  1288. R_MM3, R_EBX, R_BX, R_BL, R_XMM3, R_NO, R_NO, R_NO,
  1289. R_MM4, R_ESP, R_SP, R_AH, R_XMM4, R_NO, R_NO, R_NO,
  1290. R_MM5, R_EBP, R_BP, R_CH, R_XMM5, R_NO, R_NO, R_NO,
  1291. R_MM6, R_ESI, R_SI, R_DH, R_XMM6, R_NO, R_NO, R_NO,
  1292. R_MM7, R_EDI, R_DI, R_BH, R_XMM7, R_NO, R_NO, R_NO
  1293. );
  1294. var
  1295. j : longint;
  1296. { i,b : Toldregister;}
  1297. sym : tasmsymbol;
  1298. md,s,rv : byte;
  1299. base,index,scalefactor,
  1300. o : longint;
  1301. ireg : Tregister;
  1302. ir,br : Tnewregister;
  1303. begin
  1304. process_ea:=false;
  1305. { register ? }
  1306. if (input.typ=top_reg) then
  1307. begin
  1308. ireg:=input.reg;
  1309. if ireg.enum=R_INTREGISTER then
  1310. rv:=regval_new(ireg.number)
  1311. else
  1312. begin
  1313. j:=0;
  1314. while (j<=high(regs)) do
  1315. begin
  1316. if ireg.enum=regs[j] then
  1317. break;
  1318. inc(j);
  1319. end;
  1320. if j<=high(regs) then
  1321. rv:=j shr 3
  1322. else
  1323. rv:=255;
  1324. end;
  1325. if rv<>255 then
  1326. begin
  1327. output.sib_present:=false;
  1328. output.bytes:=0;
  1329. output.modrm:=$c0 or (rfield shl 3) or rv;
  1330. output.size:=1;
  1331. process_ea:=true;
  1332. end;
  1333. exit;
  1334. end;
  1335. { memory reference }
  1336. if (input.ref^.index.enum<>R_INTREGISTER) or (input.ref^.base.enum<>R_INTREGISTER) then
  1337. internalerror(200301081);
  1338. ir:=input.ref^.index.number;
  1339. br:=input.ref^.base.number;
  1340. { convert_register_to_enum(ir);
  1341. convert_register_to_enum(br);
  1342. i:=ir.enum;
  1343. b:=br.enum;}
  1344. s:=input.ref^.scalefactor;
  1345. o:=input.ref^.offset+input.ref^.offsetfixup;
  1346. sym:=input.ref^.symbol;
  1347. { it's direct address }
  1348. if (br=NR_NO) and (ir=NR_NO) then
  1349. begin
  1350. { it's a pure offset }
  1351. output.sib_present:=false;
  1352. output.bytes:=4;
  1353. output.modrm:=5 or (rfield shl 3);
  1354. end
  1355. else
  1356. { it's an indirection }
  1357. begin
  1358. { 16 bit address? }
  1359. if ((ir<>NR_NO) and (ir and $ff<>R_SUBD)) or ((br<>NR_NO) and (br and $ff<>R_SUBD)) then
  1360. message(asmw_e_16bit_not_supported);
  1361. {$ifdef OPTEA}
  1362. { make single reg base }
  1363. if (br=NR_NO) and (s=1) then
  1364. begin
  1365. br:=ir;
  1366. ir:=NR_NO;
  1367. end;
  1368. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  1369. if (br=NR_NO) and
  1370. (((s=2) and (ir<>NR_ESP)) or
  1371. (s=3) or (s=5) or (s=9)) then
  1372. begin
  1373. br:=ir;
  1374. dec(s);
  1375. end;
  1376. { swap ESP into base if scalefactor is 1 }
  1377. if (s=1) and (ir=NR_ESP) then
  1378. begin
  1379. ir:=br;
  1380. br:=NR_ESP;
  1381. end;
  1382. {$endif OPTEA}
  1383. { wrong, for various reasons }
  1384. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1385. exit;
  1386. { base }
  1387. case br of
  1388. NR_EAX : base:=0;
  1389. NR_ECX : base:=1;
  1390. NR_EDX : base:=2;
  1391. NR_EBX : base:=3;
  1392. NR_ESP : base:=4;
  1393. NR_NO,
  1394. NR_EBP : base:=5;
  1395. NR_ESI : base:=6;
  1396. NR_EDI : base:=7;
  1397. else
  1398. exit;
  1399. end;
  1400. { index }
  1401. case ir of
  1402. NR_EAX : index:=0;
  1403. NR_ECX : index:=1;
  1404. NR_EDX : index:=2;
  1405. NR_EBX : index:=3;
  1406. NR_NO : index:=4;
  1407. NR_EBP : index:=5;
  1408. NR_ESI : index:=6;
  1409. NR_EDI : index:=7;
  1410. else
  1411. exit;
  1412. end;
  1413. case s of
  1414. 0,
  1415. 1 : scalefactor:=0;
  1416. 2 : scalefactor:=1;
  1417. 4 : scalefactor:=2;
  1418. 8 : scalefactor:=3;
  1419. else
  1420. exit;
  1421. end;
  1422. if (br=NR_NO) or
  1423. ((br<>NR_EBP) and (o=0) and (sym=nil)) then
  1424. md:=0
  1425. else
  1426. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1427. md:=1
  1428. else
  1429. md:=2;
  1430. if (br=NR_NO) or (md=2) then
  1431. output.bytes:=4
  1432. else
  1433. output.bytes:=md;
  1434. { SIB needed ? }
  1435. if (ir=NR_NO) and (br<>NR_ESP) then
  1436. begin
  1437. output.sib_present:=false;
  1438. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1439. end
  1440. else
  1441. begin
  1442. output.sib_present:=true;
  1443. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1444. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1445. end;
  1446. end;
  1447. if output.sib_present then
  1448. output.size:=2+output.bytes
  1449. else
  1450. output.size:=1+output.bytes;
  1451. process_ea:=true;
  1452. end;
  1453. function taicpu.calcsize(p:PInsEntry):longint;
  1454. var
  1455. codes : pchar;
  1456. c : byte;
  1457. len : longint;
  1458. ea_data : ea;
  1459. begin
  1460. len:=0;
  1461. codes:=@p^.code;
  1462. repeat
  1463. c:=ord(codes^);
  1464. inc(codes);
  1465. case c of
  1466. 0 :
  1467. break;
  1468. 1,2,3 :
  1469. begin
  1470. inc(codes,c);
  1471. inc(len,c);
  1472. end;
  1473. 8,9,10 :
  1474. begin
  1475. inc(codes);
  1476. inc(len);
  1477. end;
  1478. 4,5,6,7 :
  1479. begin
  1480. if opsize=S_W then
  1481. inc(len,2)
  1482. else
  1483. inc(len);
  1484. end;
  1485. 15,
  1486. 12,13,14,
  1487. 16,17,18,
  1488. 20,21,22,
  1489. 40,41,42 :
  1490. inc(len);
  1491. 24,25,26,
  1492. 31,
  1493. 48,49,50 :
  1494. inc(len,2);
  1495. 28,29,30, { we don't have 16 bit immediates code }
  1496. 32,33,34,
  1497. 52,53,54,
  1498. 56,57,58 :
  1499. inc(len,4);
  1500. 192,193,194 :
  1501. if NeedAddrPrefix(c-192) then
  1502. inc(len);
  1503. 208 :
  1504. inc(len);
  1505. 200,
  1506. 201,
  1507. 202,
  1508. 209,
  1509. 210,
  1510. 217,218,219 : ;
  1511. 216 :
  1512. begin
  1513. inc(codes);
  1514. inc(len);
  1515. end;
  1516. 224,225,226 :
  1517. begin
  1518. InternalError(777002);
  1519. end;
  1520. else
  1521. begin
  1522. if (c>=64) and (c<=191) then
  1523. begin
  1524. if not process_ea(oper[(c shr 3) and 7], ea_data, 0) then
  1525. Message(asmw_e_invalid_effective_address)
  1526. else
  1527. inc(len,ea_data.size);
  1528. end
  1529. else
  1530. InternalError(777003);
  1531. end;
  1532. end;
  1533. until false;
  1534. calcsize:=len;
  1535. end;
  1536. procedure taicpu.GenCode(sec:TAsmObjectData);
  1537. {
  1538. * the actual codes (C syntax, i.e. octal):
  1539. * \0 - terminates the code. (Unless it's a literal of course.)
  1540. * \1, \2, \3 - that many literal bytes follow in the code stream
  1541. * \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
  1542. * (POP is never used for CS) depending on operand 0
  1543. * \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
  1544. * on operand 0
  1545. * \10, \11, \12 - a literal byte follows in the code stream, to be added
  1546. * to the register value of operand 0, 1 or 2
  1547. * \17 - encodes the literal byte 0. (Some compilers don't take
  1548. * kindly to a zero byte in the _middle_ of a compile time
  1549. * string constant, so I had to put this hack in.)
  1550. * \14, \15, \16 - a signed byte immediate operand, from operand 0, 1 or 2
  1551. * \20, \21, \22 - a byte immediate operand, from operand 0, 1 or 2
  1552. * \24, \25, \26 - an unsigned byte immediate operand, from operand 0, 1 or 2
  1553. * \30, \31, \32 - a word immediate operand, from operand 0, 1 or 2
  1554. * \34, \35, \36 - select between \3[012] and \4[012] depending on 16/32 bit
  1555. * assembly mode or the address-size override on the operand
  1556. * \37 - a word constant, from the _segment_ part of operand 0
  1557. * \40, \41, \42 - a long immediate operand, from operand 0, 1 or 2
  1558. * \50, \51, \52 - a byte relative operand, from operand 0, 1 or 2
  1559. * \60, \61, \62 - a word relative operand, from operand 0, 1 or 2
  1560. * \64, \65, \66 - select between \6[012] and \7[012] depending on 16/32 bit
  1561. * assembly mode or the address-size override on the operand
  1562. * \70, \71, \72 - a long relative operand, from operand 0, 1 or 2
  1563. * \1ab - a ModRM, calculated on EA in operand a, with the spare
  1564. * field the register value of operand b.
  1565. * \2ab - a ModRM, calculated on EA in operand a, with the spare
  1566. * field equal to digit b.
  1567. * \30x - might be an 0x67 byte, depending on the address size of
  1568. * the memory reference in operand x.
  1569. * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
  1570. * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
  1571. * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
  1572. * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
  1573. * \322 - indicates that this instruction is only valid when the
  1574. * operand size is the default (instruction to disassembler,
  1575. * generates no code in the assembler)
  1576. * \330 - a literal byte follows in the code stream, to be added
  1577. * to the condition code value of the instruction.
  1578. * \340 - reserve <operand 0> bytes of uninitialised storage.
  1579. * Operand 0 had better be a segmentless constant.
  1580. }
  1581. var
  1582. currval : longint;
  1583. currsym : tasmsymbol;
  1584. procedure getvalsym(opidx:longint);
  1585. begin
  1586. case oper[opidx].typ of
  1587. top_ref :
  1588. begin
  1589. currval:=oper[opidx].ref^.offset+oper[opidx].ref^.offsetfixup;
  1590. currsym:=oper[opidx].ref^.symbol;
  1591. end;
  1592. top_const :
  1593. begin
  1594. currval:=longint(oper[opidx].val);
  1595. currsym:=nil;
  1596. end;
  1597. top_symbol :
  1598. begin
  1599. currval:=oper[opidx].symofs;
  1600. currsym:=oper[opidx].sym;
  1601. end;
  1602. else
  1603. Message(asmw_e_immediate_or_reference_expected);
  1604. end;
  1605. end;
  1606. const
  1607. CondVal:array[TAsmCond] of byte=($0,
  1608. $7, $3, $2, $6, $2, $4, $F, $D, $C, $E, $6, $2,
  1609. $3, $7, $3, $5, $E, $C, $D, $F, $1, $B, $9, $5,
  1610. $0, $A, $A, $B, $8, $4);
  1611. var
  1612. c : byte;
  1613. pb,
  1614. codes : pchar;
  1615. bytes : array[0..3] of byte;
  1616. rfield,
  1617. data,s,opidx : longint;
  1618. ea_data : ea;
  1619. begin
  1620. {$ifdef EXTDEBUG}
  1621. { safety check }
  1622. if sec.sects[sec.currsec].datasize<>insoffset then
  1623. internalerror(200130121);
  1624. {$endif EXTDEBUG}
  1625. { load data to write }
  1626. codes:=insentry^.code;
  1627. { Force word push/pop for registers }
  1628. if (opsize=S_W) and ((codes[0]=#4) or (codes[0]=#6) or
  1629. ((codes[0]=#1) and ((codes[2]=#5) or (codes[2]=#7)))) then
  1630. begin
  1631. bytes[0]:=$66;
  1632. sec.writebytes(bytes,1);
  1633. end;
  1634. repeat
  1635. c:=ord(codes^);
  1636. inc(codes);
  1637. case c of
  1638. 0 :
  1639. break;
  1640. 1,2,3 :
  1641. begin
  1642. sec.writebytes(codes^,c);
  1643. inc(codes,c);
  1644. end;
  1645. 4,6 :
  1646. begin
  1647. case oper[0].reg.enum of
  1648. R_CS :
  1649. begin
  1650. if c=4 then
  1651. bytes[0]:=$f
  1652. else
  1653. bytes[0]:=$e;
  1654. end;
  1655. R_NO,
  1656. R_DS :
  1657. begin
  1658. if c=4 then
  1659. bytes[0]:=$1f
  1660. else
  1661. bytes[0]:=$1e;
  1662. end;
  1663. R_ES :
  1664. begin
  1665. if c=4 then
  1666. bytes[0]:=$7
  1667. else
  1668. bytes[0]:=$6;
  1669. end;
  1670. R_SS :
  1671. begin
  1672. if c=4 then
  1673. bytes[0]:=$17
  1674. else
  1675. bytes[0]:=$16;
  1676. end;
  1677. else
  1678. InternalError(777004);
  1679. end;
  1680. sec.writebytes(bytes,1);
  1681. end;
  1682. 5,7 :
  1683. begin
  1684. case oper[0].reg.enum of
  1685. R_FS :
  1686. begin
  1687. if c=5 then
  1688. bytes[0]:=$a1
  1689. else
  1690. bytes[0]:=$a0;
  1691. end;
  1692. R_GS :
  1693. begin
  1694. if c=5 then
  1695. bytes[0]:=$a9
  1696. else
  1697. bytes[0]:=$a8;
  1698. end;
  1699. else
  1700. InternalError(777005);
  1701. end;
  1702. sec.writebytes(bytes,1);
  1703. end;
  1704. 8,9,10 :
  1705. begin
  1706. if oper[c-8].reg.enum=R_INTREGISTER then
  1707. bytes[0]:=ord(codes^)+regval_new(oper[c-8].reg.number)
  1708. else
  1709. bytes[0]:=ord(codes^)+regval(oper[c-8].reg.enum);
  1710. inc(codes);
  1711. sec.writebytes(bytes,1);
  1712. end;
  1713. 15 :
  1714. begin
  1715. bytes[0]:=0;
  1716. sec.writebytes(bytes,1);
  1717. end;
  1718. 12,13,14 :
  1719. begin
  1720. getvalsym(c-12);
  1721. if (currval<-128) or (currval>127) then
  1722. Message2(asmw_e_value_exceeds_bounds,'signed byte',tostr(currval));
  1723. if assigned(currsym) then
  1724. sec.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1725. else
  1726. sec.writebytes(currval,1);
  1727. end;
  1728. 16,17,18 :
  1729. begin
  1730. getvalsym(c-16);
  1731. if (currval<-256) or (currval>255) then
  1732. Message2(asmw_e_value_exceeds_bounds,'byte',tostr(currval));
  1733. if assigned(currsym) then
  1734. sec.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1735. else
  1736. sec.writebytes(currval,1);
  1737. end;
  1738. 20,21,22 :
  1739. begin
  1740. getvalsym(c-20);
  1741. if (currval<0) or (currval>255) then
  1742. Message2(asmw_e_value_exceeds_bounds,'unsigned byte',tostr(currval));
  1743. if assigned(currsym) then
  1744. sec.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1745. else
  1746. sec.writebytes(currval,1);
  1747. end;
  1748. 24,25,26 :
  1749. begin
  1750. getvalsym(c-24);
  1751. if (currval<-65536) or (currval>65535) then
  1752. Message2(asmw_e_value_exceeds_bounds,'word',tostr(currval));
  1753. if assigned(currsym) then
  1754. sec.writereloc(currval,2,currsym,RELOC_ABSOLUTE)
  1755. else
  1756. sec.writebytes(currval,2);
  1757. end;
  1758. 28,29,30 :
  1759. begin
  1760. getvalsym(c-28);
  1761. if assigned(currsym) then
  1762. sec.writereloc(currval,4,currsym,RELOC_ABSOLUTE)
  1763. else
  1764. sec.writebytes(currval,4);
  1765. end;
  1766. 32,33,34 :
  1767. begin
  1768. getvalsym(c-32);
  1769. if assigned(currsym) then
  1770. sec.writereloc(currval,4,currsym,RELOC_ABSOLUTE)
  1771. else
  1772. sec.writebytes(currval,4);
  1773. end;
  1774. 40,41,42 :
  1775. begin
  1776. getvalsym(c-40);
  1777. data:=currval-insend;
  1778. if assigned(currsym) then
  1779. inc(data,currsym.address);
  1780. if (data>127) or (data<-128) then
  1781. Message1(asmw_e_short_jmp_out_of_range,tostr(data));
  1782. sec.writebytes(data,1);
  1783. end;
  1784. 52,53,54 :
  1785. begin
  1786. getvalsym(c-52);
  1787. if assigned(currsym) then
  1788. sec.writereloc(currval,4,currsym,RELOC_RELATIVE)
  1789. else
  1790. sec.writereloc(currval-insend,4,nil,RELOC_ABSOLUTE)
  1791. end;
  1792. 56,57,58 :
  1793. begin
  1794. getvalsym(c-56);
  1795. if assigned(currsym) then
  1796. sec.writereloc(currval,4,currsym,RELOC_RELATIVE)
  1797. else
  1798. sec.writereloc(currval-insend,4,nil,RELOC_ABSOLUTE)
  1799. end;
  1800. 192,193,194 :
  1801. begin
  1802. if NeedAddrPrefix(c-192) then
  1803. begin
  1804. bytes[0]:=$67;
  1805. sec.writebytes(bytes,1);
  1806. end;
  1807. end;
  1808. 200 :
  1809. begin
  1810. bytes[0]:=$67;
  1811. sec.writebytes(bytes,1);
  1812. end;
  1813. 208 :
  1814. begin
  1815. bytes[0]:=$66;
  1816. sec.writebytes(bytes,1);
  1817. end;
  1818. 216 :
  1819. begin
  1820. bytes[0]:=ord(codes^)+condval[condition];
  1821. inc(codes);
  1822. sec.writebytes(bytes,1);
  1823. end;
  1824. 201,
  1825. 202,
  1826. 209,
  1827. 210,
  1828. 217,218,219 :
  1829. begin
  1830. { these are dissambler hints or 32 bit prefixes which
  1831. are not needed }
  1832. end;
  1833. 31,
  1834. 48,49,50,
  1835. 224,225,226 :
  1836. begin
  1837. InternalError(777006);
  1838. end
  1839. else
  1840. begin
  1841. if (c>=64) and (c<=191) then
  1842. begin
  1843. if (c<127) then
  1844. begin
  1845. if (oper[c and 7].typ=top_reg) then
  1846. if oper[c and 7].reg.enum=R_INTREGISTER then
  1847. rfield:=regval_new(oper[c and 7].reg.number)
  1848. else
  1849. rfield:=regval(oper[c and 7].reg.enum)
  1850. else
  1851. if oper[c and 7].ref^.base.enum=R_INTREGISTER then
  1852. rfield:=regval_new(oper[c and 7].ref^.base.number)
  1853. else
  1854. rfield:=regval(oper[c and 7].ref^.base.enum);
  1855. end
  1856. else
  1857. rfield:=c and 7;
  1858. opidx:=(c shr 3) and 7;
  1859. if not process_ea(oper[opidx], ea_data, rfield) then
  1860. Message(asmw_e_invalid_effective_address);
  1861. pb:=@bytes;
  1862. pb^:=chr(ea_data.modrm);
  1863. inc(pb);
  1864. if ea_data.sib_present then
  1865. begin
  1866. pb^:=chr(ea_data.sib);
  1867. inc(pb);
  1868. end;
  1869. s:=pb-pchar(@bytes);
  1870. sec.writebytes(bytes,s);
  1871. case ea_data.bytes of
  1872. 0 : ;
  1873. 1 :
  1874. begin
  1875. if (oper[opidx].ot and OT_MEMORY)=OT_MEMORY then
  1876. sec.writereloc(oper[opidx].ref^.offset+oper[opidx].ref^.offsetfixup,1,oper[opidx].ref^.symbol,RELOC_ABSOLUTE)
  1877. else
  1878. begin
  1879. bytes[0]:=oper[opidx].ref^.offset+oper[opidx].ref^.offsetfixup;
  1880. sec.writebytes(bytes,1);
  1881. end;
  1882. inc(s);
  1883. end;
  1884. 2,4 :
  1885. begin
  1886. sec.writereloc(oper[opidx].ref^.offset+oper[opidx].ref^.offsetfixup,ea_data.bytes,
  1887. oper[opidx].ref^.symbol,RELOC_ABSOLUTE);
  1888. inc(s,ea_data.bytes);
  1889. end;
  1890. end;
  1891. end
  1892. else
  1893. InternalError(777007);
  1894. end;
  1895. end;
  1896. until false;
  1897. end;
  1898. {$endif NOAG386BIN}
  1899. function Taicpu.is_nop:boolean;
  1900. begin
  1901. {We do not check the number of operands; we assume that nobody constructs
  1902. a mov or xchg instruction with less than 2 operands. (DM)}
  1903. is_nop:=(opcode=A_NOP) or
  1904. (opcode=A_MOV) and (oper[0].typ=top_reg) and (oper[1].typ=top_reg) and (oper[0].reg.number=oper[1].reg.number) or
  1905. (opcode=A_XCHG) and (oper[0].typ=top_reg) and (oper[1].typ=top_reg) and (oper[0].reg.number=oper[1].reg.number);
  1906. end;
  1907. function Taicpu.is_move:boolean;
  1908. begin
  1909. {We do not check the number of operands; we assume that nobody constructs
  1910. a mov, movzx or movsx instruction with less than 2 operands. Note that
  1911. a move between a reference and a register is not a move that is of
  1912. interrest to the register allocation, therefore we only return true
  1913. for a move between two registers. (DM)}
  1914. is_move:=((opcode=A_MOV) or (opcode=A_MOVZX) or (opcode=A_MOVSX)) and
  1915. ((oper[0].typ=top_reg) and (oper[1].typ=top_reg));
  1916. end;
  1917. {$ifdef NEWRA}
  1918. function Taicpu.spill_registers(list:Taasmoutput;
  1919. rgget:Trggetproc;
  1920. rgunget:Trgungetproc;
  1921. r:Tsupregset;
  1922. var unusedregsint:Tsupregset;
  1923. const spilltemplist:Tspill_temp_list):boolean;
  1924. {Spill the registers in r in this instruction. Returns true if any help
  1925. registers are used. This procedure has become one big hack party, because
  1926. of the huge amount of situations you can have. The irregularity of the i386
  1927. instruction set doesn't help either. (DM)}
  1928. var i:byte;
  1929. supreg:Tsuperregister;
  1930. subreg:Tsubregister;
  1931. helpreg:Tregister;
  1932. helpins:Taicpu;
  1933. op:Tasmop;
  1934. hopsize:Topsize;
  1935. pos:Tai;
  1936. begin
  1937. {Situation examples are in intel notation, so operand order:
  1938. mov eax , ebx
  1939. ^^^ ^^^
  1940. oper[1] oper[0]
  1941. (DM)}
  1942. spill_registers:=false;
  1943. case ops of
  1944. 1:
  1945. begin
  1946. if (oper[0].typ=top_reg) and (oper[0].reg.enum=R_INTREGISTER) then
  1947. begin
  1948. supreg:=oper[0].reg.number shr 8;
  1949. if supreg in r then
  1950. begin
  1951. {Situation example:
  1952. push r20d ; r20d must be spilled into [ebp-12]
  1953. Change into:
  1954. push [ebp-12] ; Replace register by reference }
  1955. { hopsize:=reg2opsize(oper[0].reg);}
  1956. oper[0].typ:=top_ref;
  1957. new(oper[0].ref);
  1958. oper[0].ref^:=spilltemplist[supreg];
  1959. { oper[0].ref^.size:=hopsize;}
  1960. end;
  1961. end;
  1962. if oper[0].typ=top_ref then
  1963. begin
  1964. supreg:=oper[0].ref^.base.number shr 8;
  1965. if supreg in r then
  1966. begin
  1967. {Situation example:
  1968. push [r21d+4*r22d] ; r21d must be spilled into [ebp-12]
  1969. Change into:
  1970. mov r23d,[ebp-12] ; Use a help register
  1971. push [r23d+4*r22d] ; Replace register by helpregister }
  1972. subreg:=oper[0].ref^.base.number and $ff;
  1973. if oper[0].ref^.index.number=NR_NO then
  1974. pos:=Tai(previous)
  1975. else
  1976. pos:=get_insert_pos(Tai(previous),oper[0].ref^.index.number shr 8,0,0,unusedregsint);
  1977. rgget(list,pos,subreg,helpreg);
  1978. spill_registers:=true;
  1979. helpins:=Taicpu.op_ref_reg(A_MOV,reg2opsize(oper[0].ref^.base),spilltemplist[supreg],helpreg);
  1980. if pos=nil then
  1981. list.insertafter(helpins,list.first)
  1982. else
  1983. list.insertafter(helpins,pos.next);
  1984. rgunget(list,helpins,helpreg);
  1985. forward_allocation(Tai(helpins.next),unusedregsint);
  1986. oper[0].ref^.base:=helpreg;
  1987. end;
  1988. supreg:=oper[0].ref^.index.number shr 8;
  1989. if supreg in r then
  1990. begin
  1991. {Situation example:
  1992. push [r21d+4*r22d] ; r22d must be spilled into [ebp-12]
  1993. Change into:
  1994. mov r23d,[ebp-12] ; Use a help register
  1995. push [r21d+4*r23d] ; Replace register by helpregister }
  1996. subreg:=oper[0].ref^.index.number and $ff;
  1997. if oper[0].ref^.base.number=NR_NO then
  1998. pos:=Tai(previous)
  1999. else
  2000. pos:=get_insert_pos(Tai(previous),oper[0].ref^.base.number shr 8,0,0,unusedregsint);
  2001. rgget(list,pos,subreg,helpreg);
  2002. spill_registers:=true;
  2003. helpins:=Taicpu.op_ref_reg(A_MOV,reg2opsize(oper[0].ref^.index),spilltemplist[supreg],helpreg);
  2004. if pos=nil then
  2005. list.insertafter(helpins,list.first)
  2006. else
  2007. list.insertafter(helpins,pos.next);
  2008. rgunget(list,helpins,helpreg);
  2009. forward_allocation(Tai(helpins.next),unusedregsint);
  2010. oper[0].ref^.index:=helpreg;
  2011. end;
  2012. end;
  2013. end;
  2014. 2:
  2015. begin
  2016. if (oper[0].typ=top_reg) and (oper[0].reg.enum=R_INTREGISTER) then
  2017. begin
  2018. supreg:=oper[0].reg.number shr 8;
  2019. subreg:=oper[0].reg.number and $ff;
  2020. if supreg in r then
  2021. if oper[1].typ=top_ref then
  2022. begin
  2023. {Situation example:
  2024. add [r20d],r21d ; r21d must be spilled into [ebp-12]
  2025. Change into:
  2026. mov r22d,[ebp-12] ; Use a help register
  2027. add [r20d],r22d ; Replace register by helpregister }
  2028. pos:=get_insert_pos(Tai(previous),oper[0].reg.number shr 8,
  2029. oper[1].ref^.base.number shr 8,oper[1].ref^.index.number shr 8,
  2030. unusedregsint);
  2031. rgget(list,pos,subreg,helpreg);
  2032. spill_registers:=true;
  2033. helpins:=Taicpu.op_ref_reg(A_MOV,reg2opsize(oper[0].reg),spilltemplist[supreg],helpreg);
  2034. if pos=nil then
  2035. list.insertafter(helpins,list.first)
  2036. else
  2037. list.insertafter(helpins,pos.next);
  2038. oper[0].reg:=helpreg;
  2039. rgunget(list,helpins,helpreg);
  2040. forward_allocation(Tai(helpins.next),unusedregsint);
  2041. end
  2042. else
  2043. begin
  2044. {Situation example:
  2045. add r20d,r21d ; r21d must be spilled into [ebp-12]
  2046. Change into:
  2047. add r20d,[ebp-12] ; Replace register by reference }
  2048. oper[0].typ:=top_ref;
  2049. new(oper[0].ref);
  2050. oper[0].ref^:=spilltemplist[supreg];
  2051. end;
  2052. end;
  2053. if (oper[1].typ=top_reg) and (oper[1].reg.enum=R_INTREGISTER) then
  2054. begin
  2055. supreg:=oper[1].reg.number shr 8;
  2056. subreg:=oper[1].reg.number and $ff;
  2057. if supreg in r then
  2058. begin
  2059. if oper[0].typ=top_ref then
  2060. begin
  2061. {Situation example:
  2062. add r20d,[r21d] ; r20d must be spilled into [ebp-12]
  2063. Change into:
  2064. mov r22d,[r21d] ; Use a help register
  2065. add [ebp-12],r22d ; Replace register by helpregister }
  2066. pos:=get_insert_pos(Tai(previous),oper[0].ref^.base.number shr 8,
  2067. oper[0].ref^.index.number shr 8,0,unusedregsint);
  2068. rgget(list,pos,subreg,helpreg);
  2069. spill_registers:=true;
  2070. op:=A_MOV;
  2071. hopsize:=opsize; {Save old value...}
  2072. if (opcode=A_MOVZX) or (opcode=A_MOVSX) or (opcode=A_LEA) then
  2073. begin
  2074. {Because 'movzx memory,register' does not exist...}
  2075. op:=opcode;
  2076. opcode:=A_MOV;
  2077. opsize:=reg2opsize(oper[1].reg);
  2078. end;
  2079. helpins:=Taicpu.op_ref_reg(op,hopsize,oper[0].ref^,helpreg);
  2080. if pos=nil then
  2081. list.insertafter(helpins,list.first)
  2082. else
  2083. list.insertafter(helpins,pos.next);
  2084. dispose(oper[0].ref);
  2085. oper[0].typ:=top_reg;
  2086. oper[0].reg:=helpreg;
  2087. oper[1].typ:=top_ref;
  2088. new(oper[1].ref);
  2089. oper[1].ref^:=spilltemplist[supreg];
  2090. rgunget(list,helpins,helpreg);
  2091. forward_allocation(Tai(helpins.next),unusedregsint);
  2092. end
  2093. else
  2094. begin
  2095. {Situation example:
  2096. add r20d,r21d ; r20d must be spilled into [ebp-12]
  2097. Change into:
  2098. add [ebp-12],r21d ; Replace register by reference }
  2099. if (opcode=A_MOVZX) or (opcode=A_MOVSX) then
  2100. begin
  2101. {Because 'movzx memory,register' does not exist...}
  2102. spill_registers:=true;
  2103. op:=opcode;
  2104. opcode:=A_MOV;
  2105. opsize:=reg2opsize(oper[1].reg);
  2106. pos:=get_insert_pos(Tai(previous),oper[0].reg.number,0,0,unusedregsint);
  2107. rgget(list,pos,subreg,helpreg);
  2108. helpins:=Taicpu.op_reg_reg(op,hopsize,oper[0].reg,helpreg);
  2109. if pos=nil then
  2110. list.insertafter(helpins,list.first)
  2111. else
  2112. list.insertafter(helpins,pos.next);
  2113. rgunget(list,helpins,helpreg);
  2114. forward_allocation(Tai(helpins.next),unusedregsint);
  2115. end;
  2116. oper[1].typ:=top_ref;
  2117. new(oper[1].ref);
  2118. oper[1].ref^:=spilltemplist[supreg];
  2119. end;
  2120. {The i386 instruction set never gets boring... IMUL does
  2121. not support a memory location as destination. Check if
  2122. the opcode is IMUL and fix it. (DM)}
  2123. if opcode=A_IMUL then
  2124. begin
  2125. {Yikes! We just changed the destination register into
  2126. a memory location above here.
  2127. Situation example:
  2128. imul [ebp-12],r21d ; We need a help register
  2129. Change into:
  2130. mov r22d,[ebp-12] ; Use a help instruction (only for IMUL)
  2131. imul r22d,r21d ; Replace reference by helpregister
  2132. mov [ebp-12],r22d ; Use another help instruction}
  2133. rgget(list,Tai(previous),subreg,helpreg);
  2134. {First help instruction.}
  2135. helpins:=Taicpu.op_ref_reg(A_MOV,opsize,oper[1].ref^,helpreg);
  2136. if previous=nil then
  2137. list.insert(helpins)
  2138. else
  2139. list.insertafter(helpins,previous);
  2140. {Second help instruction.}
  2141. helpins:=Taicpu.op_reg_ref(A_MOV,opsize,helpreg,oper[1].ref^);
  2142. dispose(oper[1].ref);
  2143. oper[1].typ:=top_reg;
  2144. oper[1].reg:=helpreg;
  2145. list.insertafter(helpins,self);
  2146. end;
  2147. end;
  2148. end;
  2149. for i:=0 to 1 do
  2150. if oper[i].typ=top_ref then
  2151. begin
  2152. supreg:=oper[i].ref^.base.number shr 8;
  2153. if supreg in r then
  2154. begin
  2155. {Situation example:
  2156. add r20d,[r21d+4*r22d] ; r21d must be spilled into [ebp-12]
  2157. Change into:
  2158. mov r23d,[ebp-12] ; Use a help register
  2159. add r20d,[r23d+4*r22d] ; Replace register by helpregister }
  2160. subreg:=oper[i].ref^.base.number and $ff;
  2161. if i=1 then
  2162. pos:=get_insert_pos(Tai(previous),oper[i].ref^.index.number shr 8,oper[0].reg.number shr 8,
  2163. 0,unusedregsint)
  2164. else
  2165. pos:=get_insert_pos(Tai(previous),oper[i].ref^.index.number shr 8,0,0,unusedregsint);
  2166. rgget(list,pos,subreg,helpreg);
  2167. spill_registers:=true;
  2168. helpins:=Taicpu.op_ref_reg(A_MOV,reg2opsize(oper[i].ref^.base),spilltemplist[supreg],helpreg);
  2169. if pos=nil then
  2170. list.insertafter(helpins,list.first)
  2171. else
  2172. list.insertafter(helpins,pos.next);
  2173. oper[i].ref^.base:=helpreg;
  2174. rgunget(list,helpins,helpreg);
  2175. forward_allocation(Tai(helpins.next),unusedregsint);
  2176. end;
  2177. supreg:=oper[i].ref^.index.number shr 8;
  2178. if supreg in r then
  2179. begin
  2180. {Situation example:
  2181. add r20d,[r21d+4*r22d] ; r22d must be spilled into [ebp-12]
  2182. Change into:
  2183. mov r23d,[ebp-12] ; Use a help register
  2184. add r20d,[r21d+4*r23d] ; Replace register by helpregister }
  2185. subreg:=oper[i].ref^.index.number and $ff;
  2186. if i=1 then
  2187. pos:=get_insert_pos(Tai(previous),oper[i].ref^.base.number shr 8,oper[0].reg.number shr 8,
  2188. 0,unusedregsint)
  2189. else
  2190. pos:=get_insert_pos(Tai(previous),oper[i].ref^.base.number shr 8,0,0,unusedregsint);
  2191. rgget(list,pos,subreg,helpreg);
  2192. spill_registers:=true;
  2193. helpins:=Taicpu.op_ref_reg(A_MOV,reg2opsize(oper[i].ref^.index),spilltemplist[supreg],helpreg);
  2194. if pos=nil then
  2195. list.insertafter(helpins,list.first)
  2196. else
  2197. list.insertafter(helpins,pos.next);
  2198. oper[i].ref^.index:=helpreg;
  2199. rgunget(list,helpins,helpreg);
  2200. forward_allocation(Tai(helpins.next),unusedregsint);
  2201. end;
  2202. end;
  2203. end;
  2204. 3:
  2205. begin
  2206. {$warning todo!!}
  2207. end;
  2208. end;
  2209. end;
  2210. {$endif NEWRA}
  2211. {*****************************************************************************
  2212. Instruction table
  2213. *****************************************************************************}
  2214. procedure BuildInsTabCache;
  2215. {$ifndef NOAG386BIN}
  2216. var
  2217. i : longint;
  2218. {$endif}
  2219. begin
  2220. {$ifndef NOAG386BIN}
  2221. new(instabcache);
  2222. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  2223. i:=0;
  2224. while (i<InsTabEntries) do
  2225. begin
  2226. if InsTabCache^[InsTab[i].OPcode]=-1 then
  2227. InsTabCache^[InsTab[i].OPcode]:=i;
  2228. inc(i);
  2229. end;
  2230. {$endif NOAG386BIN}
  2231. end;
  2232. procedure InitAsm;
  2233. begin
  2234. {$ifndef NOAG386BIN}
  2235. if not assigned(instabcache) then
  2236. BuildInsTabCache;
  2237. {$endif NOAG386BIN}
  2238. end;
  2239. procedure DoneAsm;
  2240. begin
  2241. {$ifndef NOAG386BIN}
  2242. if assigned(instabcache) then
  2243. begin
  2244. dispose(instabcache);
  2245. instabcache:=nil;
  2246. end;
  2247. {$endif NOAG386BIN}
  2248. end;
  2249. end.
  2250. {
  2251. $Log$
  2252. Revision 1.13 2003-08-20 09:07:00 daniel
  2253. * New register coding now mandatory, some more convert_registers calls
  2254. removed.
  2255. Revision 1.12 2003/08/20 07:48:04 daniel
  2256. * Made internal assembler use new register coding
  2257. Revision 1.11 2003/08/19 13:58:33 daniel
  2258. * Corrected a comment.
  2259. Revision 1.10 2003/08/15 14:44:20 daniel
  2260. * Fixed newra compilation
  2261. Revision 1.9 2003/08/11 21:18:20 peter
  2262. * start of sparc support for newra
  2263. Revision 1.8 2003/08/09 18:56:54 daniel
  2264. * cs_regalloc renamed to cs_regvars to avoid confusion with register
  2265. allocator
  2266. * Some preventive changes to i386 spillinh code
  2267. Revision 1.7 2003/07/06 15:31:21 daniel
  2268. * Fixed register allocator. *Lots* of fixes.
  2269. Revision 1.6 2003/06/14 14:53:50 jonas
  2270. * fixed newra cycle for x86
  2271. * added constants for indicating source and destination operands of the
  2272. "move reg,reg" instruction to aasmcpu (and use those in rgobj)
  2273. Revision 1.5 2003/06/03 13:01:59 daniel
  2274. * Register allocator finished
  2275. Revision 1.4 2003/05/30 23:57:08 peter
  2276. * more sparc cleanup
  2277. * accumulator removed, splitted in function_return_reg (called) and
  2278. function_result_reg (caller)
  2279. Revision 1.3 2003/05/22 21:33:31 peter
  2280. * removed some unit dependencies
  2281. Revision 1.2 2002/04/25 16:12:09 florian
  2282. * fixed more problems with cpubase and x86-64
  2283. Revision 1.1 2003/04/25 12:43:40 florian
  2284. * merged i386/aasmcpu and x86_64/aasmcpu to x86/aasmcpu
  2285. Revision 1.18 2003/04/25 12:04:31 florian
  2286. * merged agx64att and ag386att to x86/agx86att
  2287. Revision 1.17 2003/04/22 14:33:38 peter
  2288. * removed some notes/hints
  2289. Revision 1.16 2003/04/22 10:09:35 daniel
  2290. + Implemented the actual register allocator
  2291. + Scratch registers unavailable when new register allocator used
  2292. + maybe_save/maybe_restore unavailable when new register allocator used
  2293. Revision 1.15 2003/03/26 12:50:54 armin
  2294. * avoid problems with the ide in init/dome
  2295. Revision 1.14 2003/03/08 08:59:07 daniel
  2296. + $define newra will enable new register allocator
  2297. + getregisterint will return imaginary registers with $newra
  2298. + -sr switch added, will skip register allocation so you can see
  2299. the direct output of the code generator before register allocation
  2300. Revision 1.13 2003/02/25 07:41:54 daniel
  2301. * Properly fixed reversed operands bug
  2302. Revision 1.12 2003/02/19 22:00:15 daniel
  2303. * Code generator converted to new register notation
  2304. - Horribily outdated todo.txt removed
  2305. Revision 1.11 2003/01/09 20:40:59 daniel
  2306. * Converted some code in cgx86.pas to new register numbering
  2307. Revision 1.10 2003/01/08 18:43:57 daniel
  2308. * Tregister changed into a record
  2309. Revision 1.9 2003/01/05 13:36:53 florian
  2310. * x86-64 compiles
  2311. + very basic support for float128 type (x86-64 only)
  2312. Revision 1.8 2002/11/17 16:31:58 carl
  2313. * memory optimization (3-4%) : cleanup of tai fields,
  2314. cleanup of tdef and tsym fields.
  2315. * make it work for m68k
  2316. Revision 1.7 2002/11/15 01:58:54 peter
  2317. * merged changes from 1.0.7 up to 04-11
  2318. - -V option for generating bug report tracing
  2319. - more tracing for option parsing
  2320. - errors for cdecl and high()
  2321. - win32 import stabs
  2322. - win32 records<=8 are returned in eax:edx (turned off by default)
  2323. - heaptrc update
  2324. - more info for temp management in .s file with EXTDEBUG
  2325. Revision 1.6 2002/10/31 13:28:32 pierre
  2326. * correct last wrong fix for tw2158
  2327. Revision 1.5 2002/10/30 17:10:00 pierre
  2328. * merge of fix for tw2158 bug
  2329. Revision 1.4 2002/08/15 19:10:36 peter
  2330. * first things tai,tnode storing in ppu
  2331. Revision 1.3 2002/08/13 18:01:52 carl
  2332. * rename swatoperands to swapoperands
  2333. + m68k first compilable version (still needs a lot of testing):
  2334. assembler generator, system information , inline
  2335. assembler reader.
  2336. Revision 1.2 2002/07/20 11:57:59 florian
  2337. * types.pas renamed to defbase.pas because D6 contains a types
  2338. unit so this would conflicts if D6 programms are compiled
  2339. + Willamette/SSE2 instructions to assembler added
  2340. Revision 1.1 2002/07/01 18:46:29 peter
  2341. * internal linker
  2342. * reorganized aasm layer
  2343. }