aasmtai.pas 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650
  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:aint);
  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 : pshortstring;
  343. directive : TAsmDirective;
  344. constructor Create(_directive:TAsmDirective;const _name:string);
  345. destructor Destroy;override;
  346. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  347. procedure ppuwrite(ppufile:tcompilerppufile);override;
  348. end;
  349. { Generates an assembler label }
  350. tai_label = class(tai)
  351. is_global : boolean;
  352. labsym : tasmlabel;
  353. constructor Create(_labsym : tasmlabel);
  354. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  355. procedure ppuwrite(ppufile:tcompilerppufile);override;
  356. procedure derefimpl;override;
  357. end;
  358. { Generates an assembler comment }
  359. tai_comment = class(tai)
  360. str : pchar;
  361. constructor Create(_str : pchar);
  362. destructor Destroy; override;
  363. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  364. procedure ppuwrite(ppufile:tcompilerppufile);override;
  365. function getcopy:tlinkedlistitem;override;
  366. end;
  367. { Generates a section / segment directive }
  368. tai_section = class(tai)
  369. sectype : TAsmSectiontype;
  370. secorder : TasmSectionorder;
  371. secalign : byte;
  372. name : pshortstring;
  373. sec : TObjSection; { used in binary writer }
  374. destructor Destroy;override;
  375. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  376. procedure ppuwrite(ppufile:tcompilerppufile);override;
  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. end;
  382. { Generates an uninitializised data block }
  383. tai_datablock = class(tailineinfo)
  384. is_global : boolean;
  385. sym : tasmsymbol;
  386. size : asizeint;
  387. constructor Create(const _name : string;_size : aint);
  388. constructor Create_global(const _name : string;_size : asizeint);
  389. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  390. procedure ppuwrite(ppufile:tcompilerppufile);override;
  391. procedure derefimpl;override;
  392. end;
  393. { Generates an integer const }
  394. tai_const = class(tai)
  395. sym,
  396. endsym : tasmsymbol;
  397. { if symbols and offset are provided the symofs is used,
  398. the value is calculated during assembling }
  399. symofs,
  400. value : int64;
  401. consttype : taiconst_type;
  402. { we use for the 128bit int64/qword for now because I can't imagine a
  403. case where we need 128 bit now (FK) }
  404. constructor Create(_typ:taiconst_type;_value : int64);
  405. constructor Create_128bit(_value : int64);
  406. constructor Create_64bit(_value : int64);
  407. constructor Create_32bit(_value : longint);
  408. constructor Create_16bit(_value : word);
  409. constructor Create_8bit(_value : byte);
  410. constructor Create_char(size: integer; _value: dword);
  411. constructor Create_sleb128bit(_value : int64);
  412. constructor Create_uleb128bit(_value : qword);
  413. constructor Create_aint(_value : aint);
  414. constructor Create_pint(_value : pint);
  415. constructor Create_sym(_sym:tasmsymbol);
  416. constructor Create_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  417. constructor Create_sym_offset(_sym:tasmsymbol;ofs:aint);
  418. constructor Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  419. constructor Create_rva_sym(_sym:tasmsymbol);
  420. constructor Createname(const name:string;ofs:aint);
  421. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  422. procedure ppuwrite(ppufile:tcompilerppufile);override;
  423. procedure derefimpl;override;
  424. function getcopy:tlinkedlistitem;override;
  425. function size:longint;
  426. end;
  427. { Generates a single float (32 bit real) }
  428. tai_real_32bit = class(tai)
  429. value : ts32real;
  430. constructor Create(_value : ts32real);
  431. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  432. procedure ppuwrite(ppufile:tcompilerppufile);override;
  433. end;
  434. tformatoptions = (fo_none,fo_hiloswapped);
  435. { Generates a double float (64 bit real) }
  436. tai_real_64bit = class(tai)
  437. value : ts64real;
  438. {$ifdef ARM}
  439. formatoptions : tformatoptions;
  440. constructor Create_hiloswapped(_value : ts64real);
  441. {$endif ARM}
  442. constructor Create(_value : ts64real);
  443. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  444. procedure ppuwrite(ppufile:tcompilerppufile);override;
  445. end;
  446. { Generates an extended float (80 bit real) }
  447. tai_real_80bit = class(tai)
  448. value : ts80real;
  449. savesize : byte;
  450. constructor Create(_value : ts80real; _savesize: byte);
  451. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  452. procedure ppuwrite(ppufile:tcompilerppufile);override;
  453. end;
  454. { Generates an float128 (128 bit real) }
  455. tai_real_128bit = class(tai)
  456. value : ts128real;
  457. constructor Create(_value : ts128real);
  458. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  459. procedure ppuwrite(ppufile:tcompilerppufile);override;
  460. end;
  461. { Generates a comp int (integer over 64 bits)
  462. This is Intel 80x86 specific, and is not
  463. really supported on other processors.
  464. }
  465. tai_comp_64bit = class(tai)
  466. value : ts64comp;
  467. constructor Create(_value : ts64comp);
  468. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  469. procedure ppuwrite(ppufile:tcompilerppufile);override;
  470. end;
  471. { tai_stab }
  472. tai_stab = class(tai)
  473. str : pchar;
  474. stabtype : TStabType;
  475. constructor Create(_stabtype:TStabType;_str : pchar);
  476. constructor Create_str(_stabtype:TStabType;const s:string);
  477. constructor create_ansistr(_stabtype: TStabType; const s: ansistring);
  478. destructor Destroy;override;
  479. end;
  480. tai_force_line = class(tailineinfo)
  481. constructor Create;
  482. end;
  483. tai_function_name = class(tai)
  484. funcname : pshortstring;
  485. constructor create(const s:string);
  486. destructor destroy;override;
  487. end;
  488. { Insert a cut to split assembler into several smaller files }
  489. tai_cutobject = class(tai)
  490. place : tcutplace;
  491. constructor Create;
  492. constructor Create_begin;
  493. constructor Create_end;
  494. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  495. procedure ppuwrite(ppufile:tcompilerppufile);override;
  496. end;
  497. { Insert a marker for assembler and inline blocks }
  498. tai_marker = class(tai)
  499. Kind: TAsmMarker;
  500. Constructor Create(_Kind: TAsmMarker);
  501. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  502. procedure ppuwrite(ppufile:tcompilerppufile);override;
  503. end;
  504. tai_tempalloc = class(tai)
  505. allocation : boolean;
  506. {$ifdef EXTDEBUG}
  507. problem : pshortstring;
  508. {$endif EXTDEBUG}
  509. temppos,
  510. tempsize : longint;
  511. constructor alloc(pos,size:longint);
  512. constructor dealloc(pos,size:longint);
  513. {$ifdef EXTDEBUG}
  514. constructor allocinfo(pos,size:longint;const st:string);
  515. {$endif EXTDEBUG}
  516. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  517. destructor destroy;override;
  518. procedure ppuwrite(ppufile:tcompilerppufile);override;
  519. end;
  520. tai_regalloc = class(tai)
  521. reg : tregister;
  522. ratype : TRegAllocType;
  523. { reg(de)alloc belongs to this instruction, this
  524. is only used for automatic inserted (de)alloc for
  525. imaginary register and required for spilling code }
  526. instr : tai;
  527. constructor alloc(r : tregister;ainstr:tai);
  528. constructor dealloc(r : tregister;ainstr:tai);
  529. constructor sync(r : tregister);
  530. constructor resize(r : tregister);
  531. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  532. procedure ppuwrite(ppufile:tcompilerppufile);override;
  533. end;
  534. tadd_reg_instruction_proc=procedure(instr:Tai;r:tregister) of object;
  535. { Class template for assembler instructions
  536. }
  537. tai_cpu_abstract = class(tailineinfo)
  538. protected
  539. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);virtual;
  540. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);virtual;
  541. procedure ppubuildderefimploper(var o:toper);virtual;abstract;
  542. procedure ppuderefoper(var o:toper);virtual;abstract;
  543. public
  544. { Condition flags for instruction }
  545. condition : TAsmCond;
  546. { Number of operands to instruction }
  547. ops : byte;
  548. { Number of allocate oper structures }
  549. opercnt : byte;
  550. { Operands of instruction }
  551. oper : array[0..max_operands-1] of poper;
  552. { Actual opcode of instruction }
  553. opcode : tasmop;
  554. {$ifdef x86}
  555. segprefix : tregister;
  556. {$endif x86}
  557. { true if instruction is a jmp }
  558. is_jmp : boolean; { is this instruction a jump? (needed for optimizer) }
  559. Constructor Create(op : tasmop);virtual;
  560. Destructor Destroy;override;
  561. function getcopy:TLinkedListItem;override;
  562. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  563. procedure ppuwrite(ppufile:tcompilerppufile);override;
  564. procedure buildderefimpl;override;
  565. procedure derefimpl;override;
  566. procedure SetCondition(const c:TAsmCond);
  567. procedure allocate_oper(opers:longint);
  568. procedure loadconst(opidx:longint;l:aint);
  569. procedure loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  570. procedure loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset,forceref:boolean);
  571. procedure loadref(opidx:longint;const r:treference);
  572. procedure loadreg(opidx:longint;r:tregister);
  573. procedure loadoper(opidx:longint;o:toper);
  574. procedure clearop(opidx:longint);
  575. procedure freeop(opidx:longint);
  576. { register allocator }
  577. function is_same_reg_move(regtype: Tregistertype):boolean;virtual;
  578. function spilling_get_operation_type(opnr: longint): topertype;virtual;
  579. function spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;virtual;
  580. function Pass1(objdata:TObjData):longint;virtual;
  581. procedure Pass2(objdata:TObjData);virtual;
  582. procedure resetpass1; virtual;
  583. procedure resetpass2; virtual;
  584. end;
  585. tai_cpu_class = class of tai_cpu_abstract;
  586. { Buffer type used for alignment }
  587. tfillbuffer = array[0..63] of char;
  588. { alignment for operator }
  589. tai_align_abstract = class(tai)
  590. aligntype : byte; { 1 = no align, 2 = word align, 4 = dword align }
  591. fillsize : byte; { real size to fill }
  592. fillop : byte; { value to fill with - optional }
  593. use_op : boolean;
  594. constructor Create(b:byte);virtual;
  595. constructor Create_op(b: byte; _op: byte);virtual;
  596. constructor Create_zeros(b:byte);
  597. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  598. procedure ppuwrite(ppufile:tcompilerppufile);override;
  599. function calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;virtual;
  600. end;
  601. tai_align_class = class of tai_align_abstract;
  602. tai_varloc = class(tai)
  603. newlocation,
  604. newlocationhi : tregister;
  605. varsym : tsym;
  606. constructor create(sym : tsym;loc : tregister);
  607. constructor create64(sym : tsym;loc,lochi : tregister);
  608. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  609. procedure ppuwrite(ppufile:tcompilerppufile);override;
  610. procedure buildderefimpl;override;
  611. procedure derefimpl;override;
  612. end;
  613. TSehDirectiveDatatype=(sd_none,sd_string,sd_reg,sd_offset,sd_regoffset);
  614. TSehDirectiveData=record
  615. case typ: TSehDirectiveDatatype of
  616. sd_none: ();
  617. sd_string: (name:pshortstring;flags:byte);
  618. sd_reg,sd_offset,sd_regoffset: (reg:TRegister;offset:dword);
  619. end;
  620. tai_seh_directive = class(tai)
  621. kind: TAsmSehDirective;
  622. data: TSehDirectiveData;
  623. constructor create(_kind:TAsmSehDirective);
  624. constructor create_name(_kind:TAsmSehDirective;const _name: string);
  625. constructor create_reg(_kind:TAsmSehDirective;r:TRegister);
  626. constructor create_offset(_kind:TAsmSehDirective;ofs:dword);
  627. constructor create_reg_offset(_kind:TAsmSehDirective;r:TRegister;ofs:dword);
  628. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  629. destructor destroy;override;
  630. procedure ppuwrite(ppufile:tcompilerppufile);override;
  631. procedure generate_code(objdata:TObjData);virtual;
  632. property datatype: TSehDirectiveDatatype read data.typ;
  633. end;
  634. tai_seh_directive_class=class of tai_seh_directive;
  635. var
  636. { array with all class types for tais }
  637. aiclass : taiclassarray;
  638. { target specific tais, possibly overwritten in target specific aasmcpu }
  639. cai_align : tai_align_class = tai_align_abstract;
  640. cai_cpu : tai_cpu_class = tai_cpu_abstract;
  641. cai_seh_directive: tai_seh_directive_class = tai_seh_directive;
  642. { hook to notify uses of registers }
  643. add_reg_instruction_hook : tadd_reg_instruction_proc;
  644. procedure maybe_new_object_file(list:TAsmList);
  645. procedure new_section(list:TAsmList;Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  646. procedure section_symbol_start(list:TAsmList;const Aname:string;Asymtyp:Tasmsymtype;
  647. Aglobal:boolean;Asectype:TAsmSectiontype;Aalign:byte);
  648. procedure section_symbol_end(list:TAsmList;const Aname:string);
  649. function ppuloadai(ppufile:tcompilerppufile):tai;
  650. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  651. implementation
  652. uses
  653. SysUtils,
  654. verbose,
  655. globals,
  656. fmodule;
  657. const
  658. pputaimarker = 254;
  659. {****************************************************************************
  660. Helpers
  661. ****************************************************************************}
  662. procedure maybe_new_object_file(list:TAsmList);
  663. begin
  664. if create_smartlink_library then
  665. list.concat(tai_cutobject.create);
  666. end;
  667. procedure new_section(list:TAsmList;Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  668. begin
  669. list.concat(tai_section.create(Asectype,Aname,Aalign,Asecorder));
  670. list.concat(cai_align.create(Aalign));
  671. end;
  672. procedure section_symbol_start(list:TAsmList;const Aname:string;Asymtyp:Tasmsymtype;
  673. Aglobal:boolean;Asectype:TAsmSectiontype;Aalign:byte);
  674. begin
  675. maybe_new_object_file(list);
  676. new_section(list,Asectype,Aname,Aalign);
  677. if Aglobal or
  678. create_smartlink then
  679. list.concat(tai_symbol.createname_global(Aname,Asymtyp,0))
  680. else
  681. list.concat(tai_symbol.createname(Aname,Asymtyp,0));
  682. end;
  683. procedure section_symbol_end(list:TAsmList;const Aname:string);
  684. begin
  685. list.concat(tai_symbol_end.createname(Aname));
  686. end;
  687. function ppuloadai(ppufile:tcompilerppufile):tai;
  688. var
  689. b : byte;
  690. t : taitype;
  691. begin
  692. { marker }
  693. b:=ppufile.getbyte;
  694. if b<>pputaimarker then
  695. internalerror(200208181);
  696. { load nodetype }
  697. t:=taitype(ppufile.getbyte);
  698. if t<>ait_none then
  699. begin
  700. if t>high(taitype) then
  701. internalerror(200208182);
  702. if not assigned(aiclass[t]) then
  703. internalerror(200208183);
  704. {writeln('taiload: ',taitypestr[t]);}
  705. { generate tai of the correct class }
  706. ppuloadai:=aiclass[t].ppuload(t,ppufile);
  707. end
  708. else
  709. ppuloadai:=nil;
  710. end;
  711. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  712. begin
  713. { marker, read by ppuloadnode }
  714. ppufile.putbyte(pputaimarker);
  715. if assigned(n) then
  716. begin
  717. { type, read by ppuloadnode }
  718. ppufile.putbyte(byte(n.typ));
  719. {writeln('taiwrite: ',taitypestr[n.typ]);}
  720. n.ppuwrite(ppufile);
  721. end
  722. else
  723. ppufile.putbyte(byte(ait_none));
  724. end;
  725. constructor tai_varloc.create(sym: tsym; loc: tregister);
  726. begin
  727. inherited Create;
  728. typ:=ait_varloc;
  729. newlocation:=loc;
  730. newlocationhi:=NR_NO;
  731. varsym:=sym;
  732. end;
  733. constructor tai_varloc.create64(sym: tsym; loc: tregister;lochi : tregister);
  734. begin
  735. inherited Create;
  736. typ:=ait_varloc;
  737. newlocation:=loc;
  738. newlocationhi:=lochi;
  739. varsym:=sym;
  740. end;
  741. constructor tai_varloc.ppuload(t: taitype; ppufile: tcompilerppufile);
  742. begin
  743. inherited ppuload(t, ppufile);
  744. end;
  745. procedure tai_varloc.ppuwrite(ppufile: tcompilerppufile);
  746. begin
  747. inherited ppuwrite(ppufile);
  748. end;
  749. procedure tai_varloc.buildderefimpl;
  750. begin
  751. inherited buildderefimpl;
  752. end;
  753. procedure tai_varloc.derefimpl;
  754. begin
  755. inherited derefimpl;
  756. end;
  757. {****************************************************************************
  758. TAI
  759. ****************************************************************************}
  760. constructor tai.Create;
  761. begin
  762. {$ifndef NOOPT}
  763. optinfo:=nil;
  764. {$endif NOOPT}
  765. end;
  766. constructor tai.ppuload(t:taitype;ppufile:tcompilerppufile);
  767. begin
  768. typ:=t;
  769. {$ifndef NOOPT}
  770. optinfo:=nil;
  771. {$endif}
  772. end;
  773. procedure tai.ppuwrite(ppufile:tcompilerppufile);
  774. begin
  775. end;
  776. procedure tai.buildderefimpl;
  777. begin
  778. end;
  779. procedure tai.derefimpl;
  780. begin
  781. end;
  782. {****************************************************************************
  783. TAILINEINFO
  784. ****************************************************************************}
  785. constructor tailineinfo.create;
  786. begin
  787. inherited create;
  788. fileinfo:=current_filepos;
  789. end;
  790. constructor tailineinfo.ppuload(t:taitype;ppufile:tcompilerppufile);
  791. begin
  792. inherited ppuload(t,ppufile);
  793. ppufile.getposinfo(fileinfo);
  794. end;
  795. procedure tailineinfo.ppuwrite(ppufile:tcompilerppufile);
  796. begin
  797. inherited ppuwrite(ppufile);
  798. ppufile.putposinfo(fileinfo);
  799. end;
  800. {****************************************************************************
  801. TAI_SIMPLE
  802. ****************************************************************************}
  803. constructor tai_simple.create(_typ : taitype);
  804. begin
  805. inherited create;
  806. typ:=_typ;
  807. end;
  808. {****************************************************************************
  809. TAI_SECTION
  810. ****************************************************************************}
  811. constructor tai_section.Create(Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  812. begin
  813. inherited Create;
  814. typ:=ait_section;
  815. sectype:=asectype;
  816. secalign:=Aalign;
  817. secorder:=Asecorder;
  818. name:=stringdup(Aname);
  819. sec:=nil;
  820. end;
  821. constructor tai_section.ppuload(t:taitype;ppufile:tcompilerppufile);
  822. begin
  823. inherited ppuload(t,ppufile);
  824. sectype:=TAsmSectiontype(ppufile.getbyte);
  825. secalign:=ppufile.getbyte;
  826. name:=stringdup(ppufile.getstring);
  827. sec:=nil;
  828. end;
  829. destructor tai_section.Destroy;
  830. begin
  831. stringdispose(name);
  832. end;
  833. procedure tai_section.ppuwrite(ppufile:tcompilerppufile);
  834. begin
  835. inherited ppuwrite(ppufile);
  836. ppufile.putbyte(byte(sectype));
  837. ppufile.putbyte(secalign);
  838. ppufile.putstring(name^);
  839. end;
  840. {****************************************************************************
  841. TAI_DATABLOCK
  842. ****************************************************************************}
  843. constructor tai_datablock.Create(const _name : string;_size : aint);
  844. begin
  845. inherited Create;
  846. typ:=ait_datablock;
  847. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,AT_DATA);
  848. { keep things aligned }
  849. if _size<=0 then
  850. _size:=sizeof(aint);
  851. size:=_size;
  852. is_global:=false;
  853. end;
  854. constructor tai_datablock.Create_global(const _name : string;_size : asizeint);
  855. begin
  856. inherited Create;
  857. typ:=ait_datablock;
  858. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,AT_DATA);
  859. { keep things aligned }
  860. if _size<=0 then
  861. _size:=sizeof(aint);
  862. size:=_size;
  863. is_global:=true;
  864. end;
  865. constructor tai_datablock.ppuload(t:taitype;ppufile:tcompilerppufile);
  866. begin
  867. inherited Create;
  868. sym:=ppufile.getasmsymbol;
  869. size:=ppufile.getaint;
  870. is_global:=boolean(ppufile.getbyte);
  871. end;
  872. procedure tai_datablock.ppuwrite(ppufile:tcompilerppufile);
  873. begin
  874. inherited ppuwrite(ppufile);
  875. ppufile.putasmsymbol(sym);
  876. ppufile.putaint(size);
  877. ppufile.putbyte(byte(is_global));
  878. end;
  879. procedure tai_datablock.derefimpl;
  880. begin
  881. end;
  882. {****************************************************************************
  883. TAI_SYMBOL
  884. ****************************************************************************}
  885. constructor tai_symbol.Create(_sym:tasmsymbol;siz:longint);
  886. begin
  887. inherited Create;
  888. typ:=ait_symbol;
  889. sym:=_sym;
  890. size:=siz;
  891. sym.bind:=AB_LOCAL;
  892. is_global:=false;
  893. end;
  894. constructor tai_symbol.Create_global(_sym:tasmsymbol;siz:longint);
  895. begin
  896. inherited Create;
  897. typ:=ait_symbol;
  898. sym:=_sym;
  899. size:=siz;
  900. { don't override PRIVATE_EXTERN with GLOBAL }
  901. if not(sym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN]) then
  902. sym.bind:=AB_GLOBAL;
  903. is_global:=true;
  904. end;
  905. constructor tai_symbol.Createname(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  906. begin
  907. inherited Create;
  908. typ:=ait_symbol;
  909. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,_symtyp);
  910. size:=siz;
  911. is_global:=false;
  912. end;
  913. constructor tai_symbol.Createname_global(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  914. begin
  915. inherited Create;
  916. typ:=ait_symbol;
  917. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,_symtyp);
  918. size:=siz;
  919. is_global:=true;
  920. end;
  921. constructor tai_symbol.createname_global_value(const _name: string;_symtyp: tasmsymtype; siz: longint; val: ptruint);
  922. begin
  923. Createname_global(_name,_symtyp,siz);
  924. value:=val;
  925. has_value:=true;
  926. end;
  927. constructor tai_symbol.ppuload(t:taitype;ppufile:tcompilerppufile);
  928. begin
  929. inherited ppuload(t,ppufile);
  930. sym:=ppufile.getasmsymbol;
  931. size:=ppufile.getlongint;
  932. is_global:=boolean(ppufile.getbyte);
  933. end;
  934. procedure tai_symbol.ppuwrite(ppufile:tcompilerppufile);
  935. begin
  936. inherited ppuwrite(ppufile);
  937. ppufile.putasmsymbol(sym);
  938. ppufile.putlongint(size);
  939. ppufile.putbyte(byte(is_global));
  940. end;
  941. procedure tai_symbol.derefimpl;
  942. begin
  943. end;
  944. {****************************************************************************
  945. TAI_SYMBOL_END
  946. ****************************************************************************}
  947. constructor tai_symbol_end.Create(_sym:tasmsymbol);
  948. begin
  949. inherited Create;
  950. typ:=ait_symbol_end;
  951. sym:=_sym;
  952. end;
  953. constructor tai_symbol_end.Createname(const _name : string);
  954. begin
  955. inherited Create;
  956. typ:=ait_symbol_end;
  957. sym:=current_asmdata.RefAsmSymbol(_name);
  958. end;
  959. constructor tai_symbol_end.ppuload(t:taitype;ppufile:tcompilerppufile);
  960. begin
  961. inherited ppuload(t,ppufile);
  962. sym:=ppufile.getasmsymbol;
  963. end;
  964. procedure tai_symbol_end.ppuwrite(ppufile:tcompilerppufile);
  965. begin
  966. inherited ppuwrite(ppufile);
  967. ppufile.putasmsymbol(sym);
  968. end;
  969. procedure tai_symbol_end.derefimpl;
  970. begin
  971. end;
  972. {****************************************************************************
  973. TAI_SYMBOL_END
  974. ****************************************************************************}
  975. constructor tai_directive.Create(_directive:TAsmDirective;const _name:string);
  976. begin
  977. inherited Create;
  978. typ:=ait_directive;
  979. name:=stringdup(_name);
  980. directive:=_directive;
  981. end;
  982. destructor tai_directive.Destroy;
  983. begin
  984. stringdispose(name);
  985. end;
  986. constructor tai_directive.ppuload(t:taitype;ppufile:tcompilerppufile);
  987. begin
  988. inherited ppuload(t,ppufile);
  989. name:=stringdup(ppufile.getstring);
  990. directive:=TAsmDirective(ppufile.getbyte);
  991. end;
  992. procedure tai_directive.ppuwrite(ppufile:tcompilerppufile);
  993. begin
  994. inherited ppuwrite(ppufile);
  995. ppufile.putstring(name^);
  996. ppufile.putbyte(byte(directive));
  997. end;
  998. {****************************************************************************
  999. TAI_CONST
  1000. ****************************************************************************}
  1001. constructor tai_const.Create(_typ:taiconst_type;_value : int64);
  1002. begin
  1003. inherited Create;
  1004. typ:=ait_const;
  1005. consttype:=_typ;
  1006. value:=_value;
  1007. sym:=nil;
  1008. endsym:=nil;
  1009. end;
  1010. constructor tai_const.Create_128bit(_value : int64);
  1011. begin
  1012. inherited Create;
  1013. typ:=ait_const;
  1014. consttype:=aitconst_128bit;
  1015. value:=_value;
  1016. sym:=nil;
  1017. endsym:=nil;
  1018. end;
  1019. constructor tai_const.Create_64bit(_value : int64);
  1020. begin
  1021. inherited Create;
  1022. typ:=ait_const;
  1023. consttype:=aitconst_64bit;
  1024. value:=_value;
  1025. sym:=nil;
  1026. endsym:=nil;
  1027. end;
  1028. constructor tai_const.Create_32bit(_value : longint);
  1029. begin
  1030. inherited Create;
  1031. typ:=ait_const;
  1032. consttype:=aitconst_32bit;
  1033. value:=_value;
  1034. sym:=nil;
  1035. endsym:=nil;
  1036. end;
  1037. constructor tai_const.Create_16bit(_value : word);
  1038. begin
  1039. inherited Create;
  1040. typ:=ait_const;
  1041. consttype:=aitconst_16bit;
  1042. value:=_value;
  1043. sym:=nil;
  1044. endsym:=nil;
  1045. end;
  1046. constructor tai_const.Create_8bit(_value : byte);
  1047. begin
  1048. inherited Create;
  1049. typ:=ait_const;
  1050. consttype:=aitconst_8bit;
  1051. value:=_value;
  1052. sym:=nil;
  1053. endsym:=nil;
  1054. end;
  1055. constructor tai_const.Create_char(size: integer; _value: dword);
  1056. begin
  1057. inherited Create;
  1058. typ:=ait_const;
  1059. case size of
  1060. 1:
  1061. begin
  1062. consttype:=aitconst_8bit;
  1063. value:=byte(_value)
  1064. end;
  1065. 2:
  1066. begin
  1067. consttype:=aitconst_16bit;
  1068. value:=word(_value)
  1069. end
  1070. else
  1071. InternalError(2010030701)
  1072. end
  1073. end;
  1074. constructor tai_const.Create_sleb128bit(_value : int64);
  1075. begin
  1076. inherited Create;
  1077. typ:=ait_const;
  1078. consttype:=aitconst_sleb128bit;
  1079. value:=_value;
  1080. sym:=nil;
  1081. endsym:=nil;
  1082. end;
  1083. constructor tai_const.Create_uleb128bit(_value : qword);
  1084. begin
  1085. inherited Create;
  1086. typ:=ait_const;
  1087. consttype:=aitconst_uleb128bit;
  1088. value:=int64(_value);
  1089. sym:=nil;
  1090. endsym:=nil;
  1091. end;
  1092. constructor tai_const.Create_aint(_value : aint);
  1093. begin
  1094. inherited Create;
  1095. typ:=ait_const;
  1096. consttype:=aitconst_aint;
  1097. value:=_value;
  1098. sym:=nil;
  1099. endsym:=nil;
  1100. end;
  1101. constructor tai_const.Create_pint(_value : pint);
  1102. begin
  1103. inherited Create;
  1104. typ:=ait_const;
  1105. consttype:=aitconst_ptr;
  1106. value:=_value;
  1107. sym:=nil;
  1108. endsym:=nil;
  1109. end;
  1110. constructor tai_const.Create_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  1111. begin
  1112. inherited Create;
  1113. typ:=ait_const;
  1114. consttype:=_typ;
  1115. sym:=_sym;
  1116. endsym:=nil;
  1117. value:=0;
  1118. { update sym info }
  1119. if assigned(sym) then
  1120. sym.increfs;
  1121. end;
  1122. constructor tai_const.Create_sym(_sym:tasmsymbol);
  1123. begin
  1124. self.create_sym_offset(_sym,0);
  1125. end;
  1126. constructor tai_const.Create_sym_offset(_sym:tasmsymbol;ofs:aint);
  1127. begin
  1128. inherited Create;
  1129. typ:=ait_const;
  1130. consttype:=aitconst_ptr;
  1131. { sym is allowed to be nil, this is used to write nil pointers }
  1132. sym:=_sym;
  1133. endsym:=nil;
  1134. { store the original offset in symofs so that we can recalculate the
  1135. value field in the assembler }
  1136. symofs:=ofs;
  1137. value:=ofs;
  1138. { update sym info }
  1139. if assigned(sym) then
  1140. sym.increfs;
  1141. end;
  1142. constructor tai_const.Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  1143. begin
  1144. self.create_sym_offset(_sym,0);
  1145. consttype:=_typ;
  1146. endsym:=_endsym;
  1147. endsym.increfs;
  1148. end;
  1149. constructor tai_const.Create_rva_sym(_sym:tasmsymbol);
  1150. begin
  1151. self.create_sym_offset(_sym,0);
  1152. consttype:=aitconst_rva_symbol;
  1153. end;
  1154. constructor tai_const.Createname(const name:string;ofs:aint);
  1155. begin
  1156. self.create_sym_offset(current_asmdata.RefAsmSymbol(name),ofs);
  1157. end;
  1158. constructor tai_const.ppuload(t:taitype;ppufile:tcompilerppufile);
  1159. begin
  1160. inherited ppuload(t,ppufile);
  1161. consttype:=taiconst_type(ppufile.getbyte);
  1162. sym:=ppufile.getasmsymbol;
  1163. endsym:=ppufile.getasmsymbol;
  1164. value:=ppufile.getint64;
  1165. end;
  1166. procedure tai_const.ppuwrite(ppufile:tcompilerppufile);
  1167. begin
  1168. inherited ppuwrite(ppufile);
  1169. ppufile.putbyte(byte(consttype));
  1170. ppufile.putasmsymbol(sym);
  1171. ppufile.putasmsymbol(endsym);
  1172. ppufile.putint64(value);
  1173. end;
  1174. procedure tai_const.derefimpl;
  1175. begin
  1176. end;
  1177. function tai_const.getcopy:tlinkedlistitem;
  1178. begin
  1179. getcopy:=inherited getcopy;
  1180. { we need to increase the reference number }
  1181. sym.increfs;
  1182. if assigned(endsym) then
  1183. endsym.increfs;
  1184. end;
  1185. function tai_const.size:longint;
  1186. begin
  1187. case consttype of
  1188. aitconst_8bit :
  1189. result:=1;
  1190. aitconst_16bit :
  1191. result:=2;
  1192. aitconst_32bit,aitconst_darwin_dwarf_delta32:
  1193. result:=4;
  1194. aitconst_64bit,aitconst_darwin_dwarf_delta64:
  1195. result:=8;
  1196. aitconst_secrel32_symbol,
  1197. aitconst_rva_symbol :
  1198. if target_info.system=system_x86_64_win64 then
  1199. result:=sizeof(longint)
  1200. else
  1201. result:=sizeof(pint);
  1202. aitconst_uleb128bit :
  1203. result:=LengthUleb128(qword(value));
  1204. aitconst_sleb128bit :
  1205. result:=LengthSleb128(value);
  1206. aitconst_half16bit:
  1207. result:=2;
  1208. else
  1209. internalerror(200603253);
  1210. end;
  1211. end;
  1212. {****************************************************************************
  1213. TAI_real_32bit
  1214. ****************************************************************************}
  1215. constructor tai_real_32bit.Create(_value : ts32real);
  1216. begin
  1217. inherited Create;
  1218. typ:=ait_real_32bit;
  1219. value:=_value;
  1220. end;
  1221. constructor tai_real_32bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1222. begin
  1223. inherited ppuload(t,ppufile);
  1224. value:=ppufile.getreal;
  1225. end;
  1226. procedure tai_real_32bit.ppuwrite(ppufile:tcompilerppufile);
  1227. begin
  1228. inherited ppuwrite(ppufile);
  1229. ppufile.putreal(value);
  1230. end;
  1231. {****************************************************************************
  1232. TAI_real_64bit
  1233. ****************************************************************************}
  1234. constructor tai_real_64bit.Create(_value : ts64real);
  1235. begin
  1236. inherited Create;
  1237. typ:=ait_real_64bit;
  1238. value:=_value;
  1239. end;
  1240. {$ifdef ARM}
  1241. constructor tai_real_64bit.Create_hiloswapped(_value : ts64real);
  1242. begin
  1243. inherited Create;
  1244. typ:=ait_real_64bit;
  1245. value:=_value;
  1246. formatoptions:=fo_hiloswapped;
  1247. end;
  1248. {$endif ARM}
  1249. constructor tai_real_64bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1250. begin
  1251. inherited ppuload(t,ppufile);
  1252. value:=ppufile.getreal;
  1253. {$ifdef ARM}
  1254. formatoptions:=tformatoptions(ppufile.getbyte);
  1255. {$endif ARM}
  1256. end;
  1257. procedure tai_real_64bit.ppuwrite(ppufile:tcompilerppufile);
  1258. begin
  1259. inherited ppuwrite(ppufile);
  1260. ppufile.putreal(value);
  1261. {$ifdef ARM}
  1262. ppufile.putbyte(byte(formatoptions));
  1263. {$endif ARM}
  1264. end;
  1265. {****************************************************************************
  1266. TAI_real_80bit
  1267. ****************************************************************************}
  1268. constructor tai_real_80bit.Create(_value : ts80real; _savesize: byte);
  1269. begin
  1270. inherited Create;
  1271. typ:=ait_real_80bit;
  1272. value:=_value;
  1273. savesize:=_savesize;
  1274. end;
  1275. constructor tai_real_80bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1276. begin
  1277. inherited ppuload(t,ppufile);
  1278. value:=ppufile.getreal;
  1279. savesize:=ppufile.getbyte;
  1280. end;
  1281. procedure tai_real_80bit.ppuwrite(ppufile:tcompilerppufile);
  1282. begin
  1283. inherited ppuwrite(ppufile);
  1284. ppufile.putreal(value);
  1285. ppufile.putbyte(savesize);
  1286. end;
  1287. {****************************************************************************
  1288. TAI_real_80bit
  1289. ****************************************************************************}
  1290. constructor tai_real_128bit.Create(_value : ts128real);
  1291. begin
  1292. inherited Create;
  1293. typ:=ait_real_128bit;
  1294. value:=_value;
  1295. end;
  1296. constructor tai_real_128bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1297. begin
  1298. inherited ppuload(t,ppufile);
  1299. value:=ppufile.getreal;
  1300. end;
  1301. procedure tai_real_128bit.ppuwrite(ppufile:tcompilerppufile);
  1302. begin
  1303. inherited ppuwrite(ppufile);
  1304. ppufile.putreal(value);
  1305. end;
  1306. {****************************************************************************
  1307. Tai_comp_64bit
  1308. ****************************************************************************}
  1309. constructor tai_comp_64bit.Create(_value : ts64comp);
  1310. begin
  1311. inherited Create;
  1312. typ:=ait_comp_64bit;
  1313. value:=_value;
  1314. end;
  1315. constructor tai_comp_64bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1316. begin
  1317. inherited ppuload(t,ppufile);
  1318. ppufile.putdata(value,sizeof(value));
  1319. end;
  1320. procedure tai_comp_64bit.ppuwrite(ppufile:tcompilerppufile);
  1321. begin
  1322. inherited ppuwrite(ppufile);
  1323. ppufile.getdata(value,sizeof(value));
  1324. end;
  1325. {****************************************************************************
  1326. TAI_STRING
  1327. ****************************************************************************}
  1328. constructor tai_string.Create(const _str : string);
  1329. begin
  1330. inherited Create;
  1331. typ:=ait_string;
  1332. len:=length(_str);
  1333. getmem(str,len+1);
  1334. strpcopy(str,_str);
  1335. end;
  1336. constructor tai_string.Create_pchar(_str : pchar;length : longint);
  1337. begin
  1338. inherited Create;
  1339. typ:=ait_string;
  1340. str:=_str;
  1341. len:=length;
  1342. end;
  1343. destructor tai_string.destroy;
  1344. begin
  1345. if str<>nil then
  1346. freemem(str);
  1347. inherited Destroy;
  1348. end;
  1349. constructor tai_string.ppuload(t:taitype;ppufile:tcompilerppufile);
  1350. begin
  1351. inherited ppuload(t,ppufile);
  1352. len:=ppufile.getlongint;
  1353. getmem(str,len);
  1354. ppufile.getdata(str^,len);
  1355. end;
  1356. procedure tai_string.ppuwrite(ppufile:tcompilerppufile);
  1357. begin
  1358. inherited ppuwrite(ppufile);
  1359. ppufile.putlongint(len);
  1360. ppufile.putdata(str^,len);
  1361. end;
  1362. function tai_string.getcopy : tlinkedlistitem;
  1363. var
  1364. p : tlinkedlistitem;
  1365. begin
  1366. p:=inherited getcopy;
  1367. getmem(tai_string(p).str,len);
  1368. move(str^,tai_string(p).str^,len);
  1369. getcopy:=p;
  1370. end;
  1371. {****************************************************************************
  1372. TAI_LABEL
  1373. ****************************************************************************}
  1374. constructor tai_label.create(_labsym : tasmlabel);
  1375. begin
  1376. inherited Create;
  1377. typ:=ait_label;
  1378. labsym:=_labsym;
  1379. labsym.is_set:=true;
  1380. is_global:=(labsym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN]);
  1381. end;
  1382. constructor tai_label.ppuload(t:taitype;ppufile:tcompilerppufile);
  1383. begin
  1384. inherited ppuload(t,ppufile);
  1385. labsym:=tasmlabel(ppufile.getasmsymbol);
  1386. is_global:=boolean(ppufile.getbyte);
  1387. end;
  1388. procedure tai_label.ppuwrite(ppufile:tcompilerppufile);
  1389. begin
  1390. inherited ppuwrite(ppufile);
  1391. ppufile.putasmsymbol(labsym);
  1392. ppufile.putbyte(byte(is_global));
  1393. end;
  1394. procedure tai_label.derefimpl;
  1395. begin
  1396. labsym.is_set:=true;
  1397. end;
  1398. {****************************************************************************
  1399. tai_comment comment to be inserted in the assembler file
  1400. ****************************************************************************}
  1401. constructor tai_comment.Create(_str : pchar);
  1402. begin
  1403. inherited Create;
  1404. typ:=ait_comment;
  1405. str:=_str;
  1406. end;
  1407. destructor tai_comment.destroy;
  1408. begin
  1409. freemem(str);
  1410. inherited Destroy;
  1411. end;
  1412. constructor tai_comment.ppuload(t:taitype;ppufile:tcompilerppufile);
  1413. var
  1414. len : longint;
  1415. begin
  1416. inherited ppuload(t,ppufile);
  1417. len:=ppufile.getlongint;
  1418. getmem(str,len+1);
  1419. ppufile.getdata(str^,len);
  1420. str[len]:=#0;
  1421. end;
  1422. procedure tai_comment.ppuwrite(ppufile:tcompilerppufile);
  1423. var
  1424. len : longint;
  1425. begin
  1426. inherited ppuwrite(ppufile);
  1427. len:=strlen(str);
  1428. ppufile.putlongint(len);
  1429. ppufile.putdata(str^,len);
  1430. end;
  1431. function tai_comment.getcopy : tlinkedlistitem;
  1432. var
  1433. p : tlinkedlistitem;
  1434. begin
  1435. p:=inherited getcopy;
  1436. getmem(tai_comment(p).str,strlen(str)+1);
  1437. move(str^,tai_comment(p).str^,strlen(str)+1);
  1438. getcopy:=p;
  1439. end;
  1440. {****************************************************************************
  1441. TAI_STABS
  1442. ****************************************************************************}
  1443. constructor tai_stab.create(_stabtype:TStabType;_str : pchar);
  1444. begin
  1445. inherited create;
  1446. typ:=ait_stab;
  1447. str:=_str;
  1448. stabtype:=_stabtype;
  1449. end;
  1450. constructor tai_stab.create_str(_stabtype:TStabType;const s:string);
  1451. begin
  1452. self.create(_stabtype,strpnew(s));
  1453. end;
  1454. constructor tai_stab.create_ansistr(_stabtype:TStabType;const s:ansistring);
  1455. begin
  1456. inherited create;
  1457. typ:=ait_stab;
  1458. stabtype:=_stabtype;
  1459. getmem(str,length(s)+1);
  1460. move(s[1],str^,length(s)+1);
  1461. end;
  1462. destructor tai_stab.destroy;
  1463. begin
  1464. freemem(str);
  1465. inherited destroy;
  1466. end;
  1467. {****************************************************************************
  1468. TAI_FORCE_LINE
  1469. ****************************************************************************}
  1470. constructor tai_force_line.create;
  1471. begin
  1472. inherited create;
  1473. typ:=ait_force_line;
  1474. end;
  1475. {****************************************************************************
  1476. TAI_FUNCTION_NAME
  1477. ****************************************************************************}
  1478. constructor tai_function_name.create(const s:string);
  1479. begin
  1480. inherited create;
  1481. typ:=ait_function_name;
  1482. funcname:=stringdup(s);
  1483. end;
  1484. destructor tai_function_name.destroy;
  1485. begin
  1486. stringdispose(funcname);
  1487. inherited destroy;
  1488. end;
  1489. {****************************************************************************
  1490. TAI_CUTOBJECT
  1491. ****************************************************************************}
  1492. constructor tai_cutobject.Create;
  1493. begin
  1494. inherited Create;
  1495. typ:=ait_cutobject;
  1496. place:=cut_normal;
  1497. end;
  1498. constructor tai_cutobject.Create_begin;
  1499. begin
  1500. inherited Create;
  1501. typ:=ait_cutobject;
  1502. place:=cut_begin;
  1503. end;
  1504. constructor tai_cutobject.Create_end;
  1505. begin
  1506. inherited Create;
  1507. typ:=ait_cutobject;
  1508. place:=cut_end;
  1509. end;
  1510. constructor tai_cutobject.ppuload(t:taitype;ppufile:tcompilerppufile);
  1511. begin
  1512. inherited ppuload(t,ppufile);
  1513. place:=TCutPlace(ppufile.getbyte);
  1514. end;
  1515. procedure tai_cutobject.ppuwrite(ppufile:tcompilerppufile);
  1516. begin
  1517. inherited ppuwrite(ppufile);
  1518. ppufile.putbyte(byte(place));
  1519. end;
  1520. {****************************************************************************
  1521. Tai_Marker
  1522. ****************************************************************************}
  1523. constructor Tai_Marker.Create(_Kind: TAsmMarker);
  1524. begin
  1525. Inherited Create;
  1526. typ := ait_marker;
  1527. Kind := _Kind;
  1528. end;
  1529. constructor Tai_Marker.ppuload(t:taitype;ppufile:tcompilerppufile);
  1530. begin
  1531. inherited ppuload(t,ppufile);
  1532. kind:=TAsmMarker(ppufile.getbyte);
  1533. end;
  1534. procedure Tai_Marker.ppuwrite(ppufile:tcompilerppufile);
  1535. begin
  1536. inherited ppuwrite(ppufile);
  1537. ppufile.putbyte(byte(kind));
  1538. end;
  1539. {*****************************************************************************
  1540. tai_tempalloc
  1541. *****************************************************************************}
  1542. constructor tai_tempalloc.alloc(pos,size:longint);
  1543. begin
  1544. inherited Create;
  1545. typ:=ait_tempalloc;
  1546. allocation:=true;
  1547. temppos:=pos;
  1548. tempsize:=size;
  1549. {$ifdef EXTDEBUG}
  1550. problem:=nil;
  1551. {$endif EXTDEBUG}
  1552. end;
  1553. destructor tai_tempalloc.destroy;
  1554. begin
  1555. {$ifdef EXTDEBUG}
  1556. stringdispose(problem);
  1557. {$endif EXTDEBUG}
  1558. inherited destroy;
  1559. end;
  1560. constructor tai_tempalloc.dealloc(pos,size:longint);
  1561. begin
  1562. inherited Create;
  1563. typ:=ait_tempalloc;
  1564. allocation:=false;
  1565. temppos:=pos;
  1566. tempsize:=size;
  1567. {$ifdef EXTDEBUG}
  1568. problem:=nil;
  1569. {$endif EXTDEBUG}
  1570. end;
  1571. {$ifdef EXTDEBUG}
  1572. constructor tai_tempalloc.allocinfo(pos,size:longint;const st:string);
  1573. begin
  1574. inherited Create;
  1575. typ:=ait_tempalloc;
  1576. allocation:=false;
  1577. temppos:=pos;
  1578. tempsize:=size;
  1579. problem:=stringdup(st);
  1580. end;
  1581. {$endif EXTDEBUG}
  1582. constructor tai_tempalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  1583. begin
  1584. inherited ppuload(t,ppufile);
  1585. temppos:=ppufile.getlongint;
  1586. tempsize:=ppufile.getlongint;
  1587. allocation:=boolean(ppufile.getbyte);
  1588. {$ifdef EXTDEBUG}
  1589. problem:=nil;
  1590. {$endif EXTDEBUG}
  1591. end;
  1592. procedure tai_tempalloc.ppuwrite(ppufile:tcompilerppufile);
  1593. begin
  1594. inherited ppuwrite(ppufile);
  1595. ppufile.putlongint(temppos);
  1596. ppufile.putlongint(tempsize);
  1597. ppufile.putbyte(byte(allocation));
  1598. end;
  1599. {*****************************************************************************
  1600. tai_regalloc
  1601. *****************************************************************************}
  1602. constructor tai_regalloc.alloc(r : tregister;ainstr:tai);
  1603. begin
  1604. inherited create;
  1605. typ:=ait_regalloc;
  1606. ratype:=ra_alloc;
  1607. reg:=r;
  1608. { ainstr must be an instruction }
  1609. if assigned(ainstr) and
  1610. (ainstr.typ<>ait_instruction) then
  1611. internalerror(200411011);
  1612. instr:=ainstr;
  1613. end;
  1614. constructor tai_regalloc.dealloc(r : tregister;ainstr:tai);
  1615. begin
  1616. inherited create;
  1617. typ:=ait_regalloc;
  1618. ratype:=ra_dealloc;
  1619. reg:=r;
  1620. { ainstr must be an instruction }
  1621. if assigned(ainstr) and
  1622. (ainstr.typ<>ait_instruction) then
  1623. internalerror(200411012);
  1624. instr:=ainstr;
  1625. end;
  1626. constructor tai_regalloc.sync(r : tregister);
  1627. begin
  1628. inherited create;
  1629. typ:=ait_regalloc;
  1630. ratype:=ra_sync;
  1631. reg:=r;
  1632. end;
  1633. constructor tai_regalloc.resize(r : tregister);
  1634. begin
  1635. inherited create;
  1636. typ:=ait_regalloc;
  1637. ratype:=ra_resize;
  1638. reg:=r;
  1639. end;
  1640. constructor tai_regalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  1641. begin
  1642. inherited ppuload(t,ppufile);
  1643. ppufile.getdata(reg,sizeof(Tregister));
  1644. ratype:=tregalloctype(ppufile.getbyte);
  1645. end;
  1646. procedure tai_regalloc.ppuwrite(ppufile:tcompilerppufile);
  1647. begin
  1648. inherited ppuwrite(ppufile);
  1649. ppufile.putdata(reg,sizeof(Tregister));
  1650. ppufile.putbyte(byte(ratype));
  1651. end;
  1652. {*****************************************************************************
  1653. TaiInstruction
  1654. *****************************************************************************}
  1655. constructor tai_cpu_abstract.Create(op : tasmop);
  1656. begin
  1657. inherited create;
  1658. typ:=ait_instruction;
  1659. is_jmp:=false;
  1660. opcode:=op;
  1661. ops:=0;
  1662. fillchar(condition,sizeof(condition),0);
  1663. fillchar(oper,sizeof(oper),0);
  1664. end;
  1665. destructor tai_cpu_abstract.Destroy;
  1666. var
  1667. i : integer;
  1668. begin
  1669. for i:=0 to opercnt-1 do
  1670. freeop(i);
  1671. inherited destroy;
  1672. end;
  1673. { ---------------------------------------------------------------------
  1674. Loading of operands.
  1675. ---------------------------------------------------------------------}
  1676. procedure tai_cpu_abstract.allocate_oper(opers:longint);
  1677. begin
  1678. while (opers>opercnt) do
  1679. begin
  1680. new(oper[opercnt]);
  1681. fillchar(oper[opercnt]^,sizeof(toper),0);
  1682. inc(opercnt);
  1683. end;
  1684. end;
  1685. procedure tai_cpu_abstract.loadconst(opidx:longint;l:aint);
  1686. begin
  1687. allocate_oper(opidx+1);
  1688. with oper[opidx]^ do
  1689. begin
  1690. if typ<>top_const then
  1691. clearop(opidx);
  1692. val:=l;
  1693. typ:=top_const;
  1694. end;
  1695. end;
  1696. procedure tai_cpu_abstract.loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  1697. var
  1698. r : treference;
  1699. begin
  1700. reference_reset_symbol(r,s,sofs,1);
  1701. r.refaddr:=addr_full;
  1702. loadref(opidx,r);
  1703. end;
  1704. procedure tai_cpu_abstract.loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset,forceref:boolean);
  1705. begin
  1706. if not assigned(s) then
  1707. internalerror(200204251);
  1708. allocate_oper(opidx+1);
  1709. with oper[opidx]^ do
  1710. begin
  1711. if typ<>top_local then
  1712. begin
  1713. clearop(opidx);
  1714. new(localoper);
  1715. end;
  1716. with oper[opidx]^.localoper^ do
  1717. begin
  1718. localsym:=s;
  1719. localsymofs:=sofs;
  1720. localindexreg:=indexreg;
  1721. localscale:=scale;
  1722. localgetoffset:=getoffset;
  1723. localforceref:=forceref;
  1724. end;
  1725. typ:=top_local;
  1726. end;
  1727. end;
  1728. procedure tai_cpu_abstract.loadref(opidx:longint;const r:treference);
  1729. begin
  1730. allocate_oper(opidx+1);
  1731. with oper[opidx]^ do
  1732. begin
  1733. if typ<>top_ref then
  1734. begin
  1735. clearop(opidx);
  1736. new(ref);
  1737. end;
  1738. ref^:=r;
  1739. {$ifdef x86}
  1740. { We allow this exception for x86, since overloading this would be
  1741. too much of a a speed penalty}
  1742. if (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  1743. segprefix:=ref^.segment;
  1744. {$endif}
  1745. if (cs_create_pic in current_settings.moduleswitches) and
  1746. assigned(r.symbol) and
  1747. not assigned(r.relsymbol) and
  1748. (r.refaddr=addr_no)
  1749. {$ifdef ARM}
  1750. and not(r.base=NR_R15)
  1751. {$endif ARM}
  1752. then
  1753. internalerror(200502052);
  1754. typ:=top_ref;
  1755. if assigned(add_reg_instruction_hook) then
  1756. begin
  1757. add_reg_instruction_hook(self,ref^.base);
  1758. add_reg_instruction_hook(self,ref^.index);
  1759. end;
  1760. { mark symbol as used }
  1761. if assigned(ref^.symbol) then
  1762. ref^.symbol.increfs;
  1763. if assigned(ref^.relsymbol) then
  1764. ref^.relsymbol.increfs;
  1765. end;
  1766. end;
  1767. procedure tai_cpu_abstract.loadreg(opidx:longint;r:tregister);
  1768. begin
  1769. allocate_oper(opidx+1);
  1770. with oper[opidx]^ do
  1771. begin
  1772. if typ<>top_reg then
  1773. clearop(opidx);
  1774. reg:=r;
  1775. typ:=top_reg;
  1776. end;
  1777. if assigned(add_reg_instruction_hook) then
  1778. add_reg_instruction_hook(self,r);
  1779. {$ifdef ARM}
  1780. { R15 is the PC on the ARM thus moves to R15 are jumps.
  1781. Due to speed considerations we don't use a virtual overridden method here.
  1782. Because the pc/r15 isn't handled by the reg. allocator this should never cause
  1783. problems with iregs getting r15.
  1784. }
  1785. is_jmp:=(opcode=A_MOV) and (opidx=0) and (r=NR_R15);
  1786. {$endif ARM}
  1787. end;
  1788. procedure tai_cpu_abstract.loadoper(opidx:longint;o:toper);
  1789. begin
  1790. allocate_oper(opidx+1);
  1791. clearop(opidx);
  1792. oper[opidx]^:=o;
  1793. { copy also the reference }
  1794. with oper[opidx]^ do
  1795. begin
  1796. case typ of
  1797. top_reg:
  1798. begin
  1799. if assigned(add_reg_instruction_hook) then
  1800. add_reg_instruction_hook(self,reg);
  1801. end;
  1802. top_ref:
  1803. begin
  1804. new(ref);
  1805. ref^:=o.ref^;
  1806. if assigned(add_reg_instruction_hook) then
  1807. begin
  1808. add_reg_instruction_hook(self,ref^.base);
  1809. add_reg_instruction_hook(self,ref^.index);
  1810. end;
  1811. end;
  1812. {$ifdef ARM}
  1813. top_shifterop:
  1814. begin
  1815. new(shifterop);
  1816. shifterop^:=o.shifterop^;
  1817. if assigned(add_reg_instruction_hook) then
  1818. add_reg_instruction_hook(self,shifterop^.rs);
  1819. end;
  1820. {$endif ARM}
  1821. end;
  1822. end;
  1823. end;
  1824. procedure tai_cpu_abstract.clearop(opidx:longint);
  1825. begin
  1826. with oper[opidx]^ do
  1827. begin
  1828. case typ of
  1829. top_ref:
  1830. dispose(ref);
  1831. top_local:
  1832. dispose(localoper);
  1833. {$ifdef ARM}
  1834. top_shifterop:
  1835. dispose(shifterop);
  1836. top_regset:
  1837. dispose(regset);
  1838. {$endif ARM}
  1839. end;
  1840. typ:=top_none;
  1841. end;
  1842. end;
  1843. procedure tai_cpu_abstract.freeop(opidx:longint);
  1844. begin
  1845. clearop(opidx);
  1846. dispose(oper[opidx]);
  1847. end;
  1848. { ---------------------------------------------------------------------
  1849. Miscellaneous methods.
  1850. ---------------------------------------------------------------------}
  1851. procedure tai_cpu_abstract.SetCondition(const c:TAsmCond);
  1852. begin
  1853. condition:=c;
  1854. end;
  1855. Function tai_cpu_abstract.getcopy:TLinkedListItem;
  1856. var
  1857. i : longint;
  1858. p : tai_cpu_abstract;
  1859. begin
  1860. p:=tai_cpu_abstract(inherited getcopy);
  1861. { make a copy of the references }
  1862. p.opercnt:=0;
  1863. p.allocate_oper(ops);
  1864. for i:=0 to ops-1 do
  1865. begin
  1866. p.oper[i]^:=oper[i]^;
  1867. case oper[i]^.typ of
  1868. top_local :
  1869. begin
  1870. new(p.oper[i]^.localoper);
  1871. p.oper[i]^.localoper^:=oper[i]^.localoper^;
  1872. end;
  1873. top_ref :
  1874. begin
  1875. new(p.oper[i]^.ref);
  1876. p.oper[i]^.ref^:=oper[i]^.ref^;
  1877. end;
  1878. {$ifdef ARM}
  1879. top_shifterop:
  1880. begin
  1881. new(p.oper[i]^.shifterop);
  1882. p.oper[i]^.shifterop^:=oper[i]^.shifterop^;
  1883. end;
  1884. {$endif ARM}
  1885. end;
  1886. end;
  1887. getcopy:=p;
  1888. end;
  1889. function tai_cpu_abstract.is_same_reg_move(regtype: Tregistertype):boolean;
  1890. begin
  1891. { When the generic RA is used this needs to be overridden, we don't use
  1892. virtual;abstract; to prevent a lot of warnings of unimplemented abstract methods
  1893. when tai_cpu is created (PFV) }
  1894. internalerror(200404091);
  1895. result:=false;
  1896. end;
  1897. function tai_cpu_abstract.spilling_get_operation_type(opnr: longint): topertype;
  1898. begin
  1899. internalerror(200404091);
  1900. result:=operand_readwrite;
  1901. end;
  1902. function tai_cpu_abstract.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
  1903. begin
  1904. result := operand_read;
  1905. end;
  1906. constructor tai_cpu_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  1907. var
  1908. i : integer;
  1909. begin
  1910. inherited ppuload(t,ppufile);
  1911. { hopefully, we don't get problems with big/litte endian here when cross compiling :/ }
  1912. ppufile.getdata(condition,sizeof(tasmcond));
  1913. allocate_oper(ppufile.getbyte);
  1914. for i:=0 to ops-1 do
  1915. ppuloadoper(ppufile,oper[i]^);
  1916. opcode:=tasmop(ppufile.getword);
  1917. {$ifdef x86}
  1918. ppufile.getdata(segprefix,sizeof(Tregister));
  1919. {$endif x86}
  1920. is_jmp:=boolean(ppufile.getbyte);
  1921. end;
  1922. procedure tai_cpu_abstract.ppuwrite(ppufile:tcompilerppufile);
  1923. var
  1924. i : integer;
  1925. begin
  1926. inherited ppuwrite(ppufile);
  1927. ppufile.putdata(condition,sizeof(tasmcond));
  1928. ppufile.putbyte(ops);
  1929. for i:=0 to ops-1 do
  1930. ppuwriteoper(ppufile,oper[i]^);
  1931. ppufile.putword(word(opcode));
  1932. {$ifdef x86}
  1933. ppufile.putdata(segprefix,sizeof(Tregister));
  1934. {$endif x86}
  1935. ppufile.putbyte(byte(is_jmp));
  1936. end;
  1937. procedure tai_cpu_abstract.buildderefimpl;
  1938. var
  1939. i : integer;
  1940. begin
  1941. for i:=0 to ops-1 do
  1942. ppubuildderefimploper(oper[i]^);
  1943. end;
  1944. procedure tai_cpu_abstract.derefimpl;
  1945. var
  1946. i : integer;
  1947. begin
  1948. for i:=0 to ops-1 do
  1949. ppuderefoper(oper[i]^);
  1950. end;
  1951. procedure tai_cpu_abstract.resetpass1;
  1952. begin
  1953. end;
  1954. procedure tai_cpu_abstract.resetpass2;
  1955. begin
  1956. end;
  1957. function tai_cpu_abstract.Pass1(objdata:TObjData):longint;
  1958. begin
  1959. result:=0;
  1960. end;
  1961. procedure tai_cpu_abstract.Pass2(objdata:TObjData);
  1962. begin
  1963. end;
  1964. procedure tai_cpu_abstract.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  1965. begin
  1966. o.typ:=toptype(ppufile.getbyte);
  1967. o.ot:=ppufile.getlongint;
  1968. case o.typ of
  1969. top_reg :
  1970. ppufile.getdata(o.reg,sizeof(Tregister));
  1971. top_ref :
  1972. begin
  1973. new(o.ref);
  1974. {$ifdef x86}
  1975. ppufile.getdata(o.ref^.segment,sizeof(Tregister));
  1976. {$endif x86}
  1977. ppufile.getdata(o.ref^.base,sizeof(Tregister));
  1978. ppufile.getdata(o.ref^.index,sizeof(Tregister));
  1979. ppufile.getdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));
  1980. o.ref^.scalefactor:=ppufile.getbyte;
  1981. o.ref^.offset:=ppufile.getaint;
  1982. o.ref^.symbol:=ppufile.getasmsymbol;
  1983. o.ref^.relsymbol:=ppufile.getasmsymbol;
  1984. end;
  1985. top_const :
  1986. o.val:=ppufile.getaint;
  1987. top_local :
  1988. begin
  1989. new(o.localoper);
  1990. with o.localoper^ do
  1991. begin
  1992. ppufile.getderef(localsymderef);
  1993. localsymofs:=ppufile.getaint;
  1994. localindexreg:=tregister(ppufile.getlongint);
  1995. localscale:=ppufile.getbyte;
  1996. localgetoffset:=(ppufile.getbyte<>0);
  1997. end;
  1998. end;
  1999. else
  2000. internalerror(2007010210);
  2001. end;
  2002. end;
  2003. procedure tai_cpu_abstract.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  2004. begin
  2005. ppufile.putbyte(byte(o.typ));
  2006. ppufile.putlongint(o.ot);
  2007. case o.typ of
  2008. top_reg :
  2009. ppufile.putdata(o.reg,sizeof(Tregister));
  2010. top_ref :
  2011. begin
  2012. {$ifdef x86}
  2013. ppufile.putdata(o.ref^.segment,sizeof(Tregister));
  2014. {$endif x86}
  2015. ppufile.putdata(o.ref^.base,sizeof(Tregister));
  2016. ppufile.putdata(o.ref^.index,sizeof(Tregister));
  2017. ppufile.putdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));
  2018. ppufile.putbyte(o.ref^.scalefactor);
  2019. ppufile.putaint(o.ref^.offset);
  2020. ppufile.putasmsymbol(o.ref^.symbol);
  2021. ppufile.putasmsymbol(o.ref^.relsymbol);
  2022. end;
  2023. top_const :
  2024. ppufile.putaint(o.val);
  2025. top_local :
  2026. begin
  2027. with o.localoper^ do
  2028. begin
  2029. ppufile.putderef(localsymderef);
  2030. ppufile.putaint(localsymofs);
  2031. ppufile.putlongint(longint(localindexreg));
  2032. ppufile.putbyte(localscale);
  2033. ppufile.putbyte(byte(localgetoffset));
  2034. end;
  2035. end;
  2036. else
  2037. internalerror(2007010211);
  2038. end;
  2039. end;
  2040. {****************************************************************************
  2041. tai_align_abstract
  2042. ****************************************************************************}
  2043. constructor tai_align_abstract.Create(b: byte);
  2044. begin
  2045. inherited Create;
  2046. typ:=ait_align;
  2047. {$ifdef EXTDEBUG}
  2048. if upper(classname)='TAI_ALIGN_ABSTRACT' then
  2049. internalerror(200709191);
  2050. {$endif EXTDEBUG}
  2051. if b in [1,2,4,8,16,32] then
  2052. aligntype := b
  2053. else
  2054. aligntype := 1;
  2055. fillsize:=0;
  2056. fillop:=0;
  2057. use_op:=false;
  2058. end;
  2059. constructor tai_align_abstract.Create_op(b: byte; _op: byte);
  2060. begin
  2061. inherited Create;
  2062. typ:=ait_align;
  2063. if b in [1,2,4,8,16,32] then
  2064. aligntype := b
  2065. else
  2066. aligntype := 1;
  2067. fillsize:=0;
  2068. fillop:=_op;
  2069. use_op:=true;
  2070. end;
  2071. constructor tai_align_abstract.Create_zeros(b: byte);
  2072. begin
  2073. inherited Create;
  2074. typ:=ait_align;
  2075. if b in [1,2,4,8,16,32] then
  2076. aligntype := b
  2077. else
  2078. aligntype := 1;
  2079. use_op:=true;
  2080. fillsize:=0;
  2081. fillop:=0;
  2082. end;
  2083. function tai_align_abstract.calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;
  2084. begin
  2085. if fillsize>sizeof(buf) then
  2086. internalerror(200404293);
  2087. fillchar(buf,high(buf),fillop);
  2088. calculatefillbuf:=pchar(@buf);
  2089. end;
  2090. constructor tai_align_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  2091. begin
  2092. inherited ppuload(t,ppufile);
  2093. aligntype:=ppufile.getbyte;
  2094. fillsize:=0;
  2095. fillop:=ppufile.getbyte;
  2096. use_op:=boolean(ppufile.getbyte);
  2097. end;
  2098. procedure tai_align_abstract.ppuwrite(ppufile:tcompilerppufile);
  2099. begin
  2100. inherited ppuwrite(ppufile);
  2101. ppufile.putbyte(aligntype);
  2102. ppufile.putbyte(fillop);
  2103. ppufile.putbyte(byte(use_op));
  2104. end;
  2105. {****************************************************************************
  2106. tai_seh_directive
  2107. ****************************************************************************}
  2108. const
  2109. datatypemap: array[TAsmSehDirective] of TSehDirectiveDatatype=(
  2110. sd_string, { proc }
  2111. sd_none, { endproc }
  2112. sd_none, { endprologue }
  2113. sd_string, { handler }
  2114. sd_none, { handlerdata }
  2115. sd_none,sd_none,sd_none, { eh, 32, no32 }
  2116. sd_regoffset, { setframe }
  2117. sd_offset, { stackalloc }
  2118. sd_reg, { pushreg }
  2119. sd_regoffset, { savereg }
  2120. sd_regoffset, { savexmm }
  2121. sd_none { pushframe }
  2122. );
  2123. constructor tai_seh_directive.create(_kind:TAsmSehDirective);
  2124. begin
  2125. inherited Create;
  2126. typ:=ait_seh_directive;
  2127. kind:=_kind;
  2128. data.typ:=datatypemap[_kind];
  2129. end;
  2130. constructor tai_seh_directive.create_name(_kind:TAsmSehDirective;const _name:string);
  2131. begin
  2132. create(_kind);
  2133. data.name:=stringdup(_name);
  2134. end;
  2135. constructor tai_seh_directive.create_reg(_kind:TAsmSehDirective;r:TRegister);
  2136. begin
  2137. create(_kind);
  2138. data.reg:=r;
  2139. end;
  2140. constructor tai_seh_directive.create_offset(_kind:TAsmSehDirective;ofs:dword);
  2141. begin
  2142. create(_kind);
  2143. data.offset:=ofs;
  2144. end;
  2145. constructor tai_seh_directive.create_reg_offset(_kind:TAsmSehDirective;
  2146. r:TRegister;ofs:dword);
  2147. begin
  2148. create(_kind);
  2149. data.offset:=ofs;
  2150. data.reg:=r;
  2151. end;
  2152. constructor tai_seh_directive.ppuload(t:taitype;ppufile:tcompilerppufile);
  2153. begin
  2154. inherited ppuload(t, ppufile);
  2155. kind:=TAsmSehDirective(ppufile.getbyte);
  2156. data.typ:=datatypemap[kind];
  2157. case data.typ of
  2158. sd_none: ;
  2159. sd_string:
  2160. begin
  2161. data.name:=stringdup(ppufile.getstring);
  2162. data.flags:=ppufile.getbyte;
  2163. end;
  2164. sd_reg,sd_offset,sd_regoffset:
  2165. begin
  2166. ppufile.getdata(data.reg,sizeof(TRegister));
  2167. data.offset:=ppufile.getdword;
  2168. end;
  2169. else
  2170. InternalError(2011091201);
  2171. end;
  2172. end;
  2173. destructor tai_seh_directive.destroy;
  2174. begin
  2175. if data.typ=sd_string then
  2176. stringdispose(data.name);
  2177. inherited destroy;
  2178. end;
  2179. procedure tai_seh_directive.ppuwrite(ppufile:tcompilerppufile);
  2180. begin
  2181. inherited ppuwrite(ppufile);
  2182. ppufile.putbyte(ord(kind));
  2183. case data.typ of
  2184. sd_none: ;
  2185. sd_string:
  2186. begin
  2187. ppufile.putstring(data.name^);
  2188. ppufile.putbyte(data.flags);
  2189. end;
  2190. sd_reg,sd_offset,sd_regoffset:
  2191. begin
  2192. ppufile.putdata(data.reg,sizeof(TRegister));
  2193. ppufile.putdword(data.offset);
  2194. end;
  2195. else
  2196. InternalError(2011091202);
  2197. end;
  2198. end;
  2199. procedure tai_seh_directive.generate_code(objdata:TObjData);
  2200. begin
  2201. end;
  2202. begin
  2203. { taitype should fit into a 4 byte set for speed reasons }
  2204. if ord(high(taitype))>31 then
  2205. internalerror(201108181);
  2206. end.