aasmcpu.pas 88 KB

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