aasmcpu.pas 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020
  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,globtype,globals,verbose,
  26. cpuinfo,cpubase,
  27. cgbase,
  28. 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 x8664nop.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);override;
  127. constructor create_op(b: byte; _op: byte);override;
  128. function calculatefillbuf(var buf : tfillbuffer):pchar;override;
  129. end;
  130. taicpu = class(tai_cpu_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 : aint);
  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: aint);
  140. constructor op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  141. constructor op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  142. constructor op_const_ref(op : tasmop;_size : topsize;_op1 : aint;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 : aint;_op2 : tregister;_op3 : tregister);
  146. constructor op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;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 : aint;_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(objdata:TAsmObjectdata);virtual;
  169. procedure SetOperandOrder(order:TOperandOrder);
  170. function is_same_reg_move(regtype: Tregistertype):boolean;override;
  171. protected
  172. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);override;
  173. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);override;
  174. procedure ppubuildderefimploper(var o:toper);override;
  175. procedure ppuderefoper(var o:toper);override;
  176. private
  177. { next fields are filled in pass1, so pass2 is faster }
  178. inssize : shortint;
  179. insoffset : longint;
  180. LastInsOffset : longint; { need to be public to be reset }
  181. insentry : PInsEntry;
  182. function InsEnd:longint;
  183. procedure create_ot;
  184. function Matches(p:PInsEntry):longint;
  185. function calcsize(p:PInsEntry):longint;
  186. procedure gencode(objdata:TAsmObjectData);
  187. function NeedAddrPrefix(opidx:byte):boolean;
  188. procedure Swapoperands;
  189. function FindInsentry:boolean;
  190. {$endif NOAG386BIN}
  191. end;
  192. function spilling_create_load(const ref:treference;r:tregister): tai;
  193. function spilling_create_store(r:tregister; const ref:treference): tai;
  194. procedure InitAsm;
  195. procedure DoneAsm;
  196. implementation
  197. uses
  198. cutils,
  199. itcpugas;
  200. {*****************************************************************************
  201. Instruction table
  202. *****************************************************************************}
  203. const
  204. {Instruction flags }
  205. IF_NONE = $00000000;
  206. IF_SM = $00000001; { size match first two operands }
  207. IF_SM2 = $00000002;
  208. IF_SB = $00000004; { unsized operands can't be non-byte }
  209. IF_SW = $00000008; { unsized operands can't be non-word }
  210. IF_SD = $00000010; { unsized operands can't be nondword }
  211. IF_AR0 = $00000020; { SB, SW, SD applies to argument 0 }
  212. IF_AR1 = $00000040; { SB, SW, SD applies to argument 1 }
  213. IF_AR2 = $00000060; { SB, SW, SD applies to argument 2 }
  214. IF_ARMASK = $00000060; { mask for unsized argument spec }
  215. IF_PRIV = $00000100; { it's a privileged instruction }
  216. IF_SMM = $00000200; { it's only valid in SMM }
  217. IF_PROT = $00000400; { it's protected mode only }
  218. IF_NOX86_64 = $00000800; { removed instruction in x86_64 }
  219. IF_UNDOC = $00001000; { it's an undocumented instruction }
  220. IF_FPU = $00002000; { it's an FPU instruction }
  221. IF_MMX = $00004000; { it's an MMX instruction }
  222. { it's a 3DNow! instruction }
  223. IF_3DNOW = $00008000;
  224. { it's a SSE (KNI, MMX2) instruction }
  225. IF_SSE = $00010000;
  226. { SSE2 instructions }
  227. IF_SSE2 = $00020000;
  228. { SSE3 instructions }
  229. IF_SSE3 = $00040000;
  230. { SSE64 instructions }
  231. IF_SSE64 = $00080000;
  232. { the mask for processor types }
  233. {IF_PMASK = longint($FF000000);}
  234. { the mask for disassembly "prefer" }
  235. {IF_PFMASK = longint($F001FF00);}
  236. IF_8086 = $00000000; { 8086 instruction }
  237. IF_186 = $01000000; { 186+ instruction }
  238. IF_286 = $02000000; { 286+ instruction }
  239. IF_386 = $03000000; { 386+ instruction }
  240. IF_486 = $04000000; { 486+ instruction }
  241. IF_PENT = $05000000; { Pentium instruction }
  242. IF_P6 = $06000000; { P6 instruction }
  243. IF_KATMAI = $07000000; { Katmai instructions }
  244. { Willamette instructions }
  245. IF_WILLAMETTE = $08000000;
  246. { Prescott instructions }
  247. IF_PRESCOTT = $09000000;
  248. IF_X86_64 = $0a000000;
  249. IF_CYRIX = $10000000; { Cyrix-specific instruction }
  250. IF_AMD = $20000000; { AMD-specific instruction }
  251. { added flags }
  252. IF_PRE = $40000000; { it's a prefix instruction }
  253. IF_PASS2 = longint($80000000); { if the instruction can change in a second pass }
  254. type
  255. TInsTabCache=array[TasmOp] of longint;
  256. PInsTabCache=^TInsTabCache;
  257. const
  258. {$ifdef x86_64}
  259. InsTab:array[0..instabentries-1] of TInsEntry={$i x8664tab.inc}
  260. {$else x86_64}
  261. InsTab:array[0..instabentries-1] of TInsEntry={$i i386tab.inc}
  262. {$endif x86_64}
  263. var
  264. InsTabCache : PInsTabCache;
  265. const
  266. {$ifdef x86_64}
  267. { Intel style operands ! }
  268. opsize_2_type:array[0..2,topsize] of longint=(
  269. (OT_NONE,
  270. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,OT_BITS64,OT_BITS64,OT_BITS64,
  271. OT_BITS16,OT_BITS32,OT_BITS64,
  272. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  273. OT_BITS64,
  274. OT_NEAR,OT_FAR,OT_SHORT
  275. ),
  276. (OT_NONE,
  277. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,OT_BITS8,OT_BITS16,OT_BITS32,
  278. OT_BITS16,OT_BITS32,OT_BITS64,
  279. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  280. OT_BITS64,
  281. OT_NEAR,OT_FAR,OT_SHORT
  282. ),
  283. (OT_NONE,
  284. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,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_NONE,
  287. OT_BITS64,
  288. OT_NEAR,OT_FAR,OT_SHORT
  289. )
  290. );
  291. reg_ot_table : array[tregisterindex] of longint = (
  292. {$i r8664ot.inc}
  293. );
  294. {$else x86_64}
  295. { Intel style operands ! }
  296. opsize_2_type:array[0..2,topsize] of longint=(
  297. (OT_NONE,
  298. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,
  299. OT_BITS16,OT_BITS32,OT_BITS64,
  300. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  301. OT_BITS64,
  302. OT_NEAR,OT_FAR,OT_SHORT
  303. ),
  304. (OT_NONE,
  305. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,
  306. OT_BITS16,OT_BITS32,OT_BITS64,
  307. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  308. OT_BITS64,
  309. OT_NEAR,OT_FAR,OT_SHORT
  310. ),
  311. (OT_NONE,
  312. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,
  313. OT_BITS16,OT_BITS32,OT_BITS64,
  314. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  315. OT_BITS64,
  316. OT_NEAR,OT_FAR,OT_SHORT
  317. )
  318. );
  319. reg_ot_table : array[tregisterindex] of longint = (
  320. {$i r386ot.inc}
  321. );
  322. {$endif x86_64}
  323. {****************************************************************************
  324. TAI_ALIGN
  325. ****************************************************************************}
  326. constructor tai_align.create(b: byte);
  327. begin
  328. inherited create(b);
  329. reg:=NR_ECX;
  330. end;
  331. constructor tai_align.create_op(b: byte; _op: byte);
  332. begin
  333. inherited create_op(b,_op);
  334. reg:=NR_NO;
  335. end;
  336. function tai_align.calculatefillbuf(var buf : tfillbuffer):pchar;
  337. const
  338. alignarray:array[0..5] of string[8]=(
  339. #$8D#$B4#$26#$00#$00#$00#$00,
  340. #$8D#$B6#$00#$00#$00#$00,
  341. #$8D#$74#$26#$00,
  342. #$8D#$76#$00,
  343. #$89#$F6,
  344. #$90
  345. );
  346. var
  347. bufptr : pchar;
  348. j : longint;
  349. begin
  350. inherited calculatefillbuf(buf);
  351. if not use_op then
  352. begin
  353. bufptr:=pchar(@buf);
  354. while (fillsize>0) do
  355. begin
  356. for j:=0 to 5 do
  357. if (fillsize>=length(alignarray[j])) then
  358. break;
  359. move(alignarray[j][1],bufptr^,length(alignarray[j]));
  360. inc(bufptr,length(alignarray[j]));
  361. dec(fillsize,length(alignarray[j]));
  362. end;
  363. end;
  364. calculatefillbuf:=pchar(@buf);
  365. end;
  366. {*****************************************************************************
  367. Taicpu Constructors
  368. *****************************************************************************}
  369. procedure taicpu.changeopsize(siz:topsize);
  370. begin
  371. opsize:=siz;
  372. end;
  373. procedure taicpu.init(_size : topsize);
  374. begin
  375. { default order is att }
  376. FOperandOrder:=op_att;
  377. segprefix:=NR_NO;
  378. opsize:=_size;
  379. {$ifndef NOAG386BIN}
  380. insentry:=nil;
  381. LastInsOffset:=-1;
  382. InsOffset:=0;
  383. InsSize:=0;
  384. {$endif}
  385. end;
  386. constructor taicpu.op_none(op : tasmop);
  387. begin
  388. inherited create(op);
  389. init(S_NO);
  390. end;
  391. constructor taicpu.op_none(op : tasmop;_size : topsize);
  392. begin
  393. inherited create(op);
  394. init(_size);
  395. end;
  396. constructor taicpu.op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  397. begin
  398. inherited create(op);
  399. init(_size);
  400. ops:=1;
  401. loadreg(0,_op1);
  402. end;
  403. constructor taicpu.op_const(op : tasmop;_size : topsize;_op1 : aint);
  404. begin
  405. inherited create(op);
  406. init(_size);
  407. ops:=1;
  408. loadconst(0,_op1);
  409. end;
  410. constructor taicpu.op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  411. begin
  412. inherited create(op);
  413. init(_size);
  414. ops:=1;
  415. loadref(0,_op1);
  416. end;
  417. constructor taicpu.op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  418. begin
  419. inherited create(op);
  420. init(_size);
  421. ops:=2;
  422. loadreg(0,_op1);
  423. loadreg(1,_op2);
  424. end;
  425. constructor taicpu.op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  426. begin
  427. inherited create(op);
  428. init(_size);
  429. ops:=2;
  430. loadreg(0,_op1);
  431. loadconst(1,_op2);
  432. end;
  433. constructor taicpu.op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  434. begin
  435. inherited create(op);
  436. init(_size);
  437. ops:=2;
  438. loadreg(0,_op1);
  439. loadref(1,_op2);
  440. end;
  441. constructor taicpu.op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  442. begin
  443. inherited create(op);
  444. init(_size);
  445. ops:=2;
  446. loadconst(0,_op1);
  447. loadreg(1,_op2);
  448. end;
  449. constructor taicpu.op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  450. begin
  451. inherited create(op);
  452. init(_size);
  453. ops:=2;
  454. loadconst(0,_op1);
  455. loadconst(1,_op2);
  456. end;
  457. constructor taicpu.op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  458. begin
  459. inherited create(op);
  460. init(_size);
  461. ops:=2;
  462. loadconst(0,_op1);
  463. loadref(1,_op2);
  464. end;
  465. constructor taicpu.op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  466. begin
  467. inherited create(op);
  468. init(_size);
  469. ops:=2;
  470. loadref(0,_op1);
  471. loadreg(1,_op2);
  472. end;
  473. constructor taicpu.op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  474. begin
  475. inherited create(op);
  476. init(_size);
  477. ops:=3;
  478. loadreg(0,_op1);
  479. loadreg(1,_op2);
  480. loadreg(2,_op3);
  481. end;
  482. constructor taicpu.op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  483. begin
  484. inherited create(op);
  485. init(_size);
  486. ops:=3;
  487. loadconst(0,_op1);
  488. loadreg(1,_op2);
  489. loadreg(2,_op3);
  490. end;
  491. constructor taicpu.op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister;const _op3 : treference);
  492. begin
  493. inherited create(op);
  494. init(_size);
  495. ops:=3;
  496. loadreg(0,_op1);
  497. loadreg(1,_op2);
  498. loadref(2,_op3);
  499. end;
  500. constructor taicpu.op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  501. begin
  502. inherited create(op);
  503. init(_size);
  504. ops:=3;
  505. loadconst(0,_op1);
  506. loadref(1,_op2);
  507. loadreg(2,_op3);
  508. end;
  509. constructor taicpu.op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  510. begin
  511. inherited create(op);
  512. init(_size);
  513. ops:=3;
  514. loadconst(0,_op1);
  515. loadreg(1,_op2);
  516. loadref(2,_op3);
  517. end;
  518. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  519. begin
  520. inherited create(op);
  521. init(_size);
  522. condition:=cond;
  523. ops:=1;
  524. loadsymbol(0,_op1,0);
  525. end;
  526. constructor taicpu.op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  527. begin
  528. inherited create(op);
  529. init(_size);
  530. ops:=1;
  531. loadsymbol(0,_op1,0);
  532. end;
  533. constructor taicpu.op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  534. begin
  535. inherited create(op);
  536. init(_size);
  537. ops:=1;
  538. loadsymbol(0,_op1,_op1ofs);
  539. end;
  540. constructor taicpu.op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  541. begin
  542. inherited create(op);
  543. init(_size);
  544. ops:=2;
  545. loadsymbol(0,_op1,_op1ofs);
  546. loadreg(1,_op2);
  547. end;
  548. constructor taicpu.op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  549. begin
  550. inherited create(op);
  551. init(_size);
  552. ops:=2;
  553. loadsymbol(0,_op1,_op1ofs);
  554. loadref(1,_op2);
  555. end;
  556. function taicpu.GetString:string;
  557. var
  558. i : longint;
  559. s : string;
  560. addsize : boolean;
  561. begin
  562. s:='['+std_op2str[opcode];
  563. for i:=0 to ops-1 do
  564. begin
  565. with oper[i]^ do
  566. begin
  567. if i=0 then
  568. s:=s+' '
  569. else
  570. s:=s+',';
  571. { type }
  572. addsize:=false;
  573. if (ot and OT_XMMREG)=OT_XMMREG then
  574. s:=s+'xmmreg'
  575. else
  576. if (ot and OT_MMXREG)=OT_MMXREG then
  577. s:=s+'mmxreg'
  578. else
  579. if (ot and OT_FPUREG)=OT_FPUREG then
  580. s:=s+'fpureg'
  581. else
  582. if (ot and OT_REGISTER)=OT_REGISTER then
  583. begin
  584. s:=s+'reg';
  585. addsize:=true;
  586. end
  587. else
  588. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  589. begin
  590. s:=s+'imm';
  591. addsize:=true;
  592. end
  593. else
  594. if (ot and OT_MEMORY)=OT_MEMORY then
  595. begin
  596. s:=s+'mem';
  597. addsize:=true;
  598. end
  599. else
  600. s:=s+'???';
  601. { size }
  602. if addsize then
  603. begin
  604. if (ot and OT_BITS8)<>0 then
  605. s:=s+'8'
  606. else
  607. if (ot and OT_BITS16)<>0 then
  608. s:=s+'16'
  609. else
  610. if (ot and OT_BITS32)<>0 then
  611. s:=s+'32'
  612. else
  613. s:=s+'??';
  614. { signed }
  615. if (ot and OT_SIGNED)<>0 then
  616. s:=s+'s';
  617. end;
  618. end;
  619. end;
  620. GetString:=s+']';
  621. end;
  622. procedure taicpu.Swapoperands;
  623. var
  624. p : POper;
  625. begin
  626. { Fix the operands which are in AT&T style and we need them in Intel style }
  627. case ops of
  628. 2 : begin
  629. { 0,1 -> 1,0 }
  630. p:=oper[0];
  631. oper[0]:=oper[1];
  632. oper[1]:=p;
  633. end;
  634. 3 : begin
  635. { 0,1,2 -> 2,1,0 }
  636. p:=oper[0];
  637. oper[0]:=oper[2];
  638. oper[2]:=p;
  639. end;
  640. end;
  641. end;
  642. procedure taicpu.SetOperandOrder(order:TOperandOrder);
  643. begin
  644. if FOperandOrder<>order then
  645. begin
  646. Swapoperands;
  647. FOperandOrder:=order;
  648. end;
  649. end;
  650. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  651. begin
  652. o.typ:=toptype(ppufile.getbyte);
  653. o.ot:=ppufile.getlongint;
  654. case o.typ of
  655. top_reg :
  656. ppufile.getdata(o.reg,sizeof(Tregister));
  657. top_ref :
  658. begin
  659. new(o.ref);
  660. ppufile.getdata(o.ref^.segment,sizeof(Tregister));
  661. ppufile.getdata(o.ref^.base,sizeof(Tregister));
  662. ppufile.getdata(o.ref^.index,sizeof(Tregister));
  663. o.ref^.scalefactor:=ppufile.getbyte;
  664. o.ref^.offset:=ppufile.getaint;
  665. o.ref^.symbol:=ppufile.getasmsymbol;
  666. o.ref^.relsymbol:=ppufile.getasmsymbol;
  667. end;
  668. top_const :
  669. o.val:=ppufile.getaint;
  670. top_local :
  671. begin
  672. new(o.localoper);
  673. with o.localoper^ do
  674. begin
  675. ppufile.getderef(localsymderef);
  676. localsymofs:=ppufile.getaint;
  677. localindexreg:=tregister(ppufile.getlongint);
  678. localscale:=ppufile.getbyte;
  679. localgetoffset:=(ppufile.getbyte<>0);
  680. end;
  681. end;
  682. end;
  683. end;
  684. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  685. begin
  686. ppufile.putbyte(byte(o.typ));
  687. ppufile.putlongint(o.ot);
  688. case o.typ of
  689. top_reg :
  690. ppufile.putdata(o.reg,sizeof(Tregister));
  691. top_ref :
  692. begin
  693. ppufile.putdata(o.ref^.segment,sizeof(Tregister));
  694. ppufile.putdata(o.ref^.base,sizeof(Tregister));
  695. ppufile.putdata(o.ref^.index,sizeof(Tregister));
  696. ppufile.putbyte(o.ref^.scalefactor);
  697. ppufile.putaint(o.ref^.offset);
  698. ppufile.putasmsymbol(o.ref^.symbol);
  699. ppufile.putasmsymbol(o.ref^.relsymbol);
  700. end;
  701. top_const :
  702. ppufile.putaint(o.val);
  703. top_local :
  704. begin
  705. with o.localoper^ do
  706. begin
  707. ppufile.putderef(localsymderef);
  708. ppufile.putaint(localsymofs);
  709. ppufile.putlongint(longint(localindexreg));
  710. ppufile.putbyte(localscale);
  711. ppufile.putbyte(byte(localgetoffset));
  712. end;
  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.localoper^.localsymderef.build(tvarsym(o.localoper^.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. if assigned(o.ref^.relsymbol) then
  731. objectlibrary.derefasmsymbol(o.ref^.relsymbol);
  732. end;
  733. top_local :
  734. o.localoper^.localsym:=tvarsym(o.localoper^.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. if ref^.refaddr=addr_no then
  822. begin
  823. { create ot field }
  824. if (ot and OT_SIZE_MASK)=0 then
  825. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  826. else
  827. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  828. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  829. ot:=ot or OT_MEM_OFFS;
  830. { fix scalefactor }
  831. if (ref^.index=NR_NO) then
  832. ref^.scalefactor:=0
  833. else
  834. if (ref^.scalefactor=0) then
  835. ref^.scalefactor:=1;
  836. end
  837. else
  838. begin
  839. l:=ref^.offset;
  840. if assigned(ref^.symbol) then
  841. inc(l,ref^.symbol.address);
  842. { when it is a forward jump we need to compensate the
  843. offset of the instruction since the previous time,
  844. because the symbol address is then still using the
  845. 'old-style' addressing.
  846. For backwards jumps this is not required because the
  847. address of the symbol is already adjusted to the
  848. new offset }
  849. if (l>InsOffset) and (LastInsOffset<>-1) then
  850. inc(l,InsOffset-LastInsOffset);
  851. { instruction size will then always become 2 (PFV) }
  852. relsize:=(InsOffset+2)-l;
  853. if (not assigned(ref^.symbol) or
  854. ((ref^.symbol.currbind<>AB_EXTERNAL) and (ref^.symbol.address<>0))) and
  855. (relsize>=-128) and (relsize<=127) then
  856. ot:=OT_IMM32 or OT_SHORT
  857. else
  858. ot:=OT_IMM32 or OT_NEAR;
  859. end;
  860. end;
  861. top_local :
  862. begin
  863. if (ot and OT_SIZE_MASK)=0 then
  864. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  865. else
  866. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  867. end;
  868. top_const :
  869. begin
  870. if (opsize<>S_W) and (longint(val)>=-128) and (val<=127) then
  871. ot:=OT_IMM8 or OT_SIGNED
  872. else
  873. ot:=OT_IMMEDIATE or opsize_2_type[i,opsize];
  874. end;
  875. top_none :
  876. begin
  877. { generated when there was an error in the
  878. assembler reader. It never happends when generating
  879. assembler }
  880. end;
  881. else
  882. internalerror(200402261);
  883. end;
  884. end;
  885. end;
  886. function taicpu.InsEnd:longint;
  887. begin
  888. InsEnd:=InsOffset+InsSize;
  889. end;
  890. function taicpu.Matches(p:PInsEntry):longint;
  891. { * IF_SM stands for Size Match: any operand whose size is not
  892. * explicitly specified by the template is `really' intended to be
  893. * the same size as the first size-specified operand.
  894. * Non-specification is tolerated in the input instruction, but
  895. * _wrong_ specification is not.
  896. *
  897. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  898. * three-operand instructions such as SHLD: it implies that the
  899. * first two operands must match in size, but that the third is
  900. * required to be _unspecified_.
  901. *
  902. * IF_SB invokes Size Byte: operands with unspecified size in the
  903. * template are really bytes, and so no non-byte specification in
  904. * the input instruction will be tolerated. IF_SW similarly invokes
  905. * Size Word, and IF_SD invokes Size Doubleword.
  906. *
  907. * (The default state if neither IF_SM nor IF_SM2 is specified is
  908. * that any operand with unspecified size in the template is
  909. * required to have unspecified size in the instruction too...)
  910. }
  911. var
  912. i,j,asize,oprs : longint;
  913. siz : array[0..2] of longint;
  914. begin
  915. Matches:=100;
  916. { Check the opcode and operands }
  917. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  918. begin
  919. Matches:=0;
  920. exit;
  921. end;
  922. { Check that no spurious colons or TOs are present }
  923. for i:=0 to p^.ops-1 do
  924. if (oper[i]^.ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  925. begin
  926. Matches:=0;
  927. exit;
  928. end;
  929. { Check that the operand flags all match up }
  930. for i:=0 to p^.ops-1 do
  931. begin
  932. if ((p^.optypes[i] and (not oper[i]^.ot)) or
  933. ((p^.optypes[i] and OT_SIZE_MASK) and
  934. ((p^.optypes[i] xor oper[i]^.ot) and OT_SIZE_MASK)))<>0 then
  935. begin
  936. if ((p^.optypes[i] and (not oper[i]^.ot) and OT_NON_SIZE) or
  937. (oper[i]^.ot and OT_SIZE_MASK))<>0 then
  938. begin
  939. Matches:=0;
  940. exit;
  941. end
  942. else
  943. Matches:=1;
  944. end;
  945. end;
  946. { Check operand sizes }
  947. { as default an untyped size can get all the sizes, this is different
  948. from nasm, but else we need to do a lot checking which opcodes want
  949. size or not with the automatic size generation }
  950. asize:=longint($ffffffff);
  951. if (p^.flags and IF_SB)<>0 then
  952. asize:=OT_BITS8
  953. else if (p^.flags and IF_SW)<>0 then
  954. asize:=OT_BITS16
  955. else if (p^.flags and IF_SD)<>0 then
  956. asize:=OT_BITS32;
  957. if (p^.flags and IF_ARMASK)<>0 then
  958. begin
  959. siz[0]:=0;
  960. siz[1]:=0;
  961. siz[2]:=0;
  962. if (p^.flags and IF_AR0)<>0 then
  963. siz[0]:=asize
  964. else if (p^.flags and IF_AR1)<>0 then
  965. siz[1]:=asize
  966. else if (p^.flags and IF_AR2)<>0 then
  967. siz[2]:=asize;
  968. end
  969. else
  970. begin
  971. { we can leave because the size for all operands is forced to be
  972. the same
  973. but not if IF_SB IF_SW or IF_SD is set PM }
  974. if asize=-1 then
  975. exit;
  976. siz[0]:=asize;
  977. siz[1]:=asize;
  978. siz[2]:=asize;
  979. end;
  980. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  981. begin
  982. if (p^.flags and IF_SM2)<>0 then
  983. oprs:=2
  984. else
  985. oprs:=p^.ops;
  986. for i:=0 to oprs-1 do
  987. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  988. begin
  989. for j:=0 to oprs-1 do
  990. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  991. break;
  992. end;
  993. end
  994. else
  995. oprs:=2;
  996. { Check operand sizes }
  997. for i:=0 to p^.ops-1 do
  998. begin
  999. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  1000. ((oper[i]^.ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1001. { Immediates can always include smaller size }
  1002. ((oper[i]^.ot and OT_IMMEDIATE)=0) and
  1003. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i]^.ot and OT_SIZE_MASK)) then
  1004. Matches:=2;
  1005. end;
  1006. end;
  1007. procedure taicpu.ResetPass1;
  1008. begin
  1009. { we need to reset everything here, because the choosen insentry
  1010. can be invalid for a new situation where the previously optimized
  1011. insentry is not correct }
  1012. InsEntry:=nil;
  1013. InsSize:=0;
  1014. LastInsOffset:=-1;
  1015. end;
  1016. procedure taicpu.ResetPass2;
  1017. begin
  1018. { we are here in a second pass, check if the instruction can be optimized }
  1019. if assigned(InsEntry) and
  1020. ((InsEntry^.flags and IF_PASS2)<>0) then
  1021. begin
  1022. InsEntry:=nil;
  1023. InsSize:=0;
  1024. end;
  1025. LastInsOffset:=-1;
  1026. end;
  1027. function taicpu.CheckIfValid:boolean;
  1028. begin
  1029. result:=FindInsEntry;
  1030. end;
  1031. function taicpu.FindInsentry:boolean;
  1032. var
  1033. i : longint;
  1034. begin
  1035. result:=false;
  1036. { Things which may only be done once, not when a second pass is done to
  1037. optimize }
  1038. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1039. begin
  1040. { We need intel style operands }
  1041. SetOperandOrder(op_intel);
  1042. { create the .ot fields }
  1043. create_ot;
  1044. { set the file postion }
  1045. aktfilepos:=fileinfo;
  1046. end
  1047. else
  1048. begin
  1049. { we've already an insentry so it's valid }
  1050. result:=true;
  1051. exit;
  1052. end;
  1053. { Lookup opcode in the table }
  1054. InsSize:=-1;
  1055. i:=instabcache^[opcode];
  1056. if i=-1 then
  1057. begin
  1058. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1059. exit;
  1060. end;
  1061. insentry:=@instab[i];
  1062. while (insentry^.opcode=opcode) do
  1063. begin
  1064. if matches(insentry)=100 then
  1065. begin
  1066. result:=true;
  1067. exit;
  1068. end;
  1069. inc(i);
  1070. insentry:=@instab[i];
  1071. end;
  1072. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1073. { No instruction found, set insentry to nil and inssize to -1 }
  1074. insentry:=nil;
  1075. inssize:=-1;
  1076. end;
  1077. function taicpu.Pass1(offset:longint):longint;
  1078. begin
  1079. Pass1:=0;
  1080. { Save the old offset and set the new offset }
  1081. InsOffset:=Offset;
  1082. { Error? }
  1083. if (Insentry=nil) and (InsSize=-1) then
  1084. exit;
  1085. { set the file postion }
  1086. aktfilepos:=fileinfo;
  1087. { Get InsEntry }
  1088. if FindInsEntry then
  1089. begin
  1090. { Calculate instruction size }
  1091. InsSize:=calcsize(insentry);
  1092. if segprefix<>NR_NO then
  1093. inc(InsSize);
  1094. { Fix opsize if size if forced }
  1095. if (insentry^.flags and (IF_SB or IF_SW or IF_SD))<>0 then
  1096. begin
  1097. if (insentry^.flags and IF_ARMASK)=0 then
  1098. begin
  1099. if (insentry^.flags and IF_SB)<>0 then
  1100. begin
  1101. if opsize=S_NO then
  1102. opsize:=S_B;
  1103. end
  1104. else if (insentry^.flags and IF_SW)<>0 then
  1105. begin
  1106. if opsize=S_NO then
  1107. opsize:=S_W;
  1108. end
  1109. else if (insentry^.flags and IF_SD)<>0 then
  1110. begin
  1111. if opsize=S_NO then
  1112. opsize:=S_L;
  1113. end;
  1114. end;
  1115. end;
  1116. LastInsOffset:=InsOffset;
  1117. Pass1:=InsSize;
  1118. exit;
  1119. end;
  1120. LastInsOffset:=-1;
  1121. end;
  1122. procedure taicpu.Pass2(objdata:TAsmObjectData);
  1123. var
  1124. c : longint;
  1125. begin
  1126. { error in pass1 ? }
  1127. if insentry=nil then
  1128. exit;
  1129. aktfilepos:=fileinfo;
  1130. { Segment override }
  1131. if (segprefix<>NR_NO) then
  1132. begin
  1133. case segprefix of
  1134. NR_CS : c:=$2e;
  1135. NR_DS : c:=$3e;
  1136. NR_ES : c:=$26;
  1137. NR_FS : c:=$64;
  1138. NR_GS : c:=$65;
  1139. NR_SS : c:=$36;
  1140. end;
  1141. objdata.writebytes(c,1);
  1142. { fix the offset for GenNode }
  1143. inc(InsOffset);
  1144. end;
  1145. { Generate the instruction }
  1146. GenCode(objdata);
  1147. end;
  1148. function taicpu.needaddrprefix(opidx:byte):boolean;
  1149. begin
  1150. result:=(oper[opidx]^.typ=top_ref) and
  1151. (oper[opidx]^.ref^.refaddr=addr_no) and
  1152. (
  1153. (
  1154. (oper[opidx]^.ref^.index<>NR_NO) and
  1155. (getsubreg(oper[opidx]^.ref^.index)<>R_SUBD)
  1156. ) or
  1157. (
  1158. (oper[opidx]^.ref^.base<>NR_NO) and
  1159. (getsubreg(oper[opidx]^.ref^.base)<>R_SUBD)
  1160. )
  1161. );
  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.typ<>top_ref) then
  1209. internalerror(200409262);
  1210. if ((input.ref^.index<>NR_NO) and (getregtype(input.ref^.index)<>R_INTREGISTER)) or
  1211. ((input.ref^.base<>NR_NO) and (getregtype(input.ref^.base)<>R_INTREGISTER)) then
  1212. internalerror(200301081);
  1213. ir:=input.ref^.index;
  1214. br:=input.ref^.base;
  1215. isub:=getsubreg(ir);
  1216. bsub:=getsubreg(br);
  1217. s:=input.ref^.scalefactor;
  1218. o:=input.ref^.offset;
  1219. sym:=input.ref^.symbol;
  1220. { it's direct address }
  1221. if (br=NR_NO) and (ir=NR_NO) then
  1222. begin
  1223. { it's a pure offset }
  1224. output.sib_present:=false;
  1225. output.bytes:=4;
  1226. output.modrm:=5 or (rfield shl 3);
  1227. end
  1228. else
  1229. { it's an indirection }
  1230. begin
  1231. { 16 bit address? }
  1232. if ((ir<>NR_NO) and (isub<>R_SUBD)) or
  1233. ((br<>NR_NO) and (bsub<>R_SUBD)) then
  1234. message(asmw_e_16bit_not_supported);
  1235. {$ifdef OPTEA}
  1236. { make single reg base }
  1237. if (br=NR_NO) and (s=1) then
  1238. begin
  1239. br:=ir;
  1240. ir:=NR_NO;
  1241. end;
  1242. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  1243. if (br=NR_NO) and
  1244. (((s=2) and (ir<>NR_ESP)) or
  1245. (s=3) or (s=5) or (s=9)) then
  1246. begin
  1247. br:=ir;
  1248. dec(s);
  1249. end;
  1250. { swap ESP into base if scalefactor is 1 }
  1251. if (s=1) and (ir=NR_ESP) then
  1252. begin
  1253. ir:=br;
  1254. br:=NR_ESP;
  1255. end;
  1256. {$endif OPTEA}
  1257. { wrong, for various reasons }
  1258. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1259. exit;
  1260. { base }
  1261. case br of
  1262. NR_EAX : base:=0;
  1263. NR_ECX : base:=1;
  1264. NR_EDX : base:=2;
  1265. NR_EBX : base:=3;
  1266. NR_ESP : base:=4;
  1267. NR_NO,
  1268. NR_EBP : base:=5;
  1269. NR_ESI : base:=6;
  1270. NR_EDI : base:=7;
  1271. else
  1272. exit;
  1273. end;
  1274. { index }
  1275. case ir of
  1276. NR_EAX : index:=0;
  1277. NR_ECX : index:=1;
  1278. NR_EDX : index:=2;
  1279. NR_EBX : index:=3;
  1280. NR_NO : index:=4;
  1281. NR_EBP : index:=5;
  1282. NR_ESI : index:=6;
  1283. NR_EDI : index:=7;
  1284. else
  1285. exit;
  1286. end;
  1287. case s of
  1288. 0,
  1289. 1 : scalefactor:=0;
  1290. 2 : scalefactor:=1;
  1291. 4 : scalefactor:=2;
  1292. 8 : scalefactor:=3;
  1293. else
  1294. exit;
  1295. end;
  1296. if (br=NR_NO) or
  1297. ((br<>NR_EBP) and (o=0) and (sym=nil)) then
  1298. md:=0
  1299. else
  1300. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1301. md:=1
  1302. else
  1303. md:=2;
  1304. if (br=NR_NO) or (md=2) then
  1305. output.bytes:=4
  1306. else
  1307. output.bytes:=md;
  1308. { SIB needed ? }
  1309. if (ir=NR_NO) and (br<>NR_ESP) then
  1310. begin
  1311. output.sib_present:=false;
  1312. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1313. end
  1314. else
  1315. begin
  1316. output.sib_present:=true;
  1317. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1318. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1319. end;
  1320. end;
  1321. if output.sib_present then
  1322. output.size:=2+output.bytes
  1323. else
  1324. output.size:=1+output.bytes;
  1325. process_ea:=true;
  1326. end;
  1327. function taicpu.calcsize(p:PInsEntry):longint;
  1328. var
  1329. codes : pchar;
  1330. c : byte;
  1331. len : longint;
  1332. ea_data : ea;
  1333. begin
  1334. len:=0;
  1335. codes:=@p^.code;
  1336. repeat
  1337. c:=ord(codes^);
  1338. inc(codes);
  1339. case c of
  1340. 0 :
  1341. break;
  1342. 1,2,3 :
  1343. begin
  1344. inc(codes,c);
  1345. inc(len,c);
  1346. end;
  1347. 8,9,10 :
  1348. begin
  1349. inc(codes);
  1350. inc(len);
  1351. end;
  1352. 4,5,6,7 :
  1353. begin
  1354. if opsize=S_W then
  1355. inc(len,2)
  1356. else
  1357. inc(len);
  1358. end;
  1359. 15,
  1360. 12,13,14,
  1361. 16,17,18,
  1362. 20,21,22,
  1363. 40,41,42 :
  1364. inc(len);
  1365. 24,25,26,
  1366. 31,
  1367. 48,49,50 :
  1368. inc(len,2);
  1369. 28,29,30, { we don't have 16 bit immediates code }
  1370. 32,33,34,
  1371. 52,53,54,
  1372. 56,57,58 :
  1373. inc(len,4);
  1374. 192,193,194 :
  1375. if NeedAddrPrefix(c-192) then
  1376. inc(len);
  1377. 208,
  1378. 210 :
  1379. inc(len);
  1380. 200,
  1381. 201,
  1382. 202,
  1383. 209,
  1384. 211,
  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(objdata: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. * \312 - indicates fixed 64-bit address size, i.e. optional 0x48.
  1449. * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
  1450. * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
  1451. * \322 - indicates fixed 64-bit operand size, i.e. optional 0x48.
  1452. * \323 - indicates that this instruction is only valid when the
  1453. * operand size is the default (instruction to disassembler,
  1454. * generates no code in the assembler)
  1455. * \330 - a literal byte follows in the code stream, to be added
  1456. * to the condition code value of the instruction.
  1457. * \340 - reserve <operand 0> bytes of uninitialised storage.
  1458. * Operand 0 had better be a segmentless constant.
  1459. }
  1460. var
  1461. currval : longint;
  1462. currsym : tasmsymbol;
  1463. procedure getvalsym(opidx:longint);
  1464. begin
  1465. case oper[opidx]^.typ of
  1466. top_ref :
  1467. begin
  1468. currval:=oper[opidx]^.ref^.offset;
  1469. currsym:=oper[opidx]^.ref^.symbol;
  1470. end;
  1471. top_const :
  1472. begin
  1473. currval:=longint(oper[opidx]^.val);
  1474. currsym:=nil;
  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 objdata.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. objdata.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. objdata.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. objdata.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. objdata.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. objdata.writebytes(bytes,1);
  1557. end;
  1558. 15 :
  1559. begin
  1560. bytes[0]:=0;
  1561. objdata.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. objdata.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1570. else
  1571. objdata.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. objdata.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1580. else
  1581. objdata.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. objdata.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1590. else
  1591. objdata.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. objdata.writereloc(currval,2,currsym,RELOC_ABSOLUTE)
  1600. else
  1601. objdata.writebytes(currval,2);
  1602. end;
  1603. 28,29,30 :
  1604. begin
  1605. getvalsym(c-28);
  1606. if assigned(currsym) then
  1607. objdata.writereloc(currval,4,currsym,RELOC_ABSOLUTE)
  1608. else
  1609. objdata.writebytes(currval,4);
  1610. end;
  1611. 32,33,34 :
  1612. begin
  1613. getvalsym(c-32);
  1614. if assigned(currsym) then
  1615. objdata.writereloc(currval,4,currsym,RELOC_ABSOLUTE)
  1616. else
  1617. objdata.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. objdata.writebytes(data,1);
  1628. end;
  1629. 52,53,54 :
  1630. begin
  1631. getvalsym(c-52);
  1632. if assigned(currsym) then
  1633. objdata.writereloc(currval,4,currsym,RELOC_RELATIVE)
  1634. else
  1635. objdata.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. objdata.writereloc(currval,4,currsym,RELOC_RELATIVE)
  1642. else
  1643. objdata.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. objdata.writebytes(bytes,1);
  1651. end;
  1652. end;
  1653. 200 :
  1654. begin
  1655. bytes[0]:=$67;
  1656. objdata.writebytes(bytes,1);
  1657. end;
  1658. 208 :
  1659. begin
  1660. bytes[0]:=$66;
  1661. objdata.writebytes(bytes,1);
  1662. end;
  1663. 210 :
  1664. begin
  1665. bytes[0]:=$48;
  1666. objdata.writebytes(bytes,1);
  1667. end;
  1668. 216 :
  1669. begin
  1670. bytes[0]:=ord(codes^)+condval[condition];
  1671. inc(codes);
  1672. objdata.writebytes(bytes,1);
  1673. end;
  1674. 201,
  1675. 202,
  1676. 209,
  1677. 211,
  1678. 217,218 :
  1679. begin
  1680. { these are dissambler hints or 32 bit prefixes which
  1681. are not needed }
  1682. end;
  1683. 219 :
  1684. begin
  1685. bytes[0]:=$f3;
  1686. objdata.writebytes(bytes,1);
  1687. end;
  1688. 220 :
  1689. begin
  1690. bytes[0]:=$f2;
  1691. objdata.writebytes(bytes,1);
  1692. end;
  1693. 31,
  1694. 48,49,50,
  1695. 224,225,226 :
  1696. begin
  1697. InternalError(777006);
  1698. end
  1699. else
  1700. begin
  1701. if (c>=64) and (c<=191) then
  1702. begin
  1703. if (c<127) then
  1704. begin
  1705. if (oper[c and 7]^.typ=top_reg) then
  1706. rfield:=regval(oper[c and 7]^.reg)
  1707. else
  1708. rfield:=regval(oper[c and 7]^.ref^.base);
  1709. end
  1710. else
  1711. rfield:=c and 7;
  1712. opidx:=(c shr 3) and 7;
  1713. if not process_ea(oper[opidx]^,ea_data,rfield) then
  1714. Message(asmw_e_invalid_effective_address);
  1715. pb:=@bytes;
  1716. pb^:=chr(ea_data.modrm);
  1717. inc(pb);
  1718. if ea_data.sib_present then
  1719. begin
  1720. pb^:=chr(ea_data.sib);
  1721. inc(pb);
  1722. end;
  1723. s:=pb-pchar(@bytes);
  1724. objdata.writebytes(bytes,s);
  1725. case ea_data.bytes of
  1726. 0 : ;
  1727. 1 :
  1728. begin
  1729. if (oper[opidx]^.ot and OT_MEMORY)=OT_MEMORY then
  1730. objdata.writereloc(oper[opidx]^.ref^.offset,1,oper[opidx]^.ref^.symbol,RELOC_ABSOLUTE)
  1731. else
  1732. begin
  1733. bytes[0]:=oper[opidx]^.ref^.offset;
  1734. objdata.writebytes(bytes,1);
  1735. end;
  1736. inc(s);
  1737. end;
  1738. 2,4 :
  1739. begin
  1740. objdata.writereloc(oper[opidx]^.ref^.offset,ea_data.bytes,
  1741. oper[opidx]^.ref^.symbol,RELOC_ABSOLUTE);
  1742. inc(s,ea_data.bytes);
  1743. end;
  1744. end;
  1745. end
  1746. else
  1747. InternalError(777007);
  1748. end;
  1749. end;
  1750. until false;
  1751. end;
  1752. {$endif NOAG386BIN}
  1753. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  1754. begin
  1755. result:=(((opcode=A_MOV) or (opcode=A_XCHG)) and
  1756. (regtype = R_INTREGISTER) and
  1757. (ops=2) and
  1758. (oper[0]^.typ=top_reg) and
  1759. (oper[1]^.typ=top_reg) and
  1760. (oper[0]^.reg=oper[1]^.reg)
  1761. ) or
  1762. (((opcode=A_MOVSS) or (opcode=A_MOVSD)) and
  1763. (regtype = R_MMREGISTER) and
  1764. (ops=2) and
  1765. (oper[0]^.typ=top_reg) and
  1766. (oper[1]^.typ=top_reg) and
  1767. (oper[0]^.reg=oper[1]^.reg)
  1768. );
  1769. end;
  1770. function spilling_create_load(const ref:treference;r:tregister): tai;
  1771. begin
  1772. internalerror(200406131);
  1773. end;
  1774. function spilling_create_store(r:tregister; const ref:treference): tai;
  1775. begin
  1776. internalerror(200406132);
  1777. end;
  1778. {*****************************************************************************
  1779. Instruction table
  1780. *****************************************************************************}
  1781. procedure BuildInsTabCache;
  1782. {$ifndef NOAG386BIN}
  1783. var
  1784. i : longint;
  1785. {$endif}
  1786. begin
  1787. {$ifndef NOAG386BIN}
  1788. new(instabcache);
  1789. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  1790. i:=0;
  1791. while (i<InsTabEntries) do
  1792. begin
  1793. if InsTabCache^[InsTab[i].OPcode]=-1 then
  1794. InsTabCache^[InsTab[i].OPcode]:=i;
  1795. inc(i);
  1796. end;
  1797. {$endif NOAG386BIN}
  1798. end;
  1799. procedure InitAsm;
  1800. begin
  1801. {$ifndef NOAG386BIN}
  1802. if not assigned(instabcache) then
  1803. BuildInsTabCache;
  1804. {$endif NOAG386BIN}
  1805. end;
  1806. procedure DoneAsm;
  1807. begin
  1808. {$ifndef NOAG386BIN}
  1809. if assigned(instabcache) then
  1810. begin
  1811. dispose(instabcache);
  1812. instabcache:=nil;
  1813. end;
  1814. {$endif NOAG386BIN}
  1815. end;
  1816. begin
  1817. cai_align:=tai_align;
  1818. cai_cpu:=taicpu;
  1819. end.
  1820. {
  1821. $Log$
  1822. Revision 1.58 2004-09-27 15:12:47 peter
  1823. * IE when expecting top_ref
  1824. Revision 1.57 2004/06/20 08:55:32 florian
  1825. * logs truncated
  1826. Revision 1.56 2004/06/16 20:07:11 florian
  1827. * dwarf branch merged
  1828. Revision 1.55.2.6 2004/06/13 10:51:17 florian
  1829. * fixed several register allocator problems (sparc/arm)
  1830. Revision 1.55.2.5 2004/05/02 19:08:01 florian
  1831. * rewrote tcgcallnode.handle_return_value
  1832. Revision 1.55.2.4 2004/05/01 16:02:10 peter
  1833. * POINTER_SIZE replaced with sizeof(aint)
  1834. * aint,aword,tconst*int moved to globtype
  1835. Revision 1.55.2.3 2004/04/27 18:18:26 peter
  1836. * aword -> aint
  1837. }