aasmcpu.pas 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533
  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=R_INTREGISTER then
  1148. begin
  1149. if segprefix.number<>NR_NO then
  1150. begin
  1151. case segprefix.number of
  1152. NR_CS: c:=$2e;
  1153. NR_DS: c:=$3e;
  1154. NR_ES: c:=$26;
  1155. NR_FS: c:=$64;
  1156. NR_GS: c:=$65;
  1157. NR_SS: c:=$36;
  1158. end;
  1159. sec.writebytes(c,1);
  1160. { fix the offset for GenNode }
  1161. inc(InsOffset);
  1162. end;
  1163. end
  1164. else
  1165. if (segprefix.number<>NR_NO) then
  1166. begin
  1167. case segprefix.number of
  1168. NR_CS : c:=$2e;
  1169. NR_DS : c:=$3e;
  1170. NR_ES : c:=$26;
  1171. NR_FS : c:=$64;
  1172. NR_GS : c:=$65;
  1173. NR_SS : c:=$36;
  1174. end;
  1175. sec.writebytes(c,1);
  1176. { fix the offset for GenNode }
  1177. inc(InsOffset);
  1178. end;
  1179. { Generate the instruction }
  1180. GenCode(sec);
  1181. end;
  1182. function taicpu.needaddrprefix(opidx:byte):boolean;
  1183. var i,b:Tnewregister;
  1184. ia,ba:boolean;
  1185. begin
  1186. needaddrprefix:=false;
  1187. if (OT_MEMORY and (not oper[opidx].ot))=0 then
  1188. begin
  1189. if oper[opidx].ref^.index.enum=R_INTREGISTER then
  1190. begin
  1191. i:=oper[opidx].ref^.index.number;
  1192. ia:=(i<>NR_NO) and (i and $ff<>R_SUBD);
  1193. end
  1194. else
  1195. internalerror(200308191);
  1196. if oper[opidx].ref^.base.enum=R_INTREGISTER then
  1197. begin
  1198. b:=oper[opidx].ref^.base.number;
  1199. ba:=(b<>NR_NO) and (b and $ff<>R_SUBD);
  1200. end
  1201. else
  1202. internalerror(200308191);
  1203. if ia or ba then
  1204. needaddrprefix:=true;
  1205. end;
  1206. end;
  1207. function regval(r:Toldregister):byte;
  1208. begin
  1209. case r of
  1210. R_EAX,R_AX,R_AL,R_ES,R_CR0,R_DR0,R_ST,R_ST0,R_MM0,R_XMM0 :
  1211. regval:=0;
  1212. R_ECX,R_CX,R_CL,R_CS,R_DR1,R_ST1,R_MM1,R_XMM1 :
  1213. regval:=1;
  1214. R_EDX,R_DX,R_DL,R_SS,R_CR2,R_DR2,R_ST2,R_MM2,R_XMM2 :
  1215. regval:=2;
  1216. R_EBX,R_BX,R_BL,R_DS,R_CR3,R_DR3,R_TR3,R_ST3,R_MM3,R_XMM3 :
  1217. regval:=3;
  1218. R_ESP,R_SP,R_AH,R_FS,R_CR4,R_TR4,R_ST4,R_MM4,R_XMM4 :
  1219. regval:=4;
  1220. R_EBP,R_BP,R_CH,R_GS,R_TR5,R_ST5,R_MM5,R_XMM5 :
  1221. regval:=5;
  1222. R_ESI,R_SI,R_DH,R_DR6,R_TR6,R_ST6,R_MM6,R_XMM6 :
  1223. regval:=6;
  1224. R_EDI,R_DI,R_BH,R_DR7,R_TR7,R_ST7,R_MM7,R_XMM7 :
  1225. regval:=7;
  1226. else
  1227. begin
  1228. internalerror(777001);
  1229. regval:=0;
  1230. end;
  1231. end;
  1232. end;
  1233. function regval_new(r:Tnewregister):byte;
  1234. const count=45;
  1235. bsstart=32;
  1236. registers:array[0..count-1] of Tnewregister=(
  1237. NR_CS, NR_DS, NR_ES, NR_SS,
  1238. NR_FS, NR_GS, NR_DR0, NR_DR1,
  1239. NR_DR2, NR_DR3, NR_DR6, NR_DR7,
  1240. NR_CR0, NR_CR2, NR_CR3, NR_CR4,
  1241. NR_TR3, NR_TR4, NR_TR5, NR_TR6,
  1242. NR_TR7, NR_AL, NR_AH, NR_AX,
  1243. NR_EAX, NR_BL, NR_BH, NR_BX,
  1244. NR_EBX, NR_CL, NR_CH, NR_CX,
  1245. NR_ECX, NR_DL, NR_DH, NR_DX,
  1246. NR_EDX, NR_SI, NR_ESI, NR_DI,
  1247. NR_EDI, NR_BP, NR_EBP, NR_SP,
  1248. NR_ESP);
  1249. register_values:array[0..count-1] of byte=(
  1250. 1, 3, 0, 2,
  1251. 4, 5, 0, 1,
  1252. 2, 3, 6, 7,
  1253. 0, 2, 3, 4,
  1254. 3, 4, 5, 6,
  1255. 7, 0, 4, 0,
  1256. 0, 3, 7, 3,
  1257. 3, 1, 5, 1,
  1258. 1, 2, 6, 2,
  1259. 2, 6, 6, 7,
  1260. 7, 5, 5, 4,
  1261. 4);
  1262. var i,p:byte;
  1263. begin
  1264. {Binary search.}
  1265. p:=0;
  1266. i:=bsstart;
  1267. while i<>0 do
  1268. begin
  1269. if (p+i<count) and (registers[p+i]<=r) then
  1270. p:=p+i;
  1271. i:=i shr 1;
  1272. end;
  1273. if registers[p]=r then
  1274. regval_new:=register_values[p]
  1275. else
  1276. internalerror(777001);
  1277. end;
  1278. function process_ea(const input:toper;var output:ea;rfield:longint):boolean;
  1279. var
  1280. j : longint;
  1281. sym : tasmsymbol;
  1282. md,s,rv : byte;
  1283. base,index,scalefactor,
  1284. o : longint;
  1285. ir,br : Tnewregister;
  1286. begin
  1287. process_ea:=false;
  1288. {Register ?}
  1289. if (input.typ=top_reg) then
  1290. begin
  1291. if input.reg.enum=R_INTREGISTER then
  1292. rv:=regval_new(input.reg.number)
  1293. else
  1294. rv:=regval(input.reg.enum);
  1295. output.sib_present:=false;
  1296. output.bytes:=0;
  1297. output.modrm:=$c0 or (rfield shl 3) or rv;
  1298. output.size:=1;
  1299. process_ea:=true;
  1300. exit;
  1301. end;
  1302. {No register, so memory reference.}
  1303. if (input.ref^.index.enum<>R_INTREGISTER) or (input.ref^.base.enum<>R_INTREGISTER) then
  1304. internalerror(200301081);
  1305. ir:=input.ref^.index.number;
  1306. br:=input.ref^.base.number;
  1307. s:=input.ref^.scalefactor;
  1308. o:=input.ref^.offset+input.ref^.offsetfixup;
  1309. sym:=input.ref^.symbol;
  1310. { it's direct address }
  1311. if (br=NR_NO) and (ir=NR_NO) then
  1312. begin
  1313. { it's a pure offset }
  1314. output.sib_present:=false;
  1315. output.bytes:=4;
  1316. output.modrm:=5 or (rfield shl 3);
  1317. end
  1318. else
  1319. { it's an indirection }
  1320. begin
  1321. { 16 bit address? }
  1322. if ((ir<>NR_NO) and (ir and $ff<>R_SUBD)) or ((br<>NR_NO) and (br and $ff<>R_SUBD)) then
  1323. message(asmw_e_16bit_not_supported);
  1324. {$ifdef OPTEA}
  1325. { make single reg base }
  1326. if (br=NR_NO) and (s=1) then
  1327. begin
  1328. br:=ir;
  1329. ir:=NR_NO;
  1330. end;
  1331. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  1332. if (br=NR_NO) and
  1333. (((s=2) and (ir<>NR_ESP)) or
  1334. (s=3) or (s=5) or (s=9)) then
  1335. begin
  1336. br:=ir;
  1337. dec(s);
  1338. end;
  1339. { swap ESP into base if scalefactor is 1 }
  1340. if (s=1) and (ir=NR_ESP) then
  1341. begin
  1342. ir:=br;
  1343. br:=NR_ESP;
  1344. end;
  1345. {$endif OPTEA}
  1346. { wrong, for various reasons }
  1347. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1348. exit;
  1349. { base }
  1350. case br of
  1351. NR_EAX : base:=0;
  1352. NR_ECX : base:=1;
  1353. NR_EDX : base:=2;
  1354. NR_EBX : base:=3;
  1355. NR_ESP : base:=4;
  1356. NR_NO,
  1357. NR_EBP : base:=5;
  1358. NR_ESI : base:=6;
  1359. NR_EDI : base:=7;
  1360. else
  1361. exit;
  1362. end;
  1363. { index }
  1364. case ir of
  1365. NR_EAX : index:=0;
  1366. NR_ECX : index:=1;
  1367. NR_EDX : index:=2;
  1368. NR_EBX : index:=3;
  1369. NR_NO : index:=4;
  1370. NR_EBP : index:=5;
  1371. NR_ESI : index:=6;
  1372. NR_EDI : index:=7;
  1373. else
  1374. exit;
  1375. end;
  1376. case s of
  1377. 0,
  1378. 1 : scalefactor:=0;
  1379. 2 : scalefactor:=1;
  1380. 4 : scalefactor:=2;
  1381. 8 : scalefactor:=3;
  1382. else
  1383. exit;
  1384. end;
  1385. if (br=NR_NO) or
  1386. ((br<>NR_EBP) and (o=0) and (sym=nil)) then
  1387. md:=0
  1388. else
  1389. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1390. md:=1
  1391. else
  1392. md:=2;
  1393. if (br=NR_NO) or (md=2) then
  1394. output.bytes:=4
  1395. else
  1396. output.bytes:=md;
  1397. { SIB needed ? }
  1398. if (ir=NR_NO) and (br<>NR_ESP) then
  1399. begin
  1400. output.sib_present:=false;
  1401. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1402. end
  1403. else
  1404. begin
  1405. output.sib_present:=true;
  1406. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1407. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1408. end;
  1409. end;
  1410. if output.sib_present then
  1411. output.size:=2+output.bytes
  1412. else
  1413. output.size:=1+output.bytes;
  1414. process_ea:=true;
  1415. end;
  1416. function taicpu.calcsize(p:PInsEntry):longint;
  1417. var
  1418. codes : pchar;
  1419. c : byte;
  1420. len : longint;
  1421. ea_data : ea;
  1422. begin
  1423. len:=0;
  1424. codes:=@p^.code;
  1425. repeat
  1426. c:=ord(codes^);
  1427. inc(codes);
  1428. case c of
  1429. 0 :
  1430. break;
  1431. 1,2,3 :
  1432. begin
  1433. inc(codes,c);
  1434. inc(len,c);
  1435. end;
  1436. 8,9,10 :
  1437. begin
  1438. inc(codes);
  1439. inc(len);
  1440. end;
  1441. 4,5,6,7 :
  1442. begin
  1443. if opsize=S_W then
  1444. inc(len,2)
  1445. else
  1446. inc(len);
  1447. end;
  1448. 15,
  1449. 12,13,14,
  1450. 16,17,18,
  1451. 20,21,22,
  1452. 40,41,42 :
  1453. inc(len);
  1454. 24,25,26,
  1455. 31,
  1456. 48,49,50 :
  1457. inc(len,2);
  1458. 28,29,30, { we don't have 16 bit immediates code }
  1459. 32,33,34,
  1460. 52,53,54,
  1461. 56,57,58 :
  1462. inc(len,4);
  1463. 192,193,194 :
  1464. if NeedAddrPrefix(c-192) then
  1465. inc(len);
  1466. 208 :
  1467. inc(len);
  1468. 200,
  1469. 201,
  1470. 202,
  1471. 209,
  1472. 210,
  1473. 217,218,219 : ;
  1474. 216 :
  1475. begin
  1476. inc(codes);
  1477. inc(len);
  1478. end;
  1479. 224,225,226 :
  1480. begin
  1481. InternalError(777002);
  1482. end;
  1483. else
  1484. begin
  1485. if (c>=64) and (c<=191) then
  1486. begin
  1487. if not process_ea(oper[(c shr 3) and 7], ea_data, 0) then
  1488. Message(asmw_e_invalid_effective_address)
  1489. else
  1490. inc(len,ea_data.size);
  1491. end
  1492. else
  1493. InternalError(777003);
  1494. end;
  1495. end;
  1496. until false;
  1497. calcsize:=len;
  1498. end;
  1499. procedure taicpu.GenCode(sec:TAsmObjectData);
  1500. {
  1501. * the actual codes (C syntax, i.e. octal):
  1502. * \0 - terminates the code. (Unless it's a literal of course.)
  1503. * \1, \2, \3 - that many literal bytes follow in the code stream
  1504. * \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
  1505. * (POP is never used for CS) depending on operand 0
  1506. * \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
  1507. * on operand 0
  1508. * \10, \11, \12 - a literal byte follows in the code stream, to be added
  1509. * to the register value of operand 0, 1 or 2
  1510. * \17 - encodes the literal byte 0. (Some compilers don't take
  1511. * kindly to a zero byte in the _middle_ of a compile time
  1512. * string constant, so I had to put this hack in.)
  1513. * \14, \15, \16 - a signed byte immediate operand, from operand 0, 1 or 2
  1514. * \20, \21, \22 - a byte immediate operand, from operand 0, 1 or 2
  1515. * \24, \25, \26 - an unsigned byte immediate operand, from operand 0, 1 or 2
  1516. * \30, \31, \32 - a word immediate operand, from operand 0, 1 or 2
  1517. * \34, \35, \36 - select between \3[012] and \4[012] depending on 16/32 bit
  1518. * assembly mode or the address-size override on the operand
  1519. * \37 - a word constant, from the _segment_ part of operand 0
  1520. * \40, \41, \42 - a long immediate operand, from operand 0, 1 or 2
  1521. * \50, \51, \52 - a byte relative operand, from operand 0, 1 or 2
  1522. * \60, \61, \62 - a word relative operand, from operand 0, 1 or 2
  1523. * \64, \65, \66 - select between \6[012] and \7[012] depending on 16/32 bit
  1524. * assembly mode or the address-size override on the operand
  1525. * \70, \71, \72 - a long relative operand, from operand 0, 1 or 2
  1526. * \1ab - a ModRM, calculated on EA in operand a, with the spare
  1527. * field the register value of operand b.
  1528. * \2ab - a ModRM, calculated on EA in operand a, with the spare
  1529. * field equal to digit b.
  1530. * \30x - might be an 0x67 byte, depending on the address size of
  1531. * the memory reference in operand x.
  1532. * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
  1533. * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
  1534. * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
  1535. * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
  1536. * \322 - indicates that this instruction is only valid when the
  1537. * operand size is the default (instruction to disassembler,
  1538. * generates no code in the assembler)
  1539. * \330 - a literal byte follows in the code stream, to be added
  1540. * to the condition code value of the instruction.
  1541. * \340 - reserve <operand 0> bytes of uninitialised storage.
  1542. * Operand 0 had better be a segmentless constant.
  1543. }
  1544. var
  1545. currval : longint;
  1546. currsym : tasmsymbol;
  1547. procedure getvalsym(opidx:longint);
  1548. begin
  1549. case oper[opidx].typ of
  1550. top_ref :
  1551. begin
  1552. currval:=oper[opidx].ref^.offset+oper[opidx].ref^.offsetfixup;
  1553. currsym:=oper[opidx].ref^.symbol;
  1554. end;
  1555. top_const :
  1556. begin
  1557. currval:=longint(oper[opidx].val);
  1558. currsym:=nil;
  1559. end;
  1560. top_symbol :
  1561. begin
  1562. currval:=oper[opidx].symofs;
  1563. currsym:=oper[opidx].sym;
  1564. end;
  1565. else
  1566. Message(asmw_e_immediate_or_reference_expected);
  1567. end;
  1568. end;
  1569. const
  1570. CondVal:array[TAsmCond] of byte=($0,
  1571. $7, $3, $2, $6, $2, $4, $F, $D, $C, $E, $6, $2,
  1572. $3, $7, $3, $5, $E, $C, $D, $F, $1, $B, $9, $5,
  1573. $0, $A, $A, $B, $8, $4);
  1574. var
  1575. c : byte;
  1576. pb,
  1577. codes : pchar;
  1578. bytes : array[0..3] of byte;
  1579. rfield,
  1580. data,s,opidx : longint;
  1581. ea_data : ea;
  1582. begin
  1583. {$ifdef EXTDEBUG}
  1584. { safety check }
  1585. if sec.sects[sec.currsec].datasize<>insoffset then
  1586. internalerror(200130121);
  1587. {$endif EXTDEBUG}
  1588. { load data to write }
  1589. codes:=insentry^.code;
  1590. { Force word push/pop for registers }
  1591. if (opsize=S_W) and ((codes[0]=#4) or (codes[0]=#6) or
  1592. ((codes[0]=#1) and ((codes[2]=#5) or (codes[2]=#7)))) then
  1593. begin
  1594. bytes[0]:=$66;
  1595. sec.writebytes(bytes,1);
  1596. end;
  1597. repeat
  1598. c:=ord(codes^);
  1599. inc(codes);
  1600. case c of
  1601. 0 :
  1602. break;
  1603. 1,2,3 :
  1604. begin
  1605. sec.writebytes(codes^,c);
  1606. inc(codes,c);
  1607. end;
  1608. 4,6 :
  1609. begin
  1610. case oper[0].reg.number of
  1611. NR_CS:
  1612. bytes[0]:=$e;
  1613. NR_NO,
  1614. NR_DS:
  1615. bytes[0]:=$1e;
  1616. NR_ES:
  1617. bytes[0]:=$6;
  1618. NR_SS:
  1619. bytes[0]:=$16;
  1620. else
  1621. internalerror(777004);
  1622. end;
  1623. if c=4 then
  1624. inc(bytes[0]);
  1625. sec.writebytes(bytes,1);
  1626. end;
  1627. 5,7 :
  1628. begin
  1629. case oper[0].reg.number of
  1630. NR_FS:
  1631. bytes[0]:=$a0;
  1632. NR_GS:
  1633. bytes[0]:=$a8;
  1634. else
  1635. internalerror(777005);
  1636. end;
  1637. if c=5 then
  1638. inc(bytes[0]);
  1639. sec.writebytes(bytes,1);
  1640. end;
  1641. 8,9,10 :
  1642. begin
  1643. if oper[c-8].reg.enum=R_INTREGISTER then
  1644. bytes[0]:=ord(codes^)+regval_new(oper[c-8].reg.number)
  1645. else
  1646. bytes[0]:=ord(codes^)+regval(oper[c-8].reg.enum);
  1647. inc(codes);
  1648. sec.writebytes(bytes,1);
  1649. end;
  1650. 15 :
  1651. begin
  1652. bytes[0]:=0;
  1653. sec.writebytes(bytes,1);
  1654. end;
  1655. 12,13,14 :
  1656. begin
  1657. getvalsym(c-12);
  1658. if (currval<-128) or (currval>127) then
  1659. Message2(asmw_e_value_exceeds_bounds,'signed byte',tostr(currval));
  1660. if assigned(currsym) then
  1661. sec.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1662. else
  1663. sec.writebytes(currval,1);
  1664. end;
  1665. 16,17,18 :
  1666. begin
  1667. getvalsym(c-16);
  1668. if (currval<-256) or (currval>255) then
  1669. Message2(asmw_e_value_exceeds_bounds,'byte',tostr(currval));
  1670. if assigned(currsym) then
  1671. sec.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1672. else
  1673. sec.writebytes(currval,1);
  1674. end;
  1675. 20,21,22 :
  1676. begin
  1677. getvalsym(c-20);
  1678. if (currval<0) or (currval>255) then
  1679. Message2(asmw_e_value_exceeds_bounds,'unsigned byte',tostr(currval));
  1680. if assigned(currsym) then
  1681. sec.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1682. else
  1683. sec.writebytes(currval,1);
  1684. end;
  1685. 24,25,26 :
  1686. begin
  1687. getvalsym(c-24);
  1688. if (currval<-65536) or (currval>65535) then
  1689. Message2(asmw_e_value_exceeds_bounds,'word',tostr(currval));
  1690. if assigned(currsym) then
  1691. sec.writereloc(currval,2,currsym,RELOC_ABSOLUTE)
  1692. else
  1693. sec.writebytes(currval,2);
  1694. end;
  1695. 28,29,30 :
  1696. begin
  1697. getvalsym(c-28);
  1698. if assigned(currsym) then
  1699. sec.writereloc(currval,4,currsym,RELOC_ABSOLUTE)
  1700. else
  1701. sec.writebytes(currval,4);
  1702. end;
  1703. 32,33,34 :
  1704. begin
  1705. getvalsym(c-32);
  1706. if assigned(currsym) then
  1707. sec.writereloc(currval,4,currsym,RELOC_ABSOLUTE)
  1708. else
  1709. sec.writebytes(currval,4);
  1710. end;
  1711. 40,41,42 :
  1712. begin
  1713. getvalsym(c-40);
  1714. data:=currval-insend;
  1715. if assigned(currsym) then
  1716. inc(data,currsym.address);
  1717. if (data>127) or (data<-128) then
  1718. Message1(asmw_e_short_jmp_out_of_range,tostr(data));
  1719. sec.writebytes(data,1);
  1720. end;
  1721. 52,53,54 :
  1722. begin
  1723. getvalsym(c-52);
  1724. if assigned(currsym) then
  1725. sec.writereloc(currval,4,currsym,RELOC_RELATIVE)
  1726. else
  1727. sec.writereloc(currval-insend,4,nil,RELOC_ABSOLUTE)
  1728. end;
  1729. 56,57,58 :
  1730. begin
  1731. getvalsym(c-56);
  1732. if assigned(currsym) then
  1733. sec.writereloc(currval,4,currsym,RELOC_RELATIVE)
  1734. else
  1735. sec.writereloc(currval-insend,4,nil,RELOC_ABSOLUTE)
  1736. end;
  1737. 192,193,194 :
  1738. begin
  1739. if NeedAddrPrefix(c-192) then
  1740. begin
  1741. bytes[0]:=$67;
  1742. sec.writebytes(bytes,1);
  1743. end;
  1744. end;
  1745. 200 :
  1746. begin
  1747. bytes[0]:=$67;
  1748. sec.writebytes(bytes,1);
  1749. end;
  1750. 208 :
  1751. begin
  1752. bytes[0]:=$66;
  1753. sec.writebytes(bytes,1);
  1754. end;
  1755. 216 :
  1756. begin
  1757. bytes[0]:=ord(codes^)+condval[condition];
  1758. inc(codes);
  1759. sec.writebytes(bytes,1);
  1760. end;
  1761. 201,
  1762. 202,
  1763. 209,
  1764. 210,
  1765. 217,218,219 :
  1766. begin
  1767. { these are dissambler hints or 32 bit prefixes which
  1768. are not needed }
  1769. end;
  1770. 31,
  1771. 48,49,50,
  1772. 224,225,226 :
  1773. begin
  1774. InternalError(777006);
  1775. end
  1776. else
  1777. begin
  1778. if (c>=64) and (c<=191) then
  1779. begin
  1780. if (c<127) then
  1781. begin
  1782. if (oper[c and 7].typ=top_reg) then
  1783. if oper[c and 7].reg.enum=R_INTREGISTER then
  1784. rfield:=regval_new(oper[c and 7].reg.number)
  1785. else
  1786. rfield:=regval(oper[c and 7].reg.enum)
  1787. else
  1788. if oper[c and 7].ref^.base.enum=R_INTREGISTER then
  1789. rfield:=regval_new(oper[c and 7].ref^.base.number)
  1790. else
  1791. rfield:=regval(oper[c and 7].ref^.base.enum);
  1792. end
  1793. else
  1794. rfield:=c and 7;
  1795. opidx:=(c shr 3) and 7;
  1796. if not process_ea(oper[opidx], ea_data, rfield) then
  1797. Message(asmw_e_invalid_effective_address);
  1798. pb:=@bytes;
  1799. pb^:=chr(ea_data.modrm);
  1800. inc(pb);
  1801. if ea_data.sib_present then
  1802. begin
  1803. pb^:=chr(ea_data.sib);
  1804. inc(pb);
  1805. end;
  1806. s:=pb-pchar(@bytes);
  1807. sec.writebytes(bytes,s);
  1808. case ea_data.bytes of
  1809. 0 : ;
  1810. 1 :
  1811. begin
  1812. if (oper[opidx].ot and OT_MEMORY)=OT_MEMORY then
  1813. sec.writereloc(oper[opidx].ref^.offset+oper[opidx].ref^.offsetfixup,1,oper[opidx].ref^.symbol,RELOC_ABSOLUTE)
  1814. else
  1815. begin
  1816. bytes[0]:=oper[opidx].ref^.offset+oper[opidx].ref^.offsetfixup;
  1817. sec.writebytes(bytes,1);
  1818. end;
  1819. inc(s);
  1820. end;
  1821. 2,4 :
  1822. begin
  1823. sec.writereloc(oper[opidx].ref^.offset+oper[opidx].ref^.offsetfixup,ea_data.bytes,
  1824. oper[opidx].ref^.symbol,RELOC_ABSOLUTE);
  1825. inc(s,ea_data.bytes);
  1826. end;
  1827. end;
  1828. end
  1829. else
  1830. InternalError(777007);
  1831. end;
  1832. end;
  1833. until false;
  1834. end;
  1835. {$endif NOAG386BIN}
  1836. function Taicpu.is_nop:boolean;
  1837. begin
  1838. {We do not check the number of operands; we assume that nobody constructs
  1839. a mov or xchg instruction with less than 2 operands. (DM)}
  1840. is_nop:=(opcode=A_NOP) or
  1841. (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
  1842. (opcode=A_XCHG) and (oper[0].typ=top_reg) and (oper[1].typ=top_reg) and (oper[0].reg.number=oper[1].reg.number);
  1843. end;
  1844. function Taicpu.is_move:boolean;
  1845. begin
  1846. {We do not check the number of operands; we assume that nobody constructs
  1847. a mov, movzx or movsx instruction with less than 2 operands. Note that
  1848. a move between a reference and a register is not a move that is of
  1849. interrest to the register allocation, therefore we only return true
  1850. for a move between two registers. (DM)}
  1851. is_move:=((opcode=A_MOV) or (opcode=A_MOVZX) or (opcode=A_MOVSX)) and
  1852. ((oper[0].typ=top_reg) and (oper[1].typ=top_reg));
  1853. end;
  1854. {$ifdef NEWRA}
  1855. function Taicpu.spill_registers(list:Taasmoutput;
  1856. rgget:Trggetproc;
  1857. rgunget:Trgungetproc;
  1858. r:Tsupregset;
  1859. var unusedregsint:Tsupregset;
  1860. const spilltemplist:Tspill_temp_list):boolean;
  1861. {Spill the registers in r in this instruction. Returns true if any help
  1862. registers are used. This procedure has become one big hack party, because
  1863. of the huge amount of situations you can have. The irregularity of the i386
  1864. instruction set doesn't help either. (DM)}
  1865. var i:byte;
  1866. supreg:Tsuperregister;
  1867. subreg:Tsubregister;
  1868. helpreg:Tregister;
  1869. helpins:Taicpu;
  1870. op:Tasmop;
  1871. hopsize:Topsize;
  1872. pos:Tai;
  1873. begin
  1874. {Situation examples are in intel notation, so operand order:
  1875. mov eax , ebx
  1876. ^^^ ^^^
  1877. oper[1] oper[0]
  1878. (DM)}
  1879. spill_registers:=false;
  1880. case ops of
  1881. 1:
  1882. begin
  1883. if (oper[0].typ=top_reg) and (oper[0].reg.enum=R_INTREGISTER) then
  1884. begin
  1885. supreg:=oper[0].reg.number shr 8;
  1886. if supreg in r then
  1887. begin
  1888. {Situation example:
  1889. push r20d ; r20d must be spilled into [ebp-12]
  1890. Change into:
  1891. push [ebp-12] ; Replace register by reference }
  1892. { hopsize:=reg2opsize(oper[0].reg);}
  1893. oper[0].typ:=top_ref;
  1894. new(oper[0].ref);
  1895. oper[0].ref^:=spilltemplist[supreg];
  1896. { oper[0].ref^.size:=hopsize;}
  1897. end;
  1898. end;
  1899. if oper[0].typ=top_ref then
  1900. begin
  1901. supreg:=oper[0].ref^.base.number shr 8;
  1902. if supreg in r then
  1903. begin
  1904. {Situation example:
  1905. push [r21d+4*r22d] ; r21d must be spilled into [ebp-12]
  1906. Change into:
  1907. mov r23d,[ebp-12] ; Use a help register
  1908. push [r23d+4*r22d] ; Replace register by helpregister }
  1909. subreg:=oper[0].ref^.base.number and $ff;
  1910. if oper[0].ref^.index.number=NR_NO then
  1911. pos:=Tai(previous)
  1912. else
  1913. pos:=get_insert_pos(Tai(previous),oper[0].ref^.index.number shr 8,0,0,unusedregsint);
  1914. rgget(list,pos,subreg,helpreg);
  1915. spill_registers:=true;
  1916. helpins:=Taicpu.op_ref_reg(A_MOV,reg2opsize(oper[0].ref^.base),spilltemplist[supreg],helpreg);
  1917. if pos=nil then
  1918. list.insertafter(helpins,list.first)
  1919. else
  1920. list.insertafter(helpins,pos.next);
  1921. rgunget(list,helpins,helpreg);
  1922. forward_allocation(Tai(helpins.next),unusedregsint);
  1923. oper[0].ref^.base:=helpreg;
  1924. end;
  1925. supreg:=oper[0].ref^.index.number shr 8;
  1926. if supreg in r then
  1927. begin
  1928. {Situation example:
  1929. push [r21d+4*r22d] ; r22d must be spilled into [ebp-12]
  1930. Change into:
  1931. mov r23d,[ebp-12] ; Use a help register
  1932. push [r21d+4*r23d] ; Replace register by helpregister }
  1933. subreg:=oper[0].ref^.index.number and $ff;
  1934. if oper[0].ref^.base.number=NR_NO then
  1935. pos:=Tai(previous)
  1936. else
  1937. pos:=get_insert_pos(Tai(previous),oper[0].ref^.base.number shr 8,0,0,unusedregsint);
  1938. rgget(list,pos,subreg,helpreg);
  1939. spill_registers:=true;
  1940. helpins:=Taicpu.op_ref_reg(A_MOV,reg2opsize(oper[0].ref^.index),spilltemplist[supreg],helpreg);
  1941. if pos=nil then
  1942. list.insertafter(helpins,list.first)
  1943. else
  1944. list.insertafter(helpins,pos.next);
  1945. rgunget(list,helpins,helpreg);
  1946. forward_allocation(Tai(helpins.next),unusedregsint);
  1947. oper[0].ref^.index:=helpreg;
  1948. end;
  1949. end;
  1950. end;
  1951. 2:
  1952. begin
  1953. { First spill the registers from the references. This is
  1954. required because the reference can be moved from this instruction
  1955. to a MOV instruction when spilling of the register operand is done }
  1956. for i:=0 to 1 do
  1957. if oper[i].typ=top_ref then
  1958. begin
  1959. supreg:=oper[i].ref^.base.number shr 8;
  1960. if supreg in r then
  1961. begin
  1962. {Situation example:
  1963. add r20d,[r21d+4*r22d] ; r21d must be spilled into [ebp-12]
  1964. Change into:
  1965. mov r23d,[ebp-12] ; Use a help register
  1966. add r20d,[r23d+4*r22d] ; Replace register by helpregister }
  1967. subreg:=oper[i].ref^.base.number and $ff;
  1968. if i=1 then
  1969. pos:=get_insert_pos(Tai(previous),oper[i].ref^.index.number shr 8,oper[0].reg.number shr 8,
  1970. 0,unusedregsint)
  1971. else
  1972. pos:=get_insert_pos(Tai(previous),oper[i].ref^.index.number shr 8,0,0,unusedregsint);
  1973. rgget(list,pos,subreg,helpreg);
  1974. spill_registers:=true;
  1975. helpins:=Taicpu.op_ref_reg(A_MOV,reg2opsize(oper[i].ref^.base),spilltemplist[supreg],helpreg);
  1976. if pos=nil then
  1977. list.insertafter(helpins,list.first)
  1978. else
  1979. list.insertafter(helpins,pos.next);
  1980. oper[i].ref^.base:=helpreg;
  1981. rgunget(list,helpins,helpreg);
  1982. forward_allocation(Tai(helpins.next),unusedregsint);
  1983. end;
  1984. supreg:=oper[i].ref^.index.number shr 8;
  1985. if supreg in r then
  1986. begin
  1987. {Situation example:
  1988. add r20d,[r21d+4*r22d] ; r22d must be spilled into [ebp-12]
  1989. Change into:
  1990. mov r23d,[ebp-12] ; Use a help register
  1991. add r20d,[r21d+4*r23d] ; Replace register by helpregister }
  1992. subreg:=oper[i].ref^.index.number and $ff;
  1993. if i=1 then
  1994. pos:=get_insert_pos(Tai(previous),oper[i].ref^.base.number shr 8,oper[0].reg.number shr 8,
  1995. 0,unusedregsint)
  1996. else
  1997. pos:=get_insert_pos(Tai(previous),oper[i].ref^.base.number shr 8,0,0,unusedregsint);
  1998. rgget(list,pos,subreg,helpreg);
  1999. spill_registers:=true;
  2000. helpins:=Taicpu.op_ref_reg(A_MOV,reg2opsize(oper[i].ref^.index),spilltemplist[supreg],helpreg);
  2001. if pos=nil then
  2002. list.insertafter(helpins,list.first)
  2003. else
  2004. list.insertafter(helpins,pos.next);
  2005. oper[i].ref^.index:=helpreg;
  2006. rgunget(list,helpins,helpreg);
  2007. forward_allocation(Tai(helpins.next),unusedregsint);
  2008. end;
  2009. end;
  2010. if (oper[0].typ=top_reg) and (oper[0].reg.enum=R_INTREGISTER) then
  2011. begin
  2012. supreg:=oper[0].reg.number shr 8;
  2013. subreg:=oper[0].reg.number and $ff;
  2014. if supreg in r then
  2015. if oper[1].typ=top_ref then
  2016. begin
  2017. {Situation example:
  2018. add [r20d],r21d ; r21d must be spilled into [ebp-12]
  2019. Change into:
  2020. mov r22d,[ebp-12] ; Use a help register
  2021. add [r20d],r22d ; Replace register by helpregister }
  2022. pos:=get_insert_pos(Tai(previous),oper[0].reg.number shr 8,
  2023. oper[1].ref^.base.number shr 8,oper[1].ref^.index.number shr 8,
  2024. unusedregsint);
  2025. rgget(list,pos,subreg,helpreg);
  2026. spill_registers:=true;
  2027. helpins:=Taicpu.op_ref_reg(A_MOV,reg2opsize(oper[0].reg),spilltemplist[supreg],helpreg);
  2028. if pos=nil then
  2029. list.insertafter(helpins,list.first)
  2030. else
  2031. list.insertafter(helpins,pos.next);
  2032. oper[0].reg:=helpreg;
  2033. rgunget(list,helpins,helpreg);
  2034. forward_allocation(Tai(helpins.next),unusedregsint);
  2035. end
  2036. else
  2037. begin
  2038. {Situation example:
  2039. add r20d,r21d ; r21d must be spilled into [ebp-12]
  2040. Change into:
  2041. add r20d,[ebp-12] ; Replace register by reference }
  2042. oper[0].typ:=top_ref;
  2043. new(oper[0].ref);
  2044. oper[0].ref^:=spilltemplist[supreg];
  2045. end;
  2046. end;
  2047. if (oper[1].typ=top_reg) and (oper[1].reg.enum=R_INTREGISTER) then
  2048. begin
  2049. supreg:=oper[1].reg.number shr 8;
  2050. subreg:=oper[1].reg.number and $ff;
  2051. if supreg in r then
  2052. begin
  2053. if oper[0].typ=top_ref then
  2054. begin
  2055. {Situation example:
  2056. add r20d,[r21d] ; r20d must be spilled into [ebp-12]
  2057. Change into:
  2058. mov r22d,[r21d] ; Use a help register
  2059. add [ebp-12],r22d ; Replace register by helpregister }
  2060. pos:=get_insert_pos(Tai(previous),oper[0].ref^.base.number shr 8,
  2061. oper[0].ref^.index.number shr 8,0,unusedregsint);
  2062. rgget(list,pos,subreg,helpreg);
  2063. spill_registers:=true;
  2064. op:=A_MOV;
  2065. hopsize:=opsize; {Save old value...}
  2066. if (opcode=A_MOVZX) or (opcode=A_MOVSX) or (opcode=A_LEA) then
  2067. begin
  2068. {Because 'movzx memory,register' does not exist...}
  2069. op:=opcode;
  2070. opcode:=A_MOV;
  2071. opsize:=reg2opsize(oper[1].reg);
  2072. end;
  2073. helpins:=Taicpu.op_ref_reg(op,hopsize,oper[0].ref^,helpreg);
  2074. if pos=nil then
  2075. list.insertafter(helpins,list.first)
  2076. else
  2077. list.insertafter(helpins,pos.next);
  2078. dispose(oper[0].ref);
  2079. oper[0].typ:=top_reg;
  2080. oper[0].reg:=helpreg;
  2081. oper[1].typ:=top_ref;
  2082. new(oper[1].ref);
  2083. oper[1].ref^:=spilltemplist[supreg];
  2084. rgunget(list,helpins,helpreg);
  2085. forward_allocation(Tai(helpins.next),unusedregsint);
  2086. end
  2087. else
  2088. begin
  2089. {Situation example:
  2090. add r20d,r21d ; r20d must be spilled into [ebp-12]
  2091. Change into:
  2092. add [ebp-12],r21d ; Replace register by reference }
  2093. if (opcode=A_MOVZX) or (opcode=A_MOVSX) then
  2094. begin
  2095. {Because 'movzx memory,register' does not exist...}
  2096. spill_registers:=true;
  2097. op:=opcode;
  2098. opcode:=A_MOV;
  2099. opsize:=reg2opsize(oper[1].reg);
  2100. pos:=get_insert_pos(Tai(previous),oper[0].reg.number shr 8,0,0,unusedregsint);
  2101. rgget(list,pos,subreg,helpreg);
  2102. helpins:=Taicpu.op_reg_reg(op,hopsize,oper[0].reg,helpreg);
  2103. if pos=nil then
  2104. list.insertafter(helpins,list.first)
  2105. else
  2106. list.insertafter(helpins,pos.next);
  2107. rgunget(list,helpins,helpreg);
  2108. forward_allocation(Tai(helpins.next),unusedregsint);
  2109. end;
  2110. oper[1].typ:=top_ref;
  2111. new(oper[1].ref);
  2112. oper[1].ref^:=spilltemplist[supreg];
  2113. end;
  2114. {The i386 instruction set never gets boring... IMUL does
  2115. not support a memory location as destination. Check if
  2116. the opcode is IMUL and fix it. (DM)}
  2117. if opcode=A_IMUL then
  2118. begin
  2119. {Yikes! We just changed the destination register into
  2120. a memory location above here.
  2121. Situation example:
  2122. imul [ebp-12],r21d ; We need a help register
  2123. Change into:
  2124. mov r22d,[ebp-12] ; Use a help instruction (only for IMUL)
  2125. imul r22d,r21d ; Replace reference by helpregister
  2126. mov [ebp-12],r22d ; Use another help instruction}
  2127. rgget(list,Tai(previous),subreg,helpreg);
  2128. {First help instruction.}
  2129. helpins:=Taicpu.op_ref_reg(A_MOV,opsize,oper[1].ref^,helpreg);
  2130. if previous=nil then
  2131. list.insert(helpins)
  2132. else
  2133. list.insertafter(helpins,previous);
  2134. {Second help instruction.}
  2135. helpins:=Taicpu.op_reg_ref(A_MOV,opsize,helpreg,oper[1].ref^);
  2136. dispose(oper[1].ref);
  2137. oper[1].typ:=top_reg;
  2138. oper[1].reg:=helpreg;
  2139. list.insertafter(helpins,self);
  2140. end;
  2141. end;
  2142. end;
  2143. end;
  2144. 3:
  2145. begin
  2146. {$warning todo!!}
  2147. end;
  2148. end;
  2149. end;
  2150. {$endif NEWRA}
  2151. {*****************************************************************************
  2152. Instruction table
  2153. *****************************************************************************}
  2154. procedure BuildInsTabCache;
  2155. {$ifndef NOAG386BIN}
  2156. var
  2157. i : longint;
  2158. {$endif}
  2159. begin
  2160. {$ifndef NOAG386BIN}
  2161. new(instabcache);
  2162. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  2163. i:=0;
  2164. while (i<InsTabEntries) do
  2165. begin
  2166. if InsTabCache^[InsTab[i].OPcode]=-1 then
  2167. InsTabCache^[InsTab[i].OPcode]:=i;
  2168. inc(i);
  2169. end;
  2170. {$endif NOAG386BIN}
  2171. end;
  2172. procedure InitAsm;
  2173. begin
  2174. {$ifndef NOAG386BIN}
  2175. if not assigned(instabcache) then
  2176. BuildInsTabCache;
  2177. {$endif NOAG386BIN}
  2178. end;
  2179. procedure DoneAsm;
  2180. begin
  2181. {$ifndef NOAG386BIN}
  2182. if assigned(instabcache) then
  2183. begin
  2184. dispose(instabcache);
  2185. instabcache:=nil;
  2186. end;
  2187. {$endif NOAG386BIN}
  2188. end;
  2189. end.
  2190. {
  2191. $Log$
  2192. Revision 1.16 2003-08-21 17:20:19 peter
  2193. * first spill the registers of top_ref before spilling top_reg
  2194. Revision 1.15 2003/08/21 14:48:36 peter
  2195. * fix reg-supreg range check error
  2196. Revision 1.14 2003/08/20 16:52:01 daniel
  2197. * Some old register convention code removed
  2198. * A few changes to eliminate a few lines of code
  2199. Revision 1.13 2003/08/20 09:07:00 daniel
  2200. * New register coding now mandatory, some more convert_registers calls
  2201. removed.
  2202. Revision 1.12 2003/08/20 07:48:04 daniel
  2203. * Made internal assembler use new register coding
  2204. Revision 1.11 2003/08/19 13:58:33 daniel
  2205. * Corrected a comment.
  2206. Revision 1.10 2003/08/15 14:44:20 daniel
  2207. * Fixed newra compilation
  2208. Revision 1.9 2003/08/11 21:18:20 peter
  2209. * start of sparc support for newra
  2210. Revision 1.8 2003/08/09 18:56:54 daniel
  2211. * cs_regalloc renamed to cs_regvars to avoid confusion with register
  2212. allocator
  2213. * Some preventive changes to i386 spillinh code
  2214. Revision 1.7 2003/07/06 15:31:21 daniel
  2215. * Fixed register allocator. *Lots* of fixes.
  2216. Revision 1.6 2003/06/14 14:53:50 jonas
  2217. * fixed newra cycle for x86
  2218. * added constants for indicating source and destination operands of the
  2219. "move reg,reg" instruction to aasmcpu (and use those in rgobj)
  2220. Revision 1.5 2003/06/03 13:01:59 daniel
  2221. * Register allocator finished
  2222. Revision 1.4 2003/05/30 23:57:08 peter
  2223. * more sparc cleanup
  2224. * accumulator removed, splitted in function_return_reg (called) and
  2225. function_result_reg (caller)
  2226. Revision 1.3 2003/05/22 21:33:31 peter
  2227. * removed some unit dependencies
  2228. Revision 1.2 2002/04/25 16:12:09 florian
  2229. * fixed more problems with cpubase and x86-64
  2230. Revision 1.1 2003/04/25 12:43:40 florian
  2231. * merged i386/aasmcpu and x86_64/aasmcpu to x86/aasmcpu
  2232. Revision 1.18 2003/04/25 12:04:31 florian
  2233. * merged agx64att and ag386att to x86/agx86att
  2234. Revision 1.17 2003/04/22 14:33:38 peter
  2235. * removed some notes/hints
  2236. Revision 1.16 2003/04/22 10:09:35 daniel
  2237. + Implemented the actual register allocator
  2238. + Scratch registers unavailable when new register allocator used
  2239. + maybe_save/maybe_restore unavailable when new register allocator used
  2240. Revision 1.15 2003/03/26 12:50:54 armin
  2241. * avoid problems with the ide in init/dome
  2242. Revision 1.14 2003/03/08 08:59:07 daniel
  2243. + $define newra will enable new register allocator
  2244. + getregisterint will return imaginary registers with $newra
  2245. + -sr switch added, will skip register allocation so you can see
  2246. the direct output of the code generator before register allocation
  2247. Revision 1.13 2003/02/25 07:41:54 daniel
  2248. * Properly fixed reversed operands bug
  2249. Revision 1.12 2003/02/19 22:00:15 daniel
  2250. * Code generator converted to new register notation
  2251. - Horribily outdated todo.txt removed
  2252. Revision 1.11 2003/01/09 20:40:59 daniel
  2253. * Converted some code in cgx86.pas to new register numbering
  2254. Revision 1.10 2003/01/08 18:43:57 daniel
  2255. * Tregister changed into a record
  2256. Revision 1.9 2003/01/05 13:36:53 florian
  2257. * x86-64 compiles
  2258. + very basic support for float128 type (x86-64 only)
  2259. Revision 1.8 2002/11/17 16:31:58 carl
  2260. * memory optimization (3-4%) : cleanup of tai fields,
  2261. cleanup of tdef and tsym fields.
  2262. * make it work for m68k
  2263. Revision 1.7 2002/11/15 01:58:54 peter
  2264. * merged changes from 1.0.7 up to 04-11
  2265. - -V option for generating bug report tracing
  2266. - more tracing for option parsing
  2267. - errors for cdecl and high()
  2268. - win32 import stabs
  2269. - win32 records<=8 are returned in eax:edx (turned off by default)
  2270. - heaptrc update
  2271. - more info for temp management in .s file with EXTDEBUG
  2272. Revision 1.6 2002/10/31 13:28:32 pierre
  2273. * correct last wrong fix for tw2158
  2274. Revision 1.5 2002/10/30 17:10:00 pierre
  2275. * merge of fix for tw2158 bug
  2276. Revision 1.4 2002/08/15 19:10:36 peter
  2277. * first things tai,tnode storing in ppu
  2278. Revision 1.3 2002/08/13 18:01:52 carl
  2279. * rename swatoperands to swapoperands
  2280. + m68k first compilable version (still needs a lot of testing):
  2281. assembler generator, system information , inline
  2282. assembler reader.
  2283. Revision 1.2 2002/07/20 11:57:59 florian
  2284. * types.pas renamed to defbase.pas because D6 contains a types
  2285. unit so this would conflicts if D6 programms are compiled
  2286. + Willamette/SSE2 instructions to assembler added
  2287. Revision 1.1 2002/07/01 18:46:29 peter
  2288. * internal linker
  2289. * reorganized aasm layer
  2290. }