aasmtai.pas 69 KB

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