aasmtai.pas 72 KB

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