aasmtai.pas 71 KB

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