aasmtai.pas 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647
  1. {
  2. Copyright (c) 1998-2006 by Florian Klaempfl
  3. This unit implements an abstract asmoutput class for all processor types
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. { @abstract(This unit implements an abstract asm output class for all processor types)
  18. This unit implements an abstract assembler output class for all processors, these
  19. are then overridden for each assembler writer to actually write the data in these
  20. classes to an assembler file.
  21. }
  22. unit aasmtai;
  23. {$i fpcdefs.inc}
  24. interface
  25. uses
  26. cutils,cclasses,
  27. globtype,systems,
  28. cpuinfo,cpubase,
  29. cgbase,cgutils,
  30. symtype,
  31. aasmbase,aasmdata,ogbase;
  32. type
  33. { keep the number of elements in this enumeration less or equal than 32 as long
  34. as FPC knows only 4 byte and 32 byte sets (FK) }
  35. taitype = (
  36. ait_none,
  37. ait_align,
  38. ait_section,
  39. ait_comment,
  40. ait_string,
  41. ait_instruction,
  42. ait_datablock,
  43. ait_symbol,
  44. { needed to calc the size of a symbol }
  45. ait_symbol_end,
  46. ait_directive,
  47. ait_label,
  48. ait_const,
  49. ait_real_32bit,
  50. ait_real_64bit,
  51. ait_real_80bit,
  52. ait_comp_64bit,
  53. ait_real_128bit,
  54. ait_stab,
  55. ait_force_line,
  56. ait_function_name,
  57. {$ifdef alpha}
  58. { the follow is for the DEC Alpha }
  59. ait_frame,
  60. ait_ent,
  61. {$endif alpha}
  62. {$ifdef ia64}
  63. ait_bundle,
  64. ait_stop,
  65. {$endif ia64}
  66. {$ifdef m68k}
  67. ait_labeled_instruction,
  68. {$endif m68k}
  69. {$ifdef arm}
  70. ait_thumb_func,
  71. {$endif arm}
  72. { used to split into tiny assembler files }
  73. ait_cutobject,
  74. ait_regalloc,
  75. ait_tempalloc,
  76. { used to mark assembler blocks and inlined functions }
  77. ait_marker,
  78. { used to describe a new location of a variable }
  79. ait_varloc,
  80. { SEH directives used in ARM,MIPS and x86_64 COFF targets }
  81. ait_seh_directive
  82. );
  83. taiconst_type = (
  84. aitconst_128bit,
  85. aitconst_64bit,
  86. aitconst_32bit,
  87. aitconst_16bit,
  88. aitconst_8bit,
  89. aitconst_sleb128bit,
  90. aitconst_uleb128bit,
  91. { win32 only }
  92. aitconst_rva_symbol,
  93. aitconst_secrel32_symbol,
  94. { darwin only }
  95. { From gcc/config/darwin.c (darwin_asm_output_dwarf_delta):
  96. ***
  97. Output a difference of two labels that will be an assembly time
  98. constant if the two labels are local. (.long lab1-lab2 will be
  99. very different if lab1 is at the boundary between two sections; it
  100. will be relocated according to the second section, not the first,
  101. so one ends up with a difference between labels in different
  102. sections, which is bad in the dwarf2 eh context for instance.)
  103. ***
  104. We cannot use this everywhere, because older versions of the
  105. darwin assembler don't support the construct used for these
  106. relsyms (nor do they support dwarf, for that matter)
  107. }
  108. aitconst_darwin_dwarf_delta64,
  109. aitconst_darwin_dwarf_delta32,
  110. { ARM Thumb-2 only }
  111. aitconst_half16bit { used for table jumps. The actual value is the 16bit value shifted left once }
  112. );
  113. const
  114. {$ifdef cpu64bitaddr}
  115. aitconst_ptr = aitconst_64bit;
  116. {$else cpu64bitaddr}
  117. aitconst_ptr = aitconst_32bit;
  118. {$endif cpu64bitaddr}
  119. {$ifdef cpu64bitalu}
  120. aitconst_aint = aitconst_64bit;
  121. {$else cpu64bitaddr}
  122. aitconst_aint = aitconst_32bit;
  123. {$endif cpu64bitaddr}
  124. taitypestr : array[taitype] of string[24] = (
  125. '<none>',
  126. 'align',
  127. 'section',
  128. 'comment',
  129. 'string',
  130. 'instruction',
  131. 'datablock',
  132. 'symbol',
  133. 'symbol_end',
  134. 'symbol_directive',
  135. 'label',
  136. 'const',
  137. 'real_32bit',
  138. 'real_64bit',
  139. 'real_80bit',
  140. 'comp_64bit',
  141. 'real_128bit',
  142. 'stab',
  143. 'force_line',
  144. 'function_name',
  145. {$ifdef alpha}
  146. { the follow is for the DEC Alpha }
  147. 'frame',
  148. 'ent',
  149. {$endif alpha}
  150. {$ifdef ia64}
  151. 'bundle',
  152. 'stop',
  153. {$endif ia64}
  154. {$ifdef m68k}
  155. 'labeled_instr',
  156. {$endif m68k}
  157. {$ifdef arm}
  158. 'thumb_func',
  159. {$endif arm}
  160. 'cut',
  161. 'regalloc',
  162. 'tempalloc',
  163. 'marker',
  164. 'varloc',
  165. 'seh_directive'
  166. );
  167. type
  168. { Types of operand }
  169. toptype=(top_none,top_reg,top_ref,top_const,top_bool,top_local
  170. {$ifdef arm}
  171. { ARM only }
  172. ,top_regset
  173. ,top_shifterop
  174. ,top_conditioncode
  175. ,top_modeflags
  176. {$endif arm}
  177. {$ifdef m68k}
  178. { m68k only }
  179. ,top_regset
  180. {$endif m68k}
  181. { i386 only});
  182. { kinds of operations that an instruction can perform on an operand }
  183. topertype = (operand_read,operand_write,operand_readwrite);
  184. tlocaloper = record
  185. localsym : pointer;
  186. localsymderef : tderef;
  187. localsymofs : longint;
  188. localindexreg : tregister;
  189. localscale : byte;
  190. localgetoffset,
  191. localforceref : boolean
  192. end;
  193. plocaloper = ^tlocaloper;
  194. { please keep the size of this record <=12 bytes and keep it properly aligned }
  195. toper = record
  196. ot : longint;
  197. case typ : toptype of
  198. top_none : ();
  199. top_reg : (reg:tregister);
  200. top_ref : (ref:preference);
  201. top_const : (val:tcgint);
  202. top_bool : (b:boolean);
  203. { local varsym that will be inserted in pass_generate_code }
  204. top_local : (localoper:plocaloper);
  205. {$ifdef arm}
  206. top_regset : (regset:^tcpuregisterset; regtyp: tregistertype; subreg: tsubregister);
  207. top_shifterop : (shifterop : pshifterop);
  208. top_conditioncode : (cc : TAsmCond);
  209. top_modeflags : (modeflags : tcpumodeflags);
  210. {$endif arm}
  211. {$ifdef m68k}
  212. top_regset : (regset:^tcpuregisterset);
  213. {$endif m68k}
  214. end;
  215. poper=^toper;
  216. const
  217. { ait_* types which don't result in executable code or which don't influence
  218. the way the program runs/behaves, but which may be encountered by the
  219. optimizer (= if it's sometimes added to the exprasm list). Update if you add
  220. a new ait type! }
  221. SkipInstr = [ait_comment, ait_symbol,ait_section
  222. ,ait_stab, ait_function_name, ait_force_line
  223. ,ait_regalloc, ait_tempalloc, ait_symbol_end, ait_directive
  224. ,ait_varloc,ait_seh_directive];
  225. { ait_* types which do not have line information (and hence which are of type
  226. tai, otherwise, they are of type tailineinfo }
  227. SkipLineInfo =[ait_label,
  228. ait_regalloc,ait_tempalloc,
  229. ait_stab,ait_function_name,
  230. ait_cutobject,ait_marker,ait_varloc,ait_align,ait_section,ait_comment,
  231. ait_const,
  232. {$ifdef arm}
  233. ait_thumb_func,
  234. {$endif arm}
  235. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_real_128bit,
  236. ait_symbol,
  237. ait_seh_directive
  238. ];
  239. type
  240. { cut type, required for alphanumeric ordering of the assembler filenames }
  241. TCutPlace=(cut_normal,cut_begin,cut_end);
  242. TAsmMarker = (
  243. mark_NoPropInfoStart,mark_NoPropInfoEnd,
  244. mark_AsmBlockStart,mark_AsmBlockEnd,
  245. mark_NoLineInfoStart,mark_NoLineInfoEnd,mark_BlockStart,
  246. mark_Position
  247. );
  248. TRegAllocType = (ra_alloc,ra_dealloc,ra_sync,ra_resize);
  249. TStabType = (stab_stabs,stab_stabn,stab_stabd);
  250. TAsmDirective=(
  251. asd_indirect_symbol,
  252. asd_extern,asd_nasm_import, asd_toc_entry,
  253. asd_reference,asd_no_dead_strip,asd_weak_reference,asd_lazy_reference,
  254. asd_weak_definition
  255. );
  256. TAsmSehDirective=(
  257. ash_proc,ash_endproc,
  258. ash_endprologue,ash_handler,ash_handlerdata,
  259. ash_eh,ash_32,ash_no32,
  260. ash_setframe,ash_stackalloc,ash_pushreg,
  261. ash_savereg,ash_savexmm,ash_pushframe
  262. );
  263. const
  264. regallocstr : array[tregalloctype] of string[10]=('allocated','released','sync','resized');
  265. tempallocstr : array[boolean] of string[10]=('released','allocated');
  266. stabtypestr : array[TStabType] of string[5]=('stabs','stabn','stabd');
  267. directivestr : array[TAsmDirective] of string[23]=(
  268. 'indirect_symbol',
  269. 'extern','nasm_import', 'tc', 'reference',
  270. 'no_dead_strip','weak_reference','lazy_reference','weak_definition'
  271. );
  272. sehdirectivestr : array[TAsmSehDirective] of string[16]=(
  273. '.seh_proc','.seh_endproc',
  274. '.seh_endprologue','.seh_handler','.seh_handlerdata',
  275. '.seh_eh','.seh_32','seh_no32',
  276. '.seh_setframe','.seh_stackalloc','.seh_pushreg',
  277. '.seh_savereg','.seh_savexmm','.seh_pushframe'
  278. );
  279. type
  280. { abstract assembler item }
  281. tai = class(TLinkedListItem)
  282. {$ifndef NOOPT}
  283. { pointer to record with optimizer info about this tai object }
  284. optinfo : pointer;
  285. {$endif NOOPT}
  286. typ : taitype;
  287. constructor Create;
  288. constructor ppuload(t:taitype;ppufile:tcompilerppufile);virtual;
  289. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  290. procedure buildderefimpl;virtual;
  291. procedure derefimpl;virtual;
  292. end;
  293. { abstract assembler item with line information }
  294. tailineinfo = class(tai)
  295. fileinfo : tfileposinfo;
  296. constructor Create;
  297. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  298. procedure ppuwrite(ppufile:tcompilerppufile);override;
  299. end;
  300. tai_simple = class(tai)
  301. constructor create(_typ : taitype);
  302. end;
  303. taiclass = class of tai;
  304. taiclassarray = array[taitype] of taiclass;
  305. { Generates an assembler string }
  306. tai_string = class(tailineinfo)
  307. str : pchar;
  308. { extra len so the string can contain an \0 }
  309. len : longint;
  310. constructor Create(const _str : string);
  311. constructor Create_pchar(_str : pchar;length : longint);
  312. destructor Destroy;override;
  313. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  314. procedure ppuwrite(ppufile:tcompilerppufile);override;
  315. function getcopy:tlinkedlistitem;override;
  316. end;
  317. { Generates a common label }
  318. tai_symbol = class(tai)
  319. sym : tasmsymbol;
  320. value : puint;
  321. size : longint;
  322. is_global,
  323. has_value : boolean;
  324. constructor Create(_sym:tasmsymbol;siz:longint);
  325. constructor Create_Global(_sym:tasmsymbol;siz:longint);
  326. constructor Createname(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  327. constructor Createname_global(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  328. constructor Createname_global_value(const _name : string;_symtyp:Tasmsymtype;siz:longint;val:ptruint);
  329. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  330. procedure ppuwrite(ppufile:tcompilerppufile);override;
  331. procedure derefimpl;override;
  332. end;
  333. tai_symbol_end = class(tailineinfo)
  334. sym : tasmsymbol;
  335. constructor Create(_sym:tasmsymbol);
  336. constructor Createname(const _name : string);
  337. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  338. procedure ppuwrite(ppufile:tcompilerppufile);override;
  339. procedure derefimpl;override;
  340. end;
  341. tai_directive = class(tailineinfo)
  342. name : ansistring;
  343. directive : TAsmDirective;
  344. constructor Create(_directive:TAsmDirective;const _name:ansistring);
  345. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  346. procedure ppuwrite(ppufile:tcompilerppufile);override;
  347. end;
  348. { Generates an assembler label }
  349. tai_label = class(tai)
  350. is_global : boolean;
  351. labsym : tasmlabel;
  352. constructor Create(_labsym : tasmlabel);
  353. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  354. procedure ppuwrite(ppufile:tcompilerppufile);override;
  355. procedure derefimpl;override;
  356. end;
  357. { Generates an assembler comment }
  358. tai_comment = class(tai)
  359. str : pchar;
  360. constructor Create(_str : pchar);
  361. destructor Destroy; override;
  362. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  363. procedure ppuwrite(ppufile:tcompilerppufile);override;
  364. function getcopy:tlinkedlistitem;override;
  365. end;
  366. { Generates a section / segment directive }
  367. tai_section = class(tai)
  368. sectype : TAsmSectiontype;
  369. secorder : TasmSectionorder;
  370. secalign : byte;
  371. name : pshortstring;
  372. sec : TObjSection; { used in binary writer }
  373. destructor Destroy;override;
  374. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  375. procedure ppuwrite(ppufile:tcompilerppufile);override;
  376. {$push}{$warnings off}
  377. private
  378. { this constructor is made private on purpose }
  379. { because sections should be created via new_section() }
  380. constructor Create(Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  381. {$pop}
  382. end;
  383. { Generates an uninitializised data block }
  384. tai_datablock = class(tailineinfo)
  385. is_global : boolean;
  386. sym : tasmsymbol;
  387. size : asizeint;
  388. constructor Create(const _name : string;_size : aint);
  389. constructor Create_global(const _name : string;_size : asizeint);
  390. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  391. procedure ppuwrite(ppufile:tcompilerppufile);override;
  392. procedure derefimpl;override;
  393. end;
  394. { Generates an integer const }
  395. tai_const = class(tai)
  396. sym,
  397. endsym : tasmsymbol;
  398. { if symbols and offset are provided the symofs is used,
  399. the value is calculated during assembling }
  400. symofs,
  401. value : int64;
  402. consttype : taiconst_type;
  403. { we use for the 128bit int64/qword for now because I can't imagine a
  404. case where we need 128 bit now (FK) }
  405. constructor Create(_typ:taiconst_type;_value : int64);
  406. constructor Create_128bit(_value : int64);
  407. constructor Create_64bit(_value : int64);
  408. constructor Create_32bit(_value : longint);
  409. constructor Create_16bit(_value : word);
  410. constructor Create_8bit(_value : byte);
  411. constructor Create_char(size: integer; _value: dword);
  412. constructor Create_sleb128bit(_value : int64);
  413. constructor Create_uleb128bit(_value : qword);
  414. constructor Create_aint(_value : aint);
  415. constructor Create_pint(_value : pint);
  416. constructor Create_sym(_sym:tasmsymbol);
  417. constructor Create_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  418. constructor Create_sym_offset(_sym:tasmsymbol;ofs:aint);
  419. constructor Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  420. constructor Create_rva_sym(_sym:tasmsymbol);
  421. constructor Createname(const name:string;ofs:aint);
  422. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  423. procedure ppuwrite(ppufile:tcompilerppufile);override;
  424. procedure derefimpl;override;
  425. function getcopy:tlinkedlistitem;override;
  426. function size:longint;
  427. end;
  428. { Generates a single float (32 bit real) }
  429. tai_real_32bit = class(tai)
  430. value : ts32real;
  431. constructor Create(_value : ts32real);
  432. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  433. procedure ppuwrite(ppufile:tcompilerppufile);override;
  434. end;
  435. tformatoptions = (fo_none,fo_hiloswapped);
  436. { Generates a double float (64 bit real) }
  437. tai_real_64bit = class(tai)
  438. value : ts64real;
  439. {$ifdef ARM}
  440. formatoptions : tformatoptions;
  441. constructor Create_hiloswapped(_value : ts64real);
  442. {$endif ARM}
  443. constructor Create(_value : ts64real);
  444. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  445. procedure ppuwrite(ppufile:tcompilerppufile);override;
  446. end;
  447. { Generates an extended float (80 bit real) }
  448. tai_real_80bit = class(tai)
  449. value : ts80real;
  450. savesize : byte;
  451. constructor Create(_value : ts80real; _savesize: byte);
  452. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  453. procedure ppuwrite(ppufile:tcompilerppufile);override;
  454. end;
  455. { Generates an float128 (128 bit real) }
  456. tai_real_128bit = class(tai)
  457. value : ts128real;
  458. constructor Create(_value : ts128real);
  459. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  460. procedure ppuwrite(ppufile:tcompilerppufile);override;
  461. end;
  462. { Generates a comp int (integer over 64 bits)
  463. This is Intel 80x86 specific, and is not
  464. really supported on other processors.
  465. }
  466. tai_comp_64bit = class(tai)
  467. value : ts64comp;
  468. constructor Create(_value : ts64comp);
  469. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  470. procedure ppuwrite(ppufile:tcompilerppufile);override;
  471. end;
  472. { tai_stab }
  473. tai_stab = class(tai)
  474. str : pchar;
  475. stabtype : TStabType;
  476. constructor Create(_stabtype:TStabType;_str : pchar);
  477. constructor Create_str(_stabtype:TStabType;const s:string);
  478. constructor create_ansistr(_stabtype: TStabType; const s: ansistring);
  479. destructor Destroy;override;
  480. end;
  481. tai_force_line = class(tailineinfo)
  482. constructor Create;
  483. end;
  484. tai_function_name = class(tai)
  485. funcname : pshortstring;
  486. constructor create(const s:string);
  487. destructor destroy;override;
  488. end;
  489. { Insert a cut to split assembler into several smaller files }
  490. tai_cutobject = class(tai)
  491. place : tcutplace;
  492. constructor Create;
  493. constructor Create_begin;
  494. constructor Create_end;
  495. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  496. procedure ppuwrite(ppufile:tcompilerppufile);override;
  497. end;
  498. { Insert a marker for assembler and inline blocks }
  499. tai_marker = class(tai)
  500. Kind: TAsmMarker;
  501. Constructor Create(_Kind: TAsmMarker);
  502. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  503. procedure ppuwrite(ppufile:tcompilerppufile);override;
  504. end;
  505. tai_tempalloc = class(tai)
  506. allocation : boolean;
  507. {$ifdef EXTDEBUG}
  508. problem : pshortstring;
  509. {$endif EXTDEBUG}
  510. temppos,
  511. tempsize : longint;
  512. constructor alloc(pos,size:longint);
  513. constructor dealloc(pos,size:longint);
  514. {$ifdef EXTDEBUG}
  515. constructor allocinfo(pos,size:longint;const st:string);
  516. {$endif EXTDEBUG}
  517. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  518. destructor destroy;override;
  519. procedure ppuwrite(ppufile:tcompilerppufile);override;
  520. end;
  521. tai_regalloc = class(tai)
  522. reg : tregister;
  523. ratype : TRegAllocType;
  524. { reg(de)alloc belongs to this instruction, this
  525. is only used for automatic inserted (de)alloc for
  526. imaginary register and required for spilling code }
  527. instr : tai;
  528. constructor alloc(r : tregister;ainstr:tai);
  529. constructor dealloc(r : tregister;ainstr:tai);
  530. constructor sync(r : tregister);
  531. constructor resize(r : tregister);
  532. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  533. procedure ppuwrite(ppufile:tcompilerppufile);override;
  534. end;
  535. tadd_reg_instruction_proc=procedure(instr:Tai;r:tregister) of object;
  536. { Class template for assembler instructions
  537. }
  538. tai_cpu_abstract = class(tailineinfo)
  539. protected
  540. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);virtual;
  541. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);virtual;
  542. procedure ppubuildderefimploper(var o:toper);virtual;abstract;
  543. procedure ppuderefoper(var o:toper);virtual;abstract;
  544. public
  545. { Condition flags for instruction }
  546. condition : TAsmCond;
  547. { Number of operands to instruction }
  548. ops : byte;
  549. { Number of allocate oper structures }
  550. opercnt : byte;
  551. { Operands of instruction }
  552. oper : array[0..max_operands-1] of poper;
  553. { Actual opcode of instruction }
  554. opcode : tasmop;
  555. {$ifdef x86}
  556. segprefix : tregister;
  557. {$endif x86}
  558. { true if instruction is a jmp }
  559. is_jmp : boolean; { is this instruction a jump? (needed for optimizer) }
  560. Constructor Create(op : tasmop);virtual;
  561. Destructor Destroy;override;
  562. function getcopy:TLinkedListItem;override;
  563. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  564. procedure ppuwrite(ppufile:tcompilerppufile);override;
  565. procedure buildderefimpl;override;
  566. procedure derefimpl;override;
  567. procedure SetCondition(const c:TAsmCond);
  568. procedure allocate_oper(opers:longint);
  569. procedure loadconst(opidx:longint;l:aint);
  570. procedure loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  571. procedure loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset,forceref:boolean);
  572. procedure loadref(opidx:longint;const r:treference);
  573. procedure loadreg(opidx:longint;r:tregister);
  574. procedure loadoper(opidx:longint;o:toper);
  575. procedure clearop(opidx:longint);
  576. procedure freeop(opidx:longint);
  577. { register allocator }
  578. function is_same_reg_move(regtype: Tregistertype):boolean;virtual;
  579. function spilling_get_operation_type(opnr: longint): topertype;virtual;
  580. function spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;virtual;
  581. function Pass1(objdata:TObjData):longint;virtual;
  582. procedure Pass2(objdata:TObjData);virtual;
  583. procedure resetpass1; virtual;
  584. procedure resetpass2; virtual;
  585. end;
  586. tai_cpu_class = class of tai_cpu_abstract;
  587. { Buffer type used for alignment }
  588. tfillbuffer = array[0..63] of char;
  589. { alignment for operator }
  590. tai_align_abstract = class(tai)
  591. aligntype : byte; { 1 = no align, 2 = word align, 4 = dword align }
  592. fillsize : byte; { real size to fill }
  593. fillop : byte; { value to fill with - optional }
  594. use_op : boolean;
  595. constructor Create(b:byte);virtual;
  596. constructor Create_op(b: byte; _op: byte);virtual;
  597. constructor Create_zeros(b:byte);
  598. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  599. procedure ppuwrite(ppufile:tcompilerppufile);override;
  600. function calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;virtual;
  601. end;
  602. tai_align_class = class of tai_align_abstract;
  603. tai_varloc = class(tai)
  604. newlocation,
  605. newlocationhi : tregister;
  606. varsym : tsym;
  607. constructor create(sym : tsym;loc : tregister);
  608. constructor create64(sym : tsym;loc,lochi : tregister);
  609. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  610. procedure ppuwrite(ppufile:tcompilerppufile);override;
  611. procedure buildderefimpl;override;
  612. procedure derefimpl;override;
  613. end;
  614. TSehDirectiveDatatype=(sd_none,sd_string,sd_reg,sd_offset,sd_regoffset);
  615. TSehDirectiveData=record
  616. case typ: TSehDirectiveDatatype of
  617. sd_none: ();
  618. sd_string: (name:pshortstring;flags:byte);
  619. sd_reg,sd_offset,sd_regoffset: (reg:TRegister;offset:dword);
  620. end;
  621. tai_seh_directive = class(tai)
  622. kind: TAsmSehDirective;
  623. data: TSehDirectiveData;
  624. constructor create(_kind:TAsmSehDirective);
  625. constructor create_name(_kind:TAsmSehDirective;const _name: string);
  626. constructor create_reg(_kind:TAsmSehDirective;r:TRegister);
  627. constructor create_offset(_kind:TAsmSehDirective;ofs:dword);
  628. constructor create_reg_offset(_kind:TAsmSehDirective;r:TRegister;ofs:dword);
  629. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  630. destructor destroy;override;
  631. procedure ppuwrite(ppufile:tcompilerppufile);override;
  632. procedure generate_code(objdata:TObjData);virtual;
  633. property datatype: TSehDirectiveDatatype read data.typ;
  634. end;
  635. tai_seh_directive_class=class of tai_seh_directive;
  636. var
  637. { array with all class types for tais }
  638. aiclass : taiclassarray;
  639. { target specific tais, possibly overwritten in target specific aasmcpu }
  640. cai_align : tai_align_class = tai_align_abstract;
  641. cai_cpu : tai_cpu_class = tai_cpu_abstract;
  642. cai_seh_directive: tai_seh_directive_class = tai_seh_directive;
  643. { hook to notify uses of registers }
  644. add_reg_instruction_hook : tadd_reg_instruction_proc;
  645. procedure maybe_new_object_file(list:TAsmList);
  646. procedure new_section(list:TAsmList;Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  647. procedure section_symbol_start(list:TAsmList;const Aname:string;Asymtyp:Tasmsymtype;
  648. Aglobal:boolean;Asectype:TAsmSectiontype;Aalign:byte);
  649. procedure section_symbol_end(list:TAsmList;const Aname:string);
  650. function ppuloadai(ppufile:tcompilerppufile):tai;
  651. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  652. implementation
  653. uses
  654. SysUtils,
  655. verbose,
  656. globals,
  657. fmodule;
  658. const
  659. pputaimarker = 254;
  660. {****************************************************************************
  661. Helpers
  662. ****************************************************************************}
  663. procedure maybe_new_object_file(list:TAsmList);
  664. begin
  665. if create_smartlink_library then
  666. list.concat(tai_cutobject.create);
  667. end;
  668. procedure new_section(list:TAsmList;Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  669. begin
  670. list.concat(tai_section.create(Asectype,Aname,Aalign,Asecorder));
  671. list.concat(cai_align.create(Aalign));
  672. end;
  673. procedure section_symbol_start(list:TAsmList;const Aname:string;Asymtyp:Tasmsymtype;
  674. Aglobal:boolean;Asectype:TAsmSectiontype;Aalign:byte);
  675. begin
  676. maybe_new_object_file(list);
  677. new_section(list,Asectype,Aname,Aalign);
  678. if Aglobal or
  679. create_smartlink then
  680. list.concat(tai_symbol.createname_global(Aname,Asymtyp,0))
  681. else
  682. list.concat(tai_symbol.createname(Aname,Asymtyp,0));
  683. end;
  684. procedure section_symbol_end(list:TAsmList;const Aname:string);
  685. begin
  686. list.concat(tai_symbol_end.createname(Aname));
  687. end;
  688. function ppuloadai(ppufile:tcompilerppufile):tai;
  689. var
  690. b : byte;
  691. t : taitype;
  692. begin
  693. { marker }
  694. b:=ppufile.getbyte;
  695. if b<>pputaimarker then
  696. internalerror(200208181);
  697. { load nodetype }
  698. t:=taitype(ppufile.getbyte);
  699. if t<>ait_none then
  700. begin
  701. if t>high(taitype) then
  702. internalerror(200208182);
  703. if not assigned(aiclass[t]) then
  704. internalerror(200208183);
  705. {writeln('taiload: ',taitypestr[t]);}
  706. { generate tai of the correct class }
  707. ppuloadai:=aiclass[t].ppuload(t,ppufile);
  708. end
  709. else
  710. ppuloadai:=nil;
  711. end;
  712. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  713. begin
  714. { marker, read by ppuloadnode }
  715. ppufile.putbyte(pputaimarker);
  716. if assigned(n) then
  717. begin
  718. { type, read by ppuloadnode }
  719. ppufile.putbyte(byte(n.typ));
  720. {writeln('taiwrite: ',taitypestr[n.typ]);}
  721. n.ppuwrite(ppufile);
  722. end
  723. else
  724. ppufile.putbyte(byte(ait_none));
  725. end;
  726. constructor tai_varloc.create(sym: tsym; loc: tregister);
  727. begin
  728. inherited Create;
  729. typ:=ait_varloc;
  730. newlocation:=loc;
  731. newlocationhi:=NR_NO;
  732. varsym:=sym;
  733. end;
  734. constructor tai_varloc.create64(sym: tsym; loc: tregister;lochi : tregister);
  735. begin
  736. inherited Create;
  737. typ:=ait_varloc;
  738. newlocation:=loc;
  739. newlocationhi:=lochi;
  740. varsym:=sym;
  741. end;
  742. constructor tai_varloc.ppuload(t: taitype; ppufile: tcompilerppufile);
  743. begin
  744. inherited ppuload(t, ppufile);
  745. end;
  746. procedure tai_varloc.ppuwrite(ppufile: tcompilerppufile);
  747. begin
  748. inherited ppuwrite(ppufile);
  749. end;
  750. procedure tai_varloc.buildderefimpl;
  751. begin
  752. inherited buildderefimpl;
  753. end;
  754. procedure tai_varloc.derefimpl;
  755. begin
  756. inherited derefimpl;
  757. end;
  758. {****************************************************************************
  759. TAI
  760. ****************************************************************************}
  761. constructor tai.Create;
  762. begin
  763. {$ifndef NOOPT}
  764. optinfo:=nil;
  765. {$endif NOOPT}
  766. end;
  767. constructor tai.ppuload(t:taitype;ppufile:tcompilerppufile);
  768. begin
  769. typ:=t;
  770. {$ifndef NOOPT}
  771. optinfo:=nil;
  772. {$endif}
  773. end;
  774. procedure tai.ppuwrite(ppufile:tcompilerppufile);
  775. begin
  776. end;
  777. procedure tai.buildderefimpl;
  778. begin
  779. end;
  780. procedure tai.derefimpl;
  781. begin
  782. end;
  783. {****************************************************************************
  784. TAILINEINFO
  785. ****************************************************************************}
  786. constructor tailineinfo.create;
  787. begin
  788. inherited create;
  789. fileinfo:=current_filepos;
  790. end;
  791. constructor tailineinfo.ppuload(t:taitype;ppufile:tcompilerppufile);
  792. begin
  793. inherited ppuload(t,ppufile);
  794. ppufile.getposinfo(fileinfo);
  795. end;
  796. procedure tailineinfo.ppuwrite(ppufile:tcompilerppufile);
  797. begin
  798. inherited ppuwrite(ppufile);
  799. ppufile.putposinfo(fileinfo);
  800. end;
  801. {****************************************************************************
  802. TAI_SIMPLE
  803. ****************************************************************************}
  804. constructor tai_simple.create(_typ : taitype);
  805. begin
  806. inherited create;
  807. typ:=_typ;
  808. end;
  809. {****************************************************************************
  810. TAI_SECTION
  811. ****************************************************************************}
  812. constructor tai_section.Create(Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  813. begin
  814. inherited Create;
  815. typ:=ait_section;
  816. sectype:=asectype;
  817. secalign:=Aalign;
  818. secorder:=Asecorder;
  819. name:=stringdup(Aname);
  820. sec:=nil;
  821. end;
  822. constructor tai_section.ppuload(t:taitype;ppufile:tcompilerppufile);
  823. begin
  824. inherited ppuload(t,ppufile);
  825. sectype:=TAsmSectiontype(ppufile.getbyte);
  826. secalign:=ppufile.getbyte;
  827. name:=stringdup(ppufile.getstring);
  828. sec:=nil;
  829. end;
  830. destructor tai_section.Destroy;
  831. begin
  832. stringdispose(name);
  833. end;
  834. procedure tai_section.ppuwrite(ppufile:tcompilerppufile);
  835. begin
  836. inherited ppuwrite(ppufile);
  837. ppufile.putbyte(byte(sectype));
  838. ppufile.putbyte(secalign);
  839. ppufile.putstring(name^);
  840. end;
  841. {****************************************************************************
  842. TAI_DATABLOCK
  843. ****************************************************************************}
  844. constructor tai_datablock.Create(const _name : string;_size : aint);
  845. begin
  846. inherited Create;
  847. typ:=ait_datablock;
  848. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,AT_DATA);
  849. { keep things aligned }
  850. if _size<=0 then
  851. _size:=sizeof(aint);
  852. size:=_size;
  853. is_global:=false;
  854. end;
  855. constructor tai_datablock.Create_global(const _name : string;_size : asizeint);
  856. begin
  857. inherited Create;
  858. typ:=ait_datablock;
  859. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,AT_DATA);
  860. { keep things aligned }
  861. if _size<=0 then
  862. _size:=sizeof(aint);
  863. size:=_size;
  864. is_global:=true;
  865. end;
  866. constructor tai_datablock.ppuload(t:taitype;ppufile:tcompilerppufile);
  867. begin
  868. inherited Create;
  869. sym:=ppufile.getasmsymbol;
  870. size:=ppufile.getaint;
  871. is_global:=boolean(ppufile.getbyte);
  872. end;
  873. procedure tai_datablock.ppuwrite(ppufile:tcompilerppufile);
  874. begin
  875. inherited ppuwrite(ppufile);
  876. ppufile.putasmsymbol(sym);
  877. ppufile.putaint(size);
  878. ppufile.putbyte(byte(is_global));
  879. end;
  880. procedure tai_datablock.derefimpl;
  881. begin
  882. end;
  883. {****************************************************************************
  884. TAI_SYMBOL
  885. ****************************************************************************}
  886. constructor tai_symbol.Create(_sym:tasmsymbol;siz:longint);
  887. begin
  888. inherited Create;
  889. typ:=ait_symbol;
  890. sym:=_sym;
  891. size:=siz;
  892. sym.bind:=AB_LOCAL;
  893. is_global:=false;
  894. end;
  895. constructor tai_symbol.Create_global(_sym:tasmsymbol;siz:longint);
  896. begin
  897. inherited Create;
  898. typ:=ait_symbol;
  899. sym:=_sym;
  900. size:=siz;
  901. { don't override PRIVATE_EXTERN with GLOBAL }
  902. if not(sym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN]) then
  903. sym.bind:=AB_GLOBAL;
  904. is_global:=true;
  905. end;
  906. constructor tai_symbol.Createname(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  907. begin
  908. inherited Create;
  909. typ:=ait_symbol;
  910. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,_symtyp);
  911. size:=siz;
  912. is_global:=false;
  913. end;
  914. constructor tai_symbol.Createname_global(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  915. begin
  916. inherited Create;
  917. typ:=ait_symbol;
  918. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,_symtyp);
  919. size:=siz;
  920. is_global:=true;
  921. end;
  922. constructor tai_symbol.createname_global_value(const _name: string;_symtyp: tasmsymtype; siz: longint; val: ptruint);
  923. begin
  924. Createname_global(_name,_symtyp,siz);
  925. value:=val;
  926. has_value:=true;
  927. end;
  928. constructor tai_symbol.ppuload(t:taitype;ppufile:tcompilerppufile);
  929. begin
  930. inherited ppuload(t,ppufile);
  931. sym:=ppufile.getasmsymbol;
  932. size:=ppufile.getlongint;
  933. is_global:=boolean(ppufile.getbyte);
  934. end;
  935. procedure tai_symbol.ppuwrite(ppufile:tcompilerppufile);
  936. begin
  937. inherited ppuwrite(ppufile);
  938. ppufile.putasmsymbol(sym);
  939. ppufile.putlongint(size);
  940. ppufile.putbyte(byte(is_global));
  941. end;
  942. procedure tai_symbol.derefimpl;
  943. begin
  944. end;
  945. {****************************************************************************
  946. TAI_SYMBOL_END
  947. ****************************************************************************}
  948. constructor tai_symbol_end.Create(_sym:tasmsymbol);
  949. begin
  950. inherited Create;
  951. typ:=ait_symbol_end;
  952. sym:=_sym;
  953. end;
  954. constructor tai_symbol_end.Createname(const _name : string);
  955. begin
  956. inherited Create;
  957. typ:=ait_symbol_end;
  958. sym:=current_asmdata.RefAsmSymbol(_name);
  959. end;
  960. constructor tai_symbol_end.ppuload(t:taitype;ppufile:tcompilerppufile);
  961. begin
  962. inherited ppuload(t,ppufile);
  963. sym:=ppufile.getasmsymbol;
  964. end;
  965. procedure tai_symbol_end.ppuwrite(ppufile:tcompilerppufile);
  966. begin
  967. inherited ppuwrite(ppufile);
  968. ppufile.putasmsymbol(sym);
  969. end;
  970. procedure tai_symbol_end.derefimpl;
  971. begin
  972. end;
  973. {****************************************************************************
  974. TAI_SYMBOL_END
  975. ****************************************************************************}
  976. constructor tai_directive.Create(_directive:TAsmDirective;const _name:ansistring);
  977. begin
  978. inherited Create;
  979. typ:=ait_directive;
  980. name:=_name;
  981. directive:=_directive;
  982. end;
  983. constructor tai_directive.ppuload(t:taitype;ppufile:tcompilerppufile);
  984. begin
  985. inherited ppuload(t,ppufile);
  986. name:=ppufile.getansistring;
  987. directive:=TAsmDirective(ppufile.getbyte);
  988. end;
  989. procedure tai_directive.ppuwrite(ppufile:tcompilerppufile);
  990. begin
  991. inherited ppuwrite(ppufile);
  992. ppufile.putansistring(name);
  993. ppufile.putbyte(byte(directive));
  994. end;
  995. {****************************************************************************
  996. TAI_CONST
  997. ****************************************************************************}
  998. constructor tai_const.Create(_typ:taiconst_type;_value : int64);
  999. begin
  1000. inherited Create;
  1001. typ:=ait_const;
  1002. consttype:=_typ;
  1003. value:=_value;
  1004. sym:=nil;
  1005. endsym:=nil;
  1006. end;
  1007. constructor tai_const.Create_128bit(_value : int64);
  1008. begin
  1009. inherited Create;
  1010. typ:=ait_const;
  1011. consttype:=aitconst_128bit;
  1012. value:=_value;
  1013. sym:=nil;
  1014. endsym:=nil;
  1015. end;
  1016. constructor tai_const.Create_64bit(_value : int64);
  1017. begin
  1018. inherited Create;
  1019. typ:=ait_const;
  1020. consttype:=aitconst_64bit;
  1021. value:=_value;
  1022. sym:=nil;
  1023. endsym:=nil;
  1024. end;
  1025. constructor tai_const.Create_32bit(_value : longint);
  1026. begin
  1027. inherited Create;
  1028. typ:=ait_const;
  1029. consttype:=aitconst_32bit;
  1030. value:=_value;
  1031. sym:=nil;
  1032. endsym:=nil;
  1033. end;
  1034. constructor tai_const.Create_16bit(_value : word);
  1035. begin
  1036. inherited Create;
  1037. typ:=ait_const;
  1038. consttype:=aitconst_16bit;
  1039. value:=_value;
  1040. sym:=nil;
  1041. endsym:=nil;
  1042. end;
  1043. constructor tai_const.Create_8bit(_value : byte);
  1044. begin
  1045. inherited Create;
  1046. typ:=ait_const;
  1047. consttype:=aitconst_8bit;
  1048. value:=_value;
  1049. sym:=nil;
  1050. endsym:=nil;
  1051. end;
  1052. constructor tai_const.Create_char(size: integer; _value: dword);
  1053. begin
  1054. inherited Create;
  1055. typ:=ait_const;
  1056. case size of
  1057. 1:
  1058. begin
  1059. consttype:=aitconst_8bit;
  1060. value:=byte(_value)
  1061. end;
  1062. 2:
  1063. begin
  1064. consttype:=aitconst_16bit;
  1065. value:=word(_value)
  1066. end
  1067. else
  1068. InternalError(2010030701)
  1069. end
  1070. end;
  1071. constructor tai_const.Create_sleb128bit(_value : int64);
  1072. begin
  1073. inherited Create;
  1074. typ:=ait_const;
  1075. consttype:=aitconst_sleb128bit;
  1076. value:=_value;
  1077. sym:=nil;
  1078. endsym:=nil;
  1079. end;
  1080. constructor tai_const.Create_uleb128bit(_value : qword);
  1081. begin
  1082. inherited Create;
  1083. typ:=ait_const;
  1084. consttype:=aitconst_uleb128bit;
  1085. value:=int64(_value);
  1086. sym:=nil;
  1087. endsym:=nil;
  1088. end;
  1089. constructor tai_const.Create_aint(_value : aint);
  1090. begin
  1091. inherited Create;
  1092. typ:=ait_const;
  1093. consttype:=aitconst_aint;
  1094. value:=_value;
  1095. sym:=nil;
  1096. endsym:=nil;
  1097. end;
  1098. constructor tai_const.Create_pint(_value : pint);
  1099. begin
  1100. inherited Create;
  1101. typ:=ait_const;
  1102. consttype:=aitconst_ptr;
  1103. value:=_value;
  1104. sym:=nil;
  1105. endsym:=nil;
  1106. end;
  1107. constructor tai_const.Create_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  1108. begin
  1109. inherited Create;
  1110. typ:=ait_const;
  1111. consttype:=_typ;
  1112. sym:=_sym;
  1113. endsym:=nil;
  1114. value:=0;
  1115. { update sym info }
  1116. if assigned(sym) then
  1117. sym.increfs;
  1118. end;
  1119. constructor tai_const.Create_sym(_sym:tasmsymbol);
  1120. begin
  1121. self.create_sym_offset(_sym,0);
  1122. end;
  1123. constructor tai_const.Create_sym_offset(_sym:tasmsymbol;ofs:aint);
  1124. begin
  1125. inherited Create;
  1126. typ:=ait_const;
  1127. consttype:=aitconst_ptr;
  1128. { sym is allowed to be nil, this is used to write nil pointers }
  1129. sym:=_sym;
  1130. endsym:=nil;
  1131. { store the original offset in symofs so that we can recalculate the
  1132. value field in the assembler }
  1133. symofs:=ofs;
  1134. value:=ofs;
  1135. { update sym info }
  1136. if assigned(sym) then
  1137. sym.increfs;
  1138. end;
  1139. constructor tai_const.Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  1140. begin
  1141. self.create_sym_offset(_sym,0);
  1142. consttype:=_typ;
  1143. endsym:=_endsym;
  1144. endsym.increfs;
  1145. end;
  1146. constructor tai_const.Create_rva_sym(_sym:tasmsymbol);
  1147. begin
  1148. self.create_sym_offset(_sym,0);
  1149. consttype:=aitconst_rva_symbol;
  1150. end;
  1151. constructor tai_const.Createname(const name:string;ofs:aint);
  1152. begin
  1153. self.create_sym_offset(current_asmdata.RefAsmSymbol(name),ofs);
  1154. end;
  1155. constructor tai_const.ppuload(t:taitype;ppufile:tcompilerppufile);
  1156. begin
  1157. inherited ppuload(t,ppufile);
  1158. consttype:=taiconst_type(ppufile.getbyte);
  1159. sym:=ppufile.getasmsymbol;
  1160. endsym:=ppufile.getasmsymbol;
  1161. value:=ppufile.getint64;
  1162. end;
  1163. procedure tai_const.ppuwrite(ppufile:tcompilerppufile);
  1164. begin
  1165. inherited ppuwrite(ppufile);
  1166. ppufile.putbyte(byte(consttype));
  1167. ppufile.putasmsymbol(sym);
  1168. ppufile.putasmsymbol(endsym);
  1169. ppufile.putint64(value);
  1170. end;
  1171. procedure tai_const.derefimpl;
  1172. begin
  1173. end;
  1174. function tai_const.getcopy:tlinkedlistitem;
  1175. begin
  1176. getcopy:=inherited getcopy;
  1177. { we need to increase the reference number }
  1178. sym.increfs;
  1179. if assigned(endsym) then
  1180. endsym.increfs;
  1181. end;
  1182. function tai_const.size:longint;
  1183. begin
  1184. case consttype of
  1185. aitconst_8bit :
  1186. result:=1;
  1187. aitconst_16bit :
  1188. result:=2;
  1189. aitconst_32bit,aitconst_darwin_dwarf_delta32:
  1190. result:=4;
  1191. aitconst_64bit,aitconst_darwin_dwarf_delta64:
  1192. result:=8;
  1193. aitconst_secrel32_symbol,
  1194. aitconst_rva_symbol :
  1195. if target_info.system=system_x86_64_win64 then
  1196. result:=sizeof(longint)
  1197. else
  1198. result:=sizeof(pint);
  1199. aitconst_uleb128bit :
  1200. result:=LengthUleb128(qword(value));
  1201. aitconst_sleb128bit :
  1202. result:=LengthSleb128(value);
  1203. aitconst_half16bit:
  1204. result:=2;
  1205. else
  1206. internalerror(200603253);
  1207. end;
  1208. end;
  1209. {****************************************************************************
  1210. TAI_real_32bit
  1211. ****************************************************************************}
  1212. constructor tai_real_32bit.Create(_value : ts32real);
  1213. begin
  1214. inherited Create;
  1215. typ:=ait_real_32bit;
  1216. value:=_value;
  1217. end;
  1218. constructor tai_real_32bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1219. begin
  1220. inherited ppuload(t,ppufile);
  1221. value:=ppufile.getreal;
  1222. end;
  1223. procedure tai_real_32bit.ppuwrite(ppufile:tcompilerppufile);
  1224. begin
  1225. inherited ppuwrite(ppufile);
  1226. ppufile.putreal(value);
  1227. end;
  1228. {****************************************************************************
  1229. TAI_real_64bit
  1230. ****************************************************************************}
  1231. constructor tai_real_64bit.Create(_value : ts64real);
  1232. begin
  1233. inherited Create;
  1234. typ:=ait_real_64bit;
  1235. value:=_value;
  1236. end;
  1237. {$ifdef ARM}
  1238. constructor tai_real_64bit.Create_hiloswapped(_value : ts64real);
  1239. begin
  1240. inherited Create;
  1241. typ:=ait_real_64bit;
  1242. value:=_value;
  1243. formatoptions:=fo_hiloswapped;
  1244. end;
  1245. {$endif ARM}
  1246. constructor tai_real_64bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1247. begin
  1248. inherited ppuload(t,ppufile);
  1249. value:=ppufile.getreal;
  1250. {$ifdef ARM}
  1251. formatoptions:=tformatoptions(ppufile.getbyte);
  1252. {$endif ARM}
  1253. end;
  1254. procedure tai_real_64bit.ppuwrite(ppufile:tcompilerppufile);
  1255. begin
  1256. inherited ppuwrite(ppufile);
  1257. ppufile.putreal(value);
  1258. {$ifdef ARM}
  1259. ppufile.putbyte(byte(formatoptions));
  1260. {$endif ARM}
  1261. end;
  1262. {****************************************************************************
  1263. TAI_real_80bit
  1264. ****************************************************************************}
  1265. constructor tai_real_80bit.Create(_value : ts80real; _savesize: byte);
  1266. begin
  1267. inherited Create;
  1268. typ:=ait_real_80bit;
  1269. value:=_value;
  1270. savesize:=_savesize;
  1271. end;
  1272. constructor tai_real_80bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1273. begin
  1274. inherited ppuload(t,ppufile);
  1275. value:=ppufile.getreal;
  1276. savesize:=ppufile.getbyte;
  1277. end;
  1278. procedure tai_real_80bit.ppuwrite(ppufile:tcompilerppufile);
  1279. begin
  1280. inherited ppuwrite(ppufile);
  1281. ppufile.putreal(value);
  1282. ppufile.putbyte(savesize);
  1283. end;
  1284. {****************************************************************************
  1285. TAI_real_80bit
  1286. ****************************************************************************}
  1287. constructor tai_real_128bit.Create(_value : ts128real);
  1288. begin
  1289. inherited Create;
  1290. typ:=ait_real_128bit;
  1291. value:=_value;
  1292. end;
  1293. constructor tai_real_128bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1294. begin
  1295. inherited ppuload(t,ppufile);
  1296. value:=ppufile.getreal;
  1297. end;
  1298. procedure tai_real_128bit.ppuwrite(ppufile:tcompilerppufile);
  1299. begin
  1300. inherited ppuwrite(ppufile);
  1301. ppufile.putreal(value);
  1302. end;
  1303. {****************************************************************************
  1304. Tai_comp_64bit
  1305. ****************************************************************************}
  1306. constructor tai_comp_64bit.Create(_value : ts64comp);
  1307. begin
  1308. inherited Create;
  1309. typ:=ait_comp_64bit;
  1310. value:=_value;
  1311. end;
  1312. constructor tai_comp_64bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1313. begin
  1314. inherited ppuload(t,ppufile);
  1315. ppufile.putdata(value,sizeof(value));
  1316. end;
  1317. procedure tai_comp_64bit.ppuwrite(ppufile:tcompilerppufile);
  1318. begin
  1319. inherited ppuwrite(ppufile);
  1320. ppufile.getdata(value,sizeof(value));
  1321. end;
  1322. {****************************************************************************
  1323. TAI_STRING
  1324. ****************************************************************************}
  1325. constructor tai_string.Create(const _str : string);
  1326. begin
  1327. inherited Create;
  1328. typ:=ait_string;
  1329. len:=length(_str);
  1330. getmem(str,len+1);
  1331. strpcopy(str,_str);
  1332. end;
  1333. constructor tai_string.Create_pchar(_str : pchar;length : longint);
  1334. begin
  1335. inherited Create;
  1336. typ:=ait_string;
  1337. str:=_str;
  1338. len:=length;
  1339. end;
  1340. destructor tai_string.destroy;
  1341. begin
  1342. if str<>nil then
  1343. freemem(str);
  1344. inherited Destroy;
  1345. end;
  1346. constructor tai_string.ppuload(t:taitype;ppufile:tcompilerppufile);
  1347. begin
  1348. inherited ppuload(t,ppufile);
  1349. len:=ppufile.getlongint;
  1350. getmem(str,len);
  1351. ppufile.getdata(str^,len);
  1352. end;
  1353. procedure tai_string.ppuwrite(ppufile:tcompilerppufile);
  1354. begin
  1355. inherited ppuwrite(ppufile);
  1356. ppufile.putlongint(len);
  1357. ppufile.putdata(str^,len);
  1358. end;
  1359. function tai_string.getcopy : tlinkedlistitem;
  1360. var
  1361. p : tlinkedlistitem;
  1362. begin
  1363. p:=inherited getcopy;
  1364. getmem(tai_string(p).str,len);
  1365. move(str^,tai_string(p).str^,len);
  1366. getcopy:=p;
  1367. end;
  1368. {****************************************************************************
  1369. TAI_LABEL
  1370. ****************************************************************************}
  1371. constructor tai_label.create(_labsym : tasmlabel);
  1372. begin
  1373. inherited Create;
  1374. typ:=ait_label;
  1375. labsym:=_labsym;
  1376. labsym.is_set:=true;
  1377. is_global:=(labsym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN]);
  1378. end;
  1379. constructor tai_label.ppuload(t:taitype;ppufile:tcompilerppufile);
  1380. begin
  1381. inherited ppuload(t,ppufile);
  1382. labsym:=tasmlabel(ppufile.getasmsymbol);
  1383. is_global:=boolean(ppufile.getbyte);
  1384. end;
  1385. procedure tai_label.ppuwrite(ppufile:tcompilerppufile);
  1386. begin
  1387. inherited ppuwrite(ppufile);
  1388. ppufile.putasmsymbol(labsym);
  1389. ppufile.putbyte(byte(is_global));
  1390. end;
  1391. procedure tai_label.derefimpl;
  1392. begin
  1393. labsym.is_set:=true;
  1394. end;
  1395. {****************************************************************************
  1396. tai_comment comment to be inserted in the assembler file
  1397. ****************************************************************************}
  1398. constructor tai_comment.Create(_str : pchar);
  1399. begin
  1400. inherited Create;
  1401. typ:=ait_comment;
  1402. str:=_str;
  1403. end;
  1404. destructor tai_comment.destroy;
  1405. begin
  1406. freemem(str);
  1407. inherited Destroy;
  1408. end;
  1409. constructor tai_comment.ppuload(t:taitype;ppufile:tcompilerppufile);
  1410. var
  1411. len : longint;
  1412. begin
  1413. inherited ppuload(t,ppufile);
  1414. len:=ppufile.getlongint;
  1415. getmem(str,len+1);
  1416. ppufile.getdata(str^,len);
  1417. str[len]:=#0;
  1418. end;
  1419. procedure tai_comment.ppuwrite(ppufile:tcompilerppufile);
  1420. var
  1421. len : longint;
  1422. begin
  1423. inherited ppuwrite(ppufile);
  1424. len:=strlen(str);
  1425. ppufile.putlongint(len);
  1426. ppufile.putdata(str^,len);
  1427. end;
  1428. function tai_comment.getcopy : tlinkedlistitem;
  1429. var
  1430. p : tlinkedlistitem;
  1431. begin
  1432. p:=inherited getcopy;
  1433. getmem(tai_comment(p).str,strlen(str)+1);
  1434. move(str^,tai_comment(p).str^,strlen(str)+1);
  1435. getcopy:=p;
  1436. end;
  1437. {****************************************************************************
  1438. TAI_STABS
  1439. ****************************************************************************}
  1440. constructor tai_stab.create(_stabtype:TStabType;_str : pchar);
  1441. begin
  1442. inherited create;
  1443. typ:=ait_stab;
  1444. str:=_str;
  1445. stabtype:=_stabtype;
  1446. end;
  1447. constructor tai_stab.create_str(_stabtype:TStabType;const s:string);
  1448. begin
  1449. self.create(_stabtype,strpnew(s));
  1450. end;
  1451. constructor tai_stab.create_ansistr(_stabtype:TStabType;const s:ansistring);
  1452. begin
  1453. inherited create;
  1454. typ:=ait_stab;
  1455. stabtype:=_stabtype;
  1456. getmem(str,length(s)+1);
  1457. move(s[1],str^,length(s)+1);
  1458. end;
  1459. destructor tai_stab.destroy;
  1460. begin
  1461. freemem(str);
  1462. inherited destroy;
  1463. end;
  1464. {****************************************************************************
  1465. TAI_FORCE_LINE
  1466. ****************************************************************************}
  1467. constructor tai_force_line.create;
  1468. begin
  1469. inherited create;
  1470. typ:=ait_force_line;
  1471. end;
  1472. {****************************************************************************
  1473. TAI_FUNCTION_NAME
  1474. ****************************************************************************}
  1475. constructor tai_function_name.create(const s:string);
  1476. begin
  1477. inherited create;
  1478. typ:=ait_function_name;
  1479. funcname:=stringdup(s);
  1480. end;
  1481. destructor tai_function_name.destroy;
  1482. begin
  1483. stringdispose(funcname);
  1484. inherited destroy;
  1485. end;
  1486. {****************************************************************************
  1487. TAI_CUTOBJECT
  1488. ****************************************************************************}
  1489. constructor tai_cutobject.Create;
  1490. begin
  1491. inherited Create;
  1492. typ:=ait_cutobject;
  1493. place:=cut_normal;
  1494. end;
  1495. constructor tai_cutobject.Create_begin;
  1496. begin
  1497. inherited Create;
  1498. typ:=ait_cutobject;
  1499. place:=cut_begin;
  1500. end;
  1501. constructor tai_cutobject.Create_end;
  1502. begin
  1503. inherited Create;
  1504. typ:=ait_cutobject;
  1505. place:=cut_end;
  1506. end;
  1507. constructor tai_cutobject.ppuload(t:taitype;ppufile:tcompilerppufile);
  1508. begin
  1509. inherited ppuload(t,ppufile);
  1510. place:=TCutPlace(ppufile.getbyte);
  1511. end;
  1512. procedure tai_cutobject.ppuwrite(ppufile:tcompilerppufile);
  1513. begin
  1514. inherited ppuwrite(ppufile);
  1515. ppufile.putbyte(byte(place));
  1516. end;
  1517. {****************************************************************************
  1518. Tai_Marker
  1519. ****************************************************************************}
  1520. constructor Tai_Marker.Create(_Kind: TAsmMarker);
  1521. begin
  1522. Inherited Create;
  1523. typ := ait_marker;
  1524. Kind := _Kind;
  1525. end;
  1526. constructor Tai_Marker.ppuload(t:taitype;ppufile:tcompilerppufile);
  1527. begin
  1528. inherited ppuload(t,ppufile);
  1529. kind:=TAsmMarker(ppufile.getbyte);
  1530. end;
  1531. procedure Tai_Marker.ppuwrite(ppufile:tcompilerppufile);
  1532. begin
  1533. inherited ppuwrite(ppufile);
  1534. ppufile.putbyte(byte(kind));
  1535. end;
  1536. {*****************************************************************************
  1537. tai_tempalloc
  1538. *****************************************************************************}
  1539. constructor tai_tempalloc.alloc(pos,size:longint);
  1540. begin
  1541. inherited Create;
  1542. typ:=ait_tempalloc;
  1543. allocation:=true;
  1544. temppos:=pos;
  1545. tempsize:=size;
  1546. {$ifdef EXTDEBUG}
  1547. problem:=nil;
  1548. {$endif EXTDEBUG}
  1549. end;
  1550. destructor tai_tempalloc.destroy;
  1551. begin
  1552. {$ifdef EXTDEBUG}
  1553. stringdispose(problem);
  1554. {$endif EXTDEBUG}
  1555. inherited destroy;
  1556. end;
  1557. constructor tai_tempalloc.dealloc(pos,size:longint);
  1558. begin
  1559. inherited Create;
  1560. typ:=ait_tempalloc;
  1561. allocation:=false;
  1562. temppos:=pos;
  1563. tempsize:=size;
  1564. {$ifdef EXTDEBUG}
  1565. problem:=nil;
  1566. {$endif EXTDEBUG}
  1567. end;
  1568. {$ifdef EXTDEBUG}
  1569. constructor tai_tempalloc.allocinfo(pos,size:longint;const st:string);
  1570. begin
  1571. inherited Create;
  1572. typ:=ait_tempalloc;
  1573. allocation:=false;
  1574. temppos:=pos;
  1575. tempsize:=size;
  1576. problem:=stringdup(st);
  1577. end;
  1578. {$endif EXTDEBUG}
  1579. constructor tai_tempalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  1580. begin
  1581. inherited ppuload(t,ppufile);
  1582. temppos:=ppufile.getlongint;
  1583. tempsize:=ppufile.getlongint;
  1584. allocation:=boolean(ppufile.getbyte);
  1585. {$ifdef EXTDEBUG}
  1586. problem:=nil;
  1587. {$endif EXTDEBUG}
  1588. end;
  1589. procedure tai_tempalloc.ppuwrite(ppufile:tcompilerppufile);
  1590. begin
  1591. inherited ppuwrite(ppufile);
  1592. ppufile.putlongint(temppos);
  1593. ppufile.putlongint(tempsize);
  1594. ppufile.putbyte(byte(allocation));
  1595. end;
  1596. {*****************************************************************************
  1597. tai_regalloc
  1598. *****************************************************************************}
  1599. constructor tai_regalloc.alloc(r : tregister;ainstr:tai);
  1600. begin
  1601. inherited create;
  1602. typ:=ait_regalloc;
  1603. ratype:=ra_alloc;
  1604. reg:=r;
  1605. { ainstr must be an instruction }
  1606. if assigned(ainstr) and
  1607. (ainstr.typ<>ait_instruction) then
  1608. internalerror(200411011);
  1609. instr:=ainstr;
  1610. end;
  1611. constructor tai_regalloc.dealloc(r : tregister;ainstr:tai);
  1612. begin
  1613. inherited create;
  1614. typ:=ait_regalloc;
  1615. ratype:=ra_dealloc;
  1616. reg:=r;
  1617. { ainstr must be an instruction }
  1618. if assigned(ainstr) and
  1619. (ainstr.typ<>ait_instruction) then
  1620. internalerror(200411012);
  1621. instr:=ainstr;
  1622. end;
  1623. constructor tai_regalloc.sync(r : tregister);
  1624. begin
  1625. inherited create;
  1626. typ:=ait_regalloc;
  1627. ratype:=ra_sync;
  1628. reg:=r;
  1629. end;
  1630. constructor tai_regalloc.resize(r : tregister);
  1631. begin
  1632. inherited create;
  1633. typ:=ait_regalloc;
  1634. ratype:=ra_resize;
  1635. reg:=r;
  1636. end;
  1637. constructor tai_regalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  1638. begin
  1639. inherited ppuload(t,ppufile);
  1640. ppufile.getdata(reg,sizeof(Tregister));
  1641. ratype:=tregalloctype(ppufile.getbyte);
  1642. end;
  1643. procedure tai_regalloc.ppuwrite(ppufile:tcompilerppufile);
  1644. begin
  1645. inherited ppuwrite(ppufile);
  1646. ppufile.putdata(reg,sizeof(Tregister));
  1647. ppufile.putbyte(byte(ratype));
  1648. end;
  1649. {*****************************************************************************
  1650. TaiInstruction
  1651. *****************************************************************************}
  1652. constructor tai_cpu_abstract.Create(op : tasmop);
  1653. begin
  1654. inherited create;
  1655. typ:=ait_instruction;
  1656. is_jmp:=false;
  1657. opcode:=op;
  1658. ops:=0;
  1659. fillchar(condition,sizeof(condition),0);
  1660. fillchar(oper,sizeof(oper),0);
  1661. end;
  1662. destructor tai_cpu_abstract.Destroy;
  1663. var
  1664. i : integer;
  1665. begin
  1666. for i:=0 to opercnt-1 do
  1667. freeop(i);
  1668. inherited destroy;
  1669. end;
  1670. { ---------------------------------------------------------------------
  1671. Loading of operands.
  1672. ---------------------------------------------------------------------}
  1673. procedure tai_cpu_abstract.allocate_oper(opers:longint);
  1674. begin
  1675. while (opers>opercnt) do
  1676. begin
  1677. new(oper[opercnt]);
  1678. fillchar(oper[opercnt]^,sizeof(toper),0);
  1679. inc(opercnt);
  1680. end;
  1681. end;
  1682. procedure tai_cpu_abstract.loadconst(opidx:longint;l:aint);
  1683. begin
  1684. allocate_oper(opidx+1);
  1685. with oper[opidx]^ do
  1686. begin
  1687. if typ<>top_const then
  1688. clearop(opidx);
  1689. val:=l;
  1690. typ:=top_const;
  1691. end;
  1692. end;
  1693. procedure tai_cpu_abstract.loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  1694. var
  1695. r : treference;
  1696. begin
  1697. reference_reset_symbol(r,s,sofs,1);
  1698. r.refaddr:=addr_full;
  1699. loadref(opidx,r);
  1700. end;
  1701. procedure tai_cpu_abstract.loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset,forceref:boolean);
  1702. begin
  1703. if not assigned(s) then
  1704. internalerror(200204251);
  1705. allocate_oper(opidx+1);
  1706. with oper[opidx]^ do
  1707. begin
  1708. if typ<>top_local then
  1709. begin
  1710. clearop(opidx);
  1711. new(localoper);
  1712. end;
  1713. with oper[opidx]^.localoper^ do
  1714. begin
  1715. localsym:=s;
  1716. localsymofs:=sofs;
  1717. localindexreg:=indexreg;
  1718. localscale:=scale;
  1719. localgetoffset:=getoffset;
  1720. localforceref:=forceref;
  1721. end;
  1722. typ:=top_local;
  1723. end;
  1724. end;
  1725. procedure tai_cpu_abstract.loadref(opidx:longint;const r:treference);
  1726. begin
  1727. allocate_oper(opidx+1);
  1728. with oper[opidx]^ do
  1729. begin
  1730. if typ<>top_ref then
  1731. begin
  1732. clearop(opidx);
  1733. new(ref);
  1734. end;
  1735. ref^:=r;
  1736. {$ifdef x86}
  1737. { We allow this exception for x86, since overloading this would be
  1738. too much of a a speed penalty}
  1739. if (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  1740. segprefix:=ref^.segment;
  1741. {$endif}
  1742. if (cs_create_pic in current_settings.moduleswitches) and
  1743. assigned(r.symbol) and
  1744. not assigned(r.relsymbol) and
  1745. (r.refaddr=addr_no)
  1746. {$ifdef ARM}
  1747. and not(r.base=NR_R15)
  1748. {$endif ARM}
  1749. then
  1750. internalerror(200502052);
  1751. typ:=top_ref;
  1752. if assigned(add_reg_instruction_hook) then
  1753. begin
  1754. add_reg_instruction_hook(self,ref^.base);
  1755. add_reg_instruction_hook(self,ref^.index);
  1756. end;
  1757. { mark symbol as used }
  1758. if assigned(ref^.symbol) then
  1759. ref^.symbol.increfs;
  1760. if assigned(ref^.relsymbol) then
  1761. ref^.relsymbol.increfs;
  1762. end;
  1763. end;
  1764. procedure tai_cpu_abstract.loadreg(opidx:longint;r:tregister);
  1765. begin
  1766. allocate_oper(opidx+1);
  1767. with oper[opidx]^ do
  1768. begin
  1769. if typ<>top_reg then
  1770. clearop(opidx);
  1771. reg:=r;
  1772. typ:=top_reg;
  1773. end;
  1774. if assigned(add_reg_instruction_hook) then
  1775. add_reg_instruction_hook(self,r);
  1776. {$ifdef ARM}
  1777. { R15 is the PC on the ARM thus moves to R15 are jumps.
  1778. Due to speed considerations we don't use a virtual overridden method here.
  1779. Because the pc/r15 isn't handled by the reg. allocator this should never cause
  1780. problems with iregs getting r15.
  1781. }
  1782. is_jmp:=(opcode=A_MOV) and (opidx=0) and (r=NR_R15);
  1783. {$endif ARM}
  1784. end;
  1785. procedure tai_cpu_abstract.loadoper(opidx:longint;o:toper);
  1786. begin
  1787. allocate_oper(opidx+1);
  1788. clearop(opidx);
  1789. oper[opidx]^:=o;
  1790. { copy also the reference }
  1791. with oper[opidx]^ do
  1792. begin
  1793. case typ of
  1794. top_reg:
  1795. begin
  1796. if assigned(add_reg_instruction_hook) then
  1797. add_reg_instruction_hook(self,reg);
  1798. end;
  1799. top_ref:
  1800. begin
  1801. new(ref);
  1802. ref^:=o.ref^;
  1803. if assigned(add_reg_instruction_hook) then
  1804. begin
  1805. add_reg_instruction_hook(self,ref^.base);
  1806. add_reg_instruction_hook(self,ref^.index);
  1807. end;
  1808. end;
  1809. {$ifdef ARM}
  1810. top_shifterop:
  1811. begin
  1812. new(shifterop);
  1813. shifterop^:=o.shifterop^;
  1814. if assigned(add_reg_instruction_hook) then
  1815. add_reg_instruction_hook(self,shifterop^.rs);
  1816. end;
  1817. {$endif ARM}
  1818. end;
  1819. end;
  1820. end;
  1821. procedure tai_cpu_abstract.clearop(opidx:longint);
  1822. begin
  1823. with oper[opidx]^ do
  1824. begin
  1825. case typ of
  1826. top_ref:
  1827. dispose(ref);
  1828. top_local:
  1829. dispose(localoper);
  1830. {$ifdef ARM}
  1831. top_shifterop:
  1832. dispose(shifterop);
  1833. top_regset:
  1834. dispose(regset);
  1835. {$endif ARM}
  1836. end;
  1837. typ:=top_none;
  1838. end;
  1839. end;
  1840. procedure tai_cpu_abstract.freeop(opidx:longint);
  1841. begin
  1842. clearop(opidx);
  1843. dispose(oper[opidx]);
  1844. end;
  1845. { ---------------------------------------------------------------------
  1846. Miscellaneous methods.
  1847. ---------------------------------------------------------------------}
  1848. procedure tai_cpu_abstract.SetCondition(const c:TAsmCond);
  1849. begin
  1850. condition:=c;
  1851. end;
  1852. Function tai_cpu_abstract.getcopy:TLinkedListItem;
  1853. var
  1854. i : longint;
  1855. p : tai_cpu_abstract;
  1856. begin
  1857. p:=tai_cpu_abstract(inherited getcopy);
  1858. { make a copy of the references }
  1859. p.opercnt:=0;
  1860. p.allocate_oper(ops);
  1861. for i:=0 to ops-1 do
  1862. begin
  1863. p.oper[i]^:=oper[i]^;
  1864. case oper[i]^.typ of
  1865. top_local :
  1866. begin
  1867. new(p.oper[i]^.localoper);
  1868. p.oper[i]^.localoper^:=oper[i]^.localoper^;
  1869. end;
  1870. top_ref :
  1871. begin
  1872. new(p.oper[i]^.ref);
  1873. p.oper[i]^.ref^:=oper[i]^.ref^;
  1874. end;
  1875. {$ifdef ARM}
  1876. top_shifterop:
  1877. begin
  1878. new(p.oper[i]^.shifterop);
  1879. p.oper[i]^.shifterop^:=oper[i]^.shifterop^;
  1880. end;
  1881. {$endif ARM}
  1882. end;
  1883. end;
  1884. getcopy:=p;
  1885. end;
  1886. function tai_cpu_abstract.is_same_reg_move(regtype: Tregistertype):boolean;
  1887. begin
  1888. { When the generic RA is used this needs to be overridden, we don't use
  1889. virtual;abstract; to prevent a lot of warnings of unimplemented abstract methods
  1890. when tai_cpu is created (PFV) }
  1891. internalerror(200404091);
  1892. result:=false;
  1893. end;
  1894. function tai_cpu_abstract.spilling_get_operation_type(opnr: longint): topertype;
  1895. begin
  1896. internalerror(200404091);
  1897. result:=operand_readwrite;
  1898. end;
  1899. function tai_cpu_abstract.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
  1900. begin
  1901. result := operand_read;
  1902. end;
  1903. constructor tai_cpu_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  1904. var
  1905. i : integer;
  1906. begin
  1907. inherited ppuload(t,ppufile);
  1908. { hopefully, we don't get problems with big/litte endian here when cross compiling :/ }
  1909. ppufile.getdata(condition,sizeof(tasmcond));
  1910. allocate_oper(ppufile.getbyte);
  1911. for i:=0 to ops-1 do
  1912. ppuloadoper(ppufile,oper[i]^);
  1913. opcode:=tasmop(ppufile.getword);
  1914. {$ifdef x86}
  1915. ppufile.getdata(segprefix,sizeof(Tregister));
  1916. {$endif x86}
  1917. is_jmp:=boolean(ppufile.getbyte);
  1918. end;
  1919. procedure tai_cpu_abstract.ppuwrite(ppufile:tcompilerppufile);
  1920. var
  1921. i : integer;
  1922. begin
  1923. inherited ppuwrite(ppufile);
  1924. ppufile.putdata(condition,sizeof(tasmcond));
  1925. ppufile.putbyte(ops);
  1926. for i:=0 to ops-1 do
  1927. ppuwriteoper(ppufile,oper[i]^);
  1928. ppufile.putword(word(opcode));
  1929. {$ifdef x86}
  1930. ppufile.putdata(segprefix,sizeof(Tregister));
  1931. {$endif x86}
  1932. ppufile.putbyte(byte(is_jmp));
  1933. end;
  1934. procedure tai_cpu_abstract.buildderefimpl;
  1935. var
  1936. i : integer;
  1937. begin
  1938. for i:=0 to ops-1 do
  1939. ppubuildderefimploper(oper[i]^);
  1940. end;
  1941. procedure tai_cpu_abstract.derefimpl;
  1942. var
  1943. i : integer;
  1944. begin
  1945. for i:=0 to ops-1 do
  1946. ppuderefoper(oper[i]^);
  1947. end;
  1948. procedure tai_cpu_abstract.resetpass1;
  1949. begin
  1950. end;
  1951. procedure tai_cpu_abstract.resetpass2;
  1952. begin
  1953. end;
  1954. function tai_cpu_abstract.Pass1(objdata:TObjData):longint;
  1955. begin
  1956. result:=0;
  1957. end;
  1958. procedure tai_cpu_abstract.Pass2(objdata:TObjData);
  1959. begin
  1960. end;
  1961. procedure tai_cpu_abstract.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  1962. begin
  1963. o.typ:=toptype(ppufile.getbyte);
  1964. o.ot:=ppufile.getlongint;
  1965. case o.typ of
  1966. top_reg :
  1967. ppufile.getdata(o.reg,sizeof(Tregister));
  1968. top_ref :
  1969. begin
  1970. new(o.ref);
  1971. {$ifdef x86}
  1972. ppufile.getdata(o.ref^.segment,sizeof(Tregister));
  1973. {$endif x86}
  1974. ppufile.getdata(o.ref^.base,sizeof(Tregister));
  1975. ppufile.getdata(o.ref^.index,sizeof(Tregister));
  1976. ppufile.getdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));
  1977. o.ref^.scalefactor:=ppufile.getbyte;
  1978. o.ref^.offset:=ppufile.getaint;
  1979. o.ref^.symbol:=ppufile.getasmsymbol;
  1980. o.ref^.relsymbol:=ppufile.getasmsymbol;
  1981. end;
  1982. top_const :
  1983. o.val:=ppufile.getaint;
  1984. top_local :
  1985. begin
  1986. new(o.localoper);
  1987. with o.localoper^ do
  1988. begin
  1989. ppufile.getderef(localsymderef);
  1990. localsymofs:=ppufile.getaint;
  1991. localindexreg:=tregister(ppufile.getlongint);
  1992. localscale:=ppufile.getbyte;
  1993. localgetoffset:=(ppufile.getbyte<>0);
  1994. end;
  1995. end;
  1996. else
  1997. internalerror(2007010210);
  1998. end;
  1999. end;
  2000. procedure tai_cpu_abstract.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  2001. begin
  2002. ppufile.putbyte(byte(o.typ));
  2003. ppufile.putlongint(o.ot);
  2004. case o.typ of
  2005. top_reg :
  2006. ppufile.putdata(o.reg,sizeof(Tregister));
  2007. top_ref :
  2008. begin
  2009. {$ifdef x86}
  2010. ppufile.putdata(o.ref^.segment,sizeof(Tregister));
  2011. {$endif x86}
  2012. ppufile.putdata(o.ref^.base,sizeof(Tregister));
  2013. ppufile.putdata(o.ref^.index,sizeof(Tregister));
  2014. ppufile.putdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));
  2015. ppufile.putbyte(o.ref^.scalefactor);
  2016. ppufile.putaint(o.ref^.offset);
  2017. ppufile.putasmsymbol(o.ref^.symbol);
  2018. ppufile.putasmsymbol(o.ref^.relsymbol);
  2019. end;
  2020. top_const :
  2021. ppufile.putaint(o.val);
  2022. top_local :
  2023. begin
  2024. with o.localoper^ do
  2025. begin
  2026. ppufile.putderef(localsymderef);
  2027. ppufile.putaint(localsymofs);
  2028. ppufile.putlongint(longint(localindexreg));
  2029. ppufile.putbyte(localscale);
  2030. ppufile.putbyte(byte(localgetoffset));
  2031. end;
  2032. end;
  2033. else
  2034. internalerror(2007010211);
  2035. end;
  2036. end;
  2037. {****************************************************************************
  2038. tai_align_abstract
  2039. ****************************************************************************}
  2040. constructor tai_align_abstract.Create(b: byte);
  2041. begin
  2042. inherited Create;
  2043. typ:=ait_align;
  2044. {$ifdef EXTDEBUG}
  2045. if upper(classname)='TAI_ALIGN_ABSTRACT' then
  2046. internalerror(200709191);
  2047. {$endif EXTDEBUG}
  2048. if b in [1,2,4,8,16,32] then
  2049. aligntype := b
  2050. else
  2051. aligntype := 1;
  2052. fillsize:=0;
  2053. fillop:=0;
  2054. use_op:=false;
  2055. end;
  2056. constructor tai_align_abstract.Create_op(b: byte; _op: byte);
  2057. begin
  2058. inherited Create;
  2059. typ:=ait_align;
  2060. if b in [1,2,4,8,16,32] then
  2061. aligntype := b
  2062. else
  2063. aligntype := 1;
  2064. fillsize:=0;
  2065. fillop:=_op;
  2066. use_op:=true;
  2067. end;
  2068. constructor tai_align_abstract.Create_zeros(b: byte);
  2069. begin
  2070. inherited Create;
  2071. typ:=ait_align;
  2072. if b in [1,2,4,8,16,32] then
  2073. aligntype := b
  2074. else
  2075. aligntype := 1;
  2076. use_op:=true;
  2077. fillsize:=0;
  2078. fillop:=0;
  2079. end;
  2080. function tai_align_abstract.calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;
  2081. begin
  2082. if fillsize>sizeof(buf) then
  2083. internalerror(200404293);
  2084. fillchar(buf,high(buf),fillop);
  2085. calculatefillbuf:=pchar(@buf);
  2086. end;
  2087. constructor tai_align_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  2088. begin
  2089. inherited ppuload(t,ppufile);
  2090. aligntype:=ppufile.getbyte;
  2091. fillsize:=0;
  2092. fillop:=ppufile.getbyte;
  2093. use_op:=boolean(ppufile.getbyte);
  2094. end;
  2095. procedure tai_align_abstract.ppuwrite(ppufile:tcompilerppufile);
  2096. begin
  2097. inherited ppuwrite(ppufile);
  2098. ppufile.putbyte(aligntype);
  2099. ppufile.putbyte(fillop);
  2100. ppufile.putbyte(byte(use_op));
  2101. end;
  2102. {****************************************************************************
  2103. tai_seh_directive
  2104. ****************************************************************************}
  2105. const
  2106. datatypemap: array[TAsmSehDirective] of TSehDirectiveDatatype=(
  2107. sd_string, { proc }
  2108. sd_none, { endproc }
  2109. sd_none, { endprologue }
  2110. sd_string, { handler }
  2111. sd_none, { handlerdata }
  2112. sd_none,sd_none,sd_none, { eh, 32, no32 }
  2113. sd_regoffset, { setframe }
  2114. sd_offset, { stackalloc }
  2115. sd_reg, { pushreg }
  2116. sd_regoffset, { savereg }
  2117. sd_regoffset, { savexmm }
  2118. sd_none { pushframe }
  2119. );
  2120. constructor tai_seh_directive.create(_kind:TAsmSehDirective);
  2121. begin
  2122. inherited Create;
  2123. typ:=ait_seh_directive;
  2124. kind:=_kind;
  2125. data.typ:=datatypemap[_kind];
  2126. end;
  2127. constructor tai_seh_directive.create_name(_kind:TAsmSehDirective;const _name:string);
  2128. begin
  2129. create(_kind);
  2130. data.name:=stringdup(_name);
  2131. end;
  2132. constructor tai_seh_directive.create_reg(_kind:TAsmSehDirective;r:TRegister);
  2133. begin
  2134. create(_kind);
  2135. data.reg:=r;
  2136. end;
  2137. constructor tai_seh_directive.create_offset(_kind:TAsmSehDirective;ofs:dword);
  2138. begin
  2139. create(_kind);
  2140. data.offset:=ofs;
  2141. end;
  2142. constructor tai_seh_directive.create_reg_offset(_kind:TAsmSehDirective;
  2143. r:TRegister;ofs:dword);
  2144. begin
  2145. create(_kind);
  2146. data.offset:=ofs;
  2147. data.reg:=r;
  2148. end;
  2149. constructor tai_seh_directive.ppuload(t:taitype;ppufile:tcompilerppufile);
  2150. begin
  2151. inherited ppuload(t, ppufile);
  2152. kind:=TAsmSehDirective(ppufile.getbyte);
  2153. data.typ:=datatypemap[kind];
  2154. case data.typ of
  2155. sd_none: ;
  2156. sd_string:
  2157. begin
  2158. data.name:=stringdup(ppufile.getstring);
  2159. data.flags:=ppufile.getbyte;
  2160. end;
  2161. sd_reg,sd_offset,sd_regoffset:
  2162. begin
  2163. ppufile.getdata(data.reg,sizeof(TRegister));
  2164. data.offset:=ppufile.getdword;
  2165. end;
  2166. else
  2167. InternalError(2011091201);
  2168. end;
  2169. end;
  2170. destructor tai_seh_directive.destroy;
  2171. begin
  2172. if data.typ=sd_string then
  2173. stringdispose(data.name);
  2174. inherited destroy;
  2175. end;
  2176. procedure tai_seh_directive.ppuwrite(ppufile:tcompilerppufile);
  2177. begin
  2178. inherited ppuwrite(ppufile);
  2179. ppufile.putbyte(ord(kind));
  2180. case data.typ of
  2181. sd_none: ;
  2182. sd_string:
  2183. begin
  2184. ppufile.putstring(data.name^);
  2185. ppufile.putbyte(data.flags);
  2186. end;
  2187. sd_reg,sd_offset,sd_regoffset:
  2188. begin
  2189. ppufile.putdata(data.reg,sizeof(TRegister));
  2190. ppufile.putdword(data.offset);
  2191. end;
  2192. else
  2193. InternalError(2011091202);
  2194. end;
  2195. end;
  2196. procedure tai_seh_directive.generate_code(objdata:TObjData);
  2197. begin
  2198. end;
  2199. begin
  2200. {$push}{$warnings off}
  2201. { taitype should fit into a 4 byte set for speed reasons }
  2202. if ord(high(taitype))>31 then
  2203. internalerror(201108181);
  2204. {$pop}
  2205. end.