aasmtai.pas 68 KB

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