aasmcpu.pas 88 KB

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