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;
  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. l : tasmlabel;
  288. constructor Create(_l : 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 : TAsmSection; { 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(offset:longint):longint;virtual;abstract;
  528. procedure Pass2(objdata:TAsmObjectdata);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. objectlibrary.DerefAsmsymbol(sym);
  835. end;
  836. {****************************************************************************
  837. TAI_SYMBOL
  838. ****************************************************************************}
  839. constructor tai_symbol.Create(_sym:tasmsymbol;siz:longint);
  840. begin
  841. inherited Create;
  842. typ:=ait_symbol;
  843. sym:=_sym;
  844. size:=siz;
  845. sym.defbind:=AB_LOCAL;
  846. is_global:=false;
  847. end;
  848. constructor tai_symbol.Create_global(_sym:tasmsymbol;siz:longint);
  849. begin
  850. inherited Create;
  851. typ:=ait_symbol;
  852. sym:=_sym;
  853. size:=siz;
  854. sym.defbind:=AB_GLOBAL;
  855. is_global:=true;
  856. end;
  857. constructor tai_symbol.Createname(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  858. begin
  859. inherited Create;
  860. typ:=ait_symbol;
  861. sym:=objectlibrary.newasmsymbol(_name,AB_LOCAL,_symtyp);
  862. size:=siz;
  863. is_global:=false;
  864. end;
  865. constructor tai_symbol.Createname_global(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  866. begin
  867. inherited Create;
  868. typ:=ait_symbol;
  869. sym:=objectlibrary.newasmsymbol(_name,AB_GLOBAL,_symtyp);
  870. size:=siz;
  871. is_global:=true;
  872. end;
  873. constructor tai_symbol.ppuload(t:taitype;ppufile:tcompilerppufile);
  874. begin
  875. inherited ppuload(t,ppufile);
  876. sym:=ppufile.getasmsymbol;
  877. size:=ppufile.getlongint;
  878. is_global:=boolean(ppufile.getbyte);
  879. end;
  880. procedure tai_symbol.ppuwrite(ppufile:tcompilerppufile);
  881. begin
  882. inherited ppuwrite(ppufile);
  883. ppufile.putasmsymbol(sym);
  884. ppufile.putlongint(size);
  885. ppufile.putbyte(byte(is_global));
  886. end;
  887. procedure tai_symbol.derefimpl;
  888. begin
  889. objectlibrary.DerefAsmsymbol(sym);
  890. end;
  891. {****************************************************************************
  892. TAI_SYMBOL_END
  893. ****************************************************************************}
  894. constructor tai_symbol_end.Create(_sym:tasmsymbol);
  895. begin
  896. inherited Create;
  897. typ:=ait_symbol_end;
  898. sym:=_sym;
  899. end;
  900. constructor tai_symbol_end.Createname(const _name : string);
  901. begin
  902. inherited Create;
  903. typ:=ait_symbol_end;
  904. sym:=objectlibrary.newasmsymbol(_name,AB_GLOBAL,AT_NONE);
  905. end;
  906. constructor tai_symbol_end.ppuload(t:taitype;ppufile:tcompilerppufile);
  907. begin
  908. inherited ppuload(t,ppufile);
  909. sym:=ppufile.getasmsymbol;
  910. end;
  911. procedure tai_symbol_end.ppuwrite(ppufile:tcompilerppufile);
  912. begin
  913. inherited ppuwrite(ppufile);
  914. ppufile.putasmsymbol(sym);
  915. end;
  916. procedure tai_symbol_end.derefimpl;
  917. begin
  918. objectlibrary.DerefAsmsymbol(sym);
  919. end;
  920. {****************************************************************************
  921. TAI_SYMBOL_END
  922. ****************************************************************************}
  923. constructor tai_directive.Create(_directive:tasmdirective;const _name:string);
  924. begin
  925. inherited Create;
  926. typ:=ait_directive;
  927. name:=stringdup(_name);
  928. directive:=_directive;
  929. end;
  930. destructor tai_directive.Destroy;
  931. begin
  932. stringdispose(name);
  933. end;
  934. constructor tai_directive.ppuload(t:taitype;ppufile:tcompilerppufile);
  935. begin
  936. inherited ppuload(t,ppufile);
  937. name:=stringdup(ppufile.getstring);
  938. directive:=tasmdirective(ppufile.getbyte);
  939. end;
  940. procedure tai_directive.ppuwrite(ppufile:tcompilerppufile);
  941. begin
  942. inherited ppuwrite(ppufile);
  943. ppufile.putstring(name^);
  944. ppufile.putbyte(byte(directive));
  945. end;
  946. {****************************************************************************
  947. TAI_CONST
  948. ****************************************************************************}
  949. constructor tai_const.Create(_typ:taiconst_type;_value : int64);
  950. begin
  951. inherited Create;
  952. typ:=ait_const;
  953. consttype:=_typ;
  954. value:=_value;
  955. sym:=nil;
  956. endsym:=nil;
  957. end;
  958. constructor tai_const.Create_128bit(_value : int64);
  959. begin
  960. inherited Create;
  961. typ:=ait_const;
  962. consttype:=aitconst_128bit;
  963. value:=_value;
  964. sym:=nil;
  965. endsym:=nil;
  966. end;
  967. constructor tai_const.Create_64bit(_value : int64);
  968. begin
  969. inherited Create;
  970. typ:=ait_const;
  971. consttype:=aitconst_64bit;
  972. value:=_value;
  973. sym:=nil;
  974. endsym:=nil;
  975. end;
  976. constructor tai_const.Create_32bit(_value : longint);
  977. begin
  978. inherited Create;
  979. typ:=ait_const;
  980. consttype:=aitconst_32bit;
  981. value:=_value;
  982. sym:=nil;
  983. endsym:=nil;
  984. end;
  985. constructor tai_const.Create_16bit(_value : word);
  986. begin
  987. inherited Create;
  988. typ:=ait_const;
  989. consttype:=aitconst_16bit;
  990. value:=_value;
  991. sym:=nil;
  992. endsym:=nil;
  993. end;
  994. constructor tai_const.Create_8bit(_value : byte);
  995. begin
  996. inherited Create;
  997. typ:=ait_const;
  998. consttype:=aitconst_8bit;
  999. value:=_value;
  1000. sym:=nil;
  1001. endsym:=nil;
  1002. end;
  1003. constructor tai_const.Create_sleb128bit(_value : int64);
  1004. begin
  1005. inherited Create;
  1006. typ:=ait_const;
  1007. consttype:=aitconst_sleb128bit;
  1008. value:=_value;
  1009. sym:=nil;
  1010. endsym:=nil;
  1011. end;
  1012. constructor tai_const.Create_uleb128bit(_value : qword);
  1013. begin
  1014. inherited Create;
  1015. typ:=ait_const;
  1016. consttype:=aitconst_uleb128bit;
  1017. value:=int64(_value);
  1018. sym:=nil;
  1019. endsym:=nil;
  1020. end;
  1021. constructor tai_const.Create_aint(_value : aint);
  1022. begin
  1023. inherited Create;
  1024. typ:=ait_const;
  1025. consttype:=aitconst_aint;
  1026. value:=_value;
  1027. sym:=nil;
  1028. endsym:=nil;
  1029. end;
  1030. constructor tai_const.Create_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  1031. begin
  1032. inherited Create;
  1033. typ:=ait_const;
  1034. consttype:=_typ;
  1035. sym:=_sym;
  1036. endsym:=nil;
  1037. value:=0;
  1038. { update sym info }
  1039. if assigned(sym) then
  1040. sym.increfs;
  1041. end;
  1042. constructor tai_const.Create_sym(_sym:tasmsymbol);
  1043. begin
  1044. inherited Create;
  1045. typ:=ait_const;
  1046. consttype:=aitconst_ptr;
  1047. { sym is allowed to be nil, this is used to write nil pointers }
  1048. sym:=_sym;
  1049. endsym:=nil;
  1050. value:=0;
  1051. { update sym info }
  1052. if assigned(sym) then
  1053. sym.increfs;
  1054. end;
  1055. constructor tai_const.Create_sym_offset(_sym:tasmsymbol;ofs:aint);
  1056. begin
  1057. inherited Create;
  1058. typ:=ait_const;
  1059. consttype:=aitconst_ptr;
  1060. if not assigned(_sym) then
  1061. internalerror(200404121);
  1062. sym:=_sym;
  1063. endsym:=nil;
  1064. value:=ofs;
  1065. { update sym info }
  1066. sym.increfs;
  1067. end;
  1068. constructor tai_const.Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  1069. begin
  1070. inherited Create;
  1071. typ:=ait_const;
  1072. consttype:=_typ;
  1073. sym:=_sym;
  1074. endsym:=_endsym;
  1075. value:=0;
  1076. { update sym info }
  1077. sym.increfs;
  1078. endsym.increfs;
  1079. end;
  1080. constructor tai_const.Create_rva_sym(_sym:tasmsymbol);
  1081. begin
  1082. inherited Create;
  1083. typ:=ait_const;
  1084. consttype:=aitconst_rva_symbol;
  1085. sym:=_sym;
  1086. endsym:=nil;
  1087. value:=0;
  1088. { update sym info }
  1089. sym.increfs;
  1090. end;
  1091. constructor tai_const.Create_indirect_sym(_sym:tasmsymbol);
  1092. begin
  1093. inherited Create;
  1094. typ:=ait_const;
  1095. consttype:=aitconst_indirect_symbol;
  1096. sym:=_sym;
  1097. endsym:=nil;
  1098. value:=0;
  1099. { update sym info }
  1100. sym.increfs;
  1101. end;
  1102. constructor tai_const.Createname(const name:string;_symtyp:Tasmsymtype;ofs:aint);
  1103. begin
  1104. inherited Create;
  1105. typ:=ait_const;
  1106. consttype:=aitconst_ptr;
  1107. sym:=objectlibrary.newasmsymbol(name,AB_EXTERNAL,_symtyp);
  1108. endsym:=nil;
  1109. value:=ofs;
  1110. { update sym info }
  1111. sym.increfs;
  1112. end;
  1113. constructor tai_const.Createname_rva(const name:string);
  1114. begin
  1115. inherited Create;
  1116. typ:=ait_const;
  1117. consttype:=aitconst_rva_symbol;
  1118. sym:=objectlibrary.newasmsymbol(name,AB_EXTERNAL,AT_FUNCTION);
  1119. endsym:=nil;
  1120. value:=0;
  1121. { update sym info }
  1122. sym.increfs;
  1123. end;
  1124. constructor tai_const.ppuload(t:taitype;ppufile:tcompilerppufile);
  1125. begin
  1126. inherited ppuload(t,ppufile);
  1127. consttype:=taiconst_type(ppufile.getbyte);
  1128. sym:=ppufile.getasmsymbol;
  1129. endsym:=ppufile.getasmsymbol;
  1130. value:=ppufile.getint64;
  1131. end;
  1132. procedure tai_const.ppuwrite(ppufile:tcompilerppufile);
  1133. begin
  1134. inherited ppuwrite(ppufile);
  1135. ppufile.putbyte(byte(consttype));
  1136. ppufile.putasmsymbol(sym);
  1137. ppufile.putasmsymbol(endsym);
  1138. ppufile.putint64(value);
  1139. end;
  1140. procedure tai_const.derefimpl;
  1141. begin
  1142. objectlibrary.DerefAsmsymbol(sym);
  1143. objectlibrary.DerefAsmsymbol(endsym);
  1144. end;
  1145. function tai_const.getcopy:tlinkedlistitem;
  1146. begin
  1147. getcopy:=inherited getcopy;
  1148. { we need to increase the reference number }
  1149. sym.increfs;
  1150. if assigned(endsym) then
  1151. endsym.increfs;
  1152. end;
  1153. function tai_const.size:longint;
  1154. begin
  1155. case consttype of
  1156. aitconst_8bit :
  1157. result:=1;
  1158. aitconst_16bit :
  1159. result:=2;
  1160. aitconst_32bit :
  1161. result:=4;
  1162. aitconst_64bit :
  1163. result:=8;
  1164. aitconst_indirect_symbol,
  1165. aitconst_rva_symbol :
  1166. result:=sizeof(aint);
  1167. end;
  1168. end;
  1169. {****************************************************************************
  1170. TAI_real_32bit
  1171. ****************************************************************************}
  1172. constructor tai_real_32bit.Create(_value : ts32real);
  1173. begin
  1174. inherited Create;
  1175. typ:=ait_real_32bit;
  1176. value:=_value;
  1177. end;
  1178. constructor tai_real_32bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1179. begin
  1180. inherited ppuload(t,ppufile);
  1181. value:=ppufile.getreal;
  1182. end;
  1183. procedure tai_real_32bit.ppuwrite(ppufile:tcompilerppufile);
  1184. begin
  1185. inherited ppuwrite(ppufile);
  1186. ppufile.putreal(value);
  1187. end;
  1188. {****************************************************************************
  1189. TAI_real_64bit
  1190. ****************************************************************************}
  1191. constructor tai_real_64bit.Create(_value : ts64real);
  1192. begin
  1193. inherited Create;
  1194. typ:=ait_real_64bit;
  1195. value:=_value;
  1196. end;
  1197. {$ifdef ARM}
  1198. constructor tai_real_64bit.Create_hiloswapped(_value : ts64real);
  1199. begin
  1200. inherited Create;
  1201. typ:=ait_real_64bit;
  1202. value:=_value;
  1203. formatoptions:=fo_hiloswapped;
  1204. end;
  1205. {$endif ARM}
  1206. constructor tai_real_64bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1207. begin
  1208. inherited ppuload(t,ppufile);
  1209. value:=ppufile.getreal;
  1210. {$ifdef ARM}
  1211. formatoptions:=tformatoptions(ppufile.getbyte);
  1212. {$endif ARM}
  1213. end;
  1214. procedure tai_real_64bit.ppuwrite(ppufile:tcompilerppufile);
  1215. begin
  1216. inherited ppuwrite(ppufile);
  1217. ppufile.putreal(value);
  1218. {$ifdef ARM}
  1219. ppufile.putbyte(byte(formatoptions));
  1220. {$endif ARM}
  1221. end;
  1222. {****************************************************************************
  1223. TAI_real_80bit
  1224. ****************************************************************************}
  1225. constructor tai_real_80bit.Create(_value : ts80real);
  1226. begin
  1227. inherited Create;
  1228. typ:=ait_real_80bit;
  1229. value:=_value;
  1230. end;
  1231. constructor tai_real_80bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1232. begin
  1233. inherited ppuload(t,ppufile);
  1234. value:=ppufile.getreal;
  1235. end;
  1236. procedure tai_real_80bit.ppuwrite(ppufile:tcompilerppufile);
  1237. begin
  1238. inherited ppuwrite(ppufile);
  1239. ppufile.putreal(value);
  1240. end;
  1241. {****************************************************************************
  1242. TAI_real_80bit
  1243. ****************************************************************************}
  1244. constructor tai_real_128bit.Create(_value : ts128real);
  1245. begin
  1246. inherited Create;
  1247. typ:=ait_real_128bit;
  1248. value:=_value;
  1249. end;
  1250. constructor tai_real_128bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1251. begin
  1252. inherited ppuload(t,ppufile);
  1253. value:=ppufile.getreal;
  1254. end;
  1255. procedure tai_real_128bit.ppuwrite(ppufile:tcompilerppufile);
  1256. begin
  1257. inherited ppuwrite(ppufile);
  1258. ppufile.putreal(value);
  1259. end;
  1260. {****************************************************************************
  1261. Tai_comp_64bit
  1262. ****************************************************************************}
  1263. constructor tai_comp_64bit.Create(_value : ts64comp);
  1264. begin
  1265. inherited Create;
  1266. typ:=ait_comp_64bit;
  1267. value:=_value;
  1268. end;
  1269. constructor tai_comp_64bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1270. begin
  1271. inherited ppuload(t,ppufile);
  1272. ppufile.putdata(value,sizeof(value));
  1273. end;
  1274. procedure tai_comp_64bit.ppuwrite(ppufile:tcompilerppufile);
  1275. begin
  1276. inherited ppuwrite(ppufile);
  1277. ppufile.getdata(value,sizeof(value));
  1278. end;
  1279. {****************************************************************************
  1280. TAI_STRING
  1281. ****************************************************************************}
  1282. constructor tai_string.Create(const _str : string);
  1283. begin
  1284. inherited Create;
  1285. typ:=ait_string;
  1286. len:=length(_str);
  1287. getmem(str,len+1);
  1288. strpcopy(str,_str);
  1289. end;
  1290. constructor tai_string.Create_pchar(_str : pchar;length : longint);
  1291. begin
  1292. inherited Create;
  1293. typ:=ait_string;
  1294. str:=_str;
  1295. len:=length;
  1296. end;
  1297. destructor tai_string.destroy;
  1298. begin
  1299. if str<>nil then
  1300. freemem(str);
  1301. inherited Destroy;
  1302. end;
  1303. constructor tai_string.ppuload(t:taitype;ppufile:tcompilerppufile);
  1304. begin
  1305. inherited ppuload(t,ppufile);
  1306. len:=ppufile.getlongint;
  1307. getmem(str,len);
  1308. ppufile.getdata(str^,len);
  1309. end;
  1310. procedure tai_string.ppuwrite(ppufile:tcompilerppufile);
  1311. begin
  1312. inherited ppuwrite(ppufile);
  1313. ppufile.putlongint(len);
  1314. ppufile.putdata(str^,len);
  1315. end;
  1316. function tai_string.getcopy : tlinkedlistitem;
  1317. var
  1318. p : tlinkedlistitem;
  1319. begin
  1320. p:=inherited getcopy;
  1321. getmem(tai_string(p).str,len);
  1322. move(str^,tai_string(p).str^,len);
  1323. getcopy:=p;
  1324. end;
  1325. {****************************************************************************
  1326. TAI_LABEL
  1327. ****************************************************************************}
  1328. constructor tai_label.create(_l : tasmlabel);
  1329. begin
  1330. inherited Create;
  1331. typ:=ait_label;
  1332. l:=_l;
  1333. l.is_set:=true;
  1334. is_global:=(l.defbind=AB_GLOBAL);
  1335. end;
  1336. constructor tai_label.ppuload(t:taitype;ppufile:tcompilerppufile);
  1337. begin
  1338. inherited ppuload(t,ppufile);
  1339. l:=tasmlabel(ppufile.getasmsymbol);
  1340. is_global:=boolean(ppufile.getbyte);
  1341. end;
  1342. procedure tai_label.ppuwrite(ppufile:tcompilerppufile);
  1343. begin
  1344. inherited ppuwrite(ppufile);
  1345. ppufile.putasmsymbol(l);
  1346. ppufile.putbyte(byte(is_global));
  1347. end;
  1348. procedure tai_label.derefimpl;
  1349. begin
  1350. objectlibrary.DerefAsmsymbol(tasmsymbol(l));
  1351. l.is_set:=true;
  1352. end;
  1353. {****************************************************************************
  1354. tai_comment comment to be inserted in the assembler file
  1355. ****************************************************************************}
  1356. constructor tai_comment.Create(_str : pchar);
  1357. begin
  1358. inherited Create;
  1359. typ:=ait_comment;
  1360. str:=_str;
  1361. end;
  1362. destructor tai_comment.destroy;
  1363. begin
  1364. strdispose(str);
  1365. inherited Destroy;
  1366. end;
  1367. constructor tai_comment.ppuload(t:taitype;ppufile:tcompilerppufile);
  1368. var
  1369. len : longint;
  1370. begin
  1371. inherited ppuload(t,ppufile);
  1372. len:=ppufile.getlongint;
  1373. getmem(str,len+1);
  1374. ppufile.getdata(str^,len);
  1375. str[len]:=#0;
  1376. end;
  1377. procedure tai_comment.ppuwrite(ppufile:tcompilerppufile);
  1378. var
  1379. len : longint;
  1380. begin
  1381. inherited ppuwrite(ppufile);
  1382. len:=strlen(str);
  1383. ppufile.putlongint(len);
  1384. ppufile.putdata(str^,len);
  1385. end;
  1386. function tai_comment.getcopy : tlinkedlistitem;
  1387. var
  1388. p : tlinkedlistitem;
  1389. begin
  1390. p:=inherited getcopy;
  1391. getmem(tai_comment(p).str,strlen(str)+1);
  1392. move(str^,tai_comment(p).str^,strlen(str)+1);
  1393. getcopy:=p;
  1394. end;
  1395. {****************************************************************************
  1396. TAI_STABS
  1397. ****************************************************************************}
  1398. constructor tai_stab.create(_stabtype:tstabtype;_str : pchar);
  1399. begin
  1400. inherited create;
  1401. typ:=ait_stab;
  1402. str:=_str;
  1403. stabtype:=_stabtype;
  1404. end;
  1405. constructor tai_stab.create_str(_stabtype:tstabtype;const s:string);
  1406. begin
  1407. self.create(_stabtype,strpnew(s));
  1408. end;
  1409. destructor tai_stab.destroy;
  1410. begin
  1411. strdispose(str);
  1412. inherited destroy;
  1413. end;
  1414. {****************************************************************************
  1415. TAI_FORCE_LINE
  1416. ****************************************************************************}
  1417. constructor tai_force_line.create;
  1418. begin
  1419. inherited create;
  1420. typ:=ait_force_line;
  1421. end;
  1422. {****************************************************************************
  1423. TAI_FUNCTION_NAME
  1424. ****************************************************************************}
  1425. constructor tai_function_name.create(const s:string);
  1426. begin
  1427. inherited create;
  1428. typ:=ait_function_name;
  1429. funcname:=stringdup(s);
  1430. end;
  1431. destructor tai_function_name.destroy;
  1432. begin
  1433. stringdispose(funcname);
  1434. inherited destroy;
  1435. end;
  1436. {****************************************************************************
  1437. TAI_CUTOBJECT
  1438. ****************************************************************************}
  1439. constructor tai_cutobject.Create;
  1440. begin
  1441. inherited Create;
  1442. typ:=ait_cutobject;
  1443. place:=cut_normal;
  1444. end;
  1445. constructor tai_cutobject.Create_begin;
  1446. begin
  1447. inherited Create;
  1448. typ:=ait_cutobject;
  1449. place:=cut_begin;
  1450. end;
  1451. constructor tai_cutobject.Create_end;
  1452. begin
  1453. inherited Create;
  1454. typ:=ait_cutobject;
  1455. place:=cut_end;
  1456. end;
  1457. constructor tai_cutobject.ppuload(t:taitype;ppufile:tcompilerppufile);
  1458. begin
  1459. inherited ppuload(t,ppufile);
  1460. place:=TCutPlace(ppufile.getbyte);
  1461. end;
  1462. procedure tai_cutobject.ppuwrite(ppufile:tcompilerppufile);
  1463. begin
  1464. inherited ppuwrite(ppufile);
  1465. ppufile.putbyte(byte(place));
  1466. end;
  1467. {****************************************************************************
  1468. Tai_Marker
  1469. ****************************************************************************}
  1470. constructor Tai_Marker.Create(_Kind: TMarker);
  1471. begin
  1472. Inherited Create;
  1473. typ := ait_marker;
  1474. Kind := _Kind;
  1475. end;
  1476. constructor Tai_Marker.ppuload(t:taitype;ppufile:tcompilerppufile);
  1477. begin
  1478. inherited ppuload(t,ppufile);
  1479. kind:=TMarker(ppufile.getbyte);
  1480. end;
  1481. procedure Tai_Marker.ppuwrite(ppufile:tcompilerppufile);
  1482. begin
  1483. inherited ppuwrite(ppufile);
  1484. ppufile.putbyte(byte(kind));
  1485. end;
  1486. {*****************************************************************************
  1487. tai_tempalloc
  1488. *****************************************************************************}
  1489. constructor tai_tempalloc.alloc(pos,size:longint);
  1490. begin
  1491. inherited Create;
  1492. typ:=ait_tempalloc;
  1493. allocation:=true;
  1494. temppos:=pos;
  1495. tempsize:=size;
  1496. {$ifdef EXTDEBUG}
  1497. problem:=nil;
  1498. {$endif EXTDEBUG}
  1499. end;
  1500. destructor tai_tempalloc.destroy;
  1501. begin
  1502. {$ifdef EXTDEBUG}
  1503. stringdispose(problem);
  1504. {$endif EXTDEBUG}
  1505. inherited destroy;
  1506. end;
  1507. constructor tai_tempalloc.dealloc(pos,size:longint);
  1508. begin
  1509. inherited Create;
  1510. typ:=ait_tempalloc;
  1511. allocation:=false;
  1512. temppos:=pos;
  1513. tempsize:=size;
  1514. {$ifdef EXTDEBUG}
  1515. problem:=nil;
  1516. {$endif EXTDEBUG}
  1517. end;
  1518. {$ifdef EXTDEBUG}
  1519. constructor tai_tempalloc.allocinfo(pos,size:longint;const st:string);
  1520. begin
  1521. inherited Create;
  1522. typ:=ait_tempalloc;
  1523. allocation:=false;
  1524. temppos:=pos;
  1525. tempsize:=size;
  1526. problem:=stringdup(st);
  1527. end;
  1528. {$endif EXTDEBUG}
  1529. constructor tai_tempalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  1530. begin
  1531. inherited ppuload(t,ppufile);
  1532. temppos:=ppufile.getlongint;
  1533. tempsize:=ppufile.getlongint;
  1534. allocation:=boolean(ppufile.getbyte);
  1535. {$ifdef EXTDEBUG}
  1536. problem:=nil;
  1537. {$endif EXTDEBUG}
  1538. end;
  1539. procedure tai_tempalloc.ppuwrite(ppufile:tcompilerppufile);
  1540. begin
  1541. inherited ppuwrite(ppufile);
  1542. ppufile.putlongint(temppos);
  1543. ppufile.putlongint(tempsize);
  1544. ppufile.putbyte(byte(allocation));
  1545. end;
  1546. {*****************************************************************************
  1547. tai_regalloc
  1548. *****************************************************************************}
  1549. constructor tai_regalloc.alloc(r : tregister;ainstr:tai);
  1550. begin
  1551. inherited create;
  1552. typ:=ait_regalloc;
  1553. ratype:=ra_alloc;
  1554. reg:=r;
  1555. { ainstr must be an instruction }
  1556. if assigned(ainstr) and
  1557. (ainstr.typ<>ait_instruction) then
  1558. internalerror(200411011);
  1559. instr:=ainstr;
  1560. end;
  1561. constructor tai_regalloc.dealloc(r : tregister;ainstr:tai);
  1562. begin
  1563. inherited create;
  1564. typ:=ait_regalloc;
  1565. ratype:=ra_dealloc;
  1566. reg:=r;
  1567. { ainstr must be an instruction }
  1568. if assigned(ainstr) and
  1569. (ainstr.typ<>ait_instruction) then
  1570. internalerror(200411012);
  1571. instr:=ainstr;
  1572. end;
  1573. constructor tai_regalloc.sync(r : tregister);
  1574. begin
  1575. inherited create;
  1576. typ:=ait_regalloc;
  1577. ratype:=ra_sync;
  1578. reg:=r;
  1579. end;
  1580. constructor tai_regalloc.resize(r : tregister);
  1581. begin
  1582. inherited create;
  1583. typ:=ait_regalloc;
  1584. ratype:=ra_resize;
  1585. reg:=r;
  1586. end;
  1587. constructor tai_regalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  1588. begin
  1589. inherited ppuload(t,ppufile);
  1590. ppufile.getdata(reg,sizeof(Tregister));
  1591. ratype:=tregalloctype(ppufile.getbyte);
  1592. end;
  1593. procedure tai_regalloc.ppuwrite(ppufile:tcompilerppufile);
  1594. begin
  1595. inherited ppuwrite(ppufile);
  1596. ppufile.putdata(reg,sizeof(Tregister));
  1597. ppufile.putbyte(byte(ratype));
  1598. end;
  1599. {****************************************************************************
  1600. tai_file
  1601. ****************************************************************************}
  1602. constructor tai_file.Create(_str : string);
  1603. begin
  1604. inherited Create;
  1605. typ:=ait_file;
  1606. str:=strpnew(_str);
  1607. end;
  1608. destructor tai_file.destroy;
  1609. begin
  1610. strdispose(str);
  1611. inherited Destroy;
  1612. end;
  1613. constructor tai_file.ppuload(t:taitype;ppufile:tcompilerppufile);
  1614. var
  1615. len : longint;
  1616. begin
  1617. inherited ppuload(t,ppufile);
  1618. len:=ppufile.getlongint;
  1619. getmem(str,len+1);
  1620. ppufile.getdata(str^,len);
  1621. str[len]:=#0;
  1622. idx:=ppufile.getlongint;
  1623. end;
  1624. procedure tai_file.ppuwrite(ppufile:tcompilerppufile);
  1625. var
  1626. len : longint;
  1627. begin
  1628. inherited ppuwrite(ppufile);
  1629. len:=strlen(str);
  1630. ppufile.putlongint(len);
  1631. ppufile.putdata(str^,len);
  1632. ppufile.putlongint(idx);
  1633. end;
  1634. function tai_file.getcopy : tlinkedlistitem;
  1635. var
  1636. p : tlinkedlistitem;
  1637. begin
  1638. p:=inherited getcopy;
  1639. getmem(tai_comment(p).str,strlen(str)+1);
  1640. move(str^,tai_comment(p).str^,strlen(str)+1);
  1641. getcopy:=p;
  1642. end;
  1643. {****************************************************************************
  1644. tai_loc
  1645. ****************************************************************************}
  1646. constructor tai_loc.Create(_fileidx : tai_file;_line,_column : longint);
  1647. begin
  1648. inherited Create;
  1649. typ:=ait_loc;
  1650. fileentry:=_fileidx;
  1651. line:=_line;
  1652. column:=_column;
  1653. end;
  1654. constructor tai_loc.ppuload(t:taitype;ppufile:tcompilerppufile);
  1655. begin
  1656. inherited ppuload(t,ppufile);
  1657. {!!!! fileidx:=ppufile.getlongint; }
  1658. line:=ppufile.getlongint;
  1659. column:=ppufile.getlongint;
  1660. end;
  1661. procedure tai_loc.ppuwrite(ppufile:tcompilerppufile);
  1662. begin
  1663. inherited ppuwrite(ppufile);
  1664. {!!!!! ppufile.putlongint(fileidx); }
  1665. ppufile.putlongint(line);
  1666. ppufile.putlongint(column);
  1667. end;
  1668. {*****************************************************************************
  1669. TaiInstruction
  1670. *****************************************************************************}
  1671. constructor tai_cpu_abstract.Create(op : tasmop);
  1672. begin
  1673. inherited create;
  1674. typ:=ait_instruction;
  1675. is_jmp:=false;
  1676. opcode:=op;
  1677. ops:=0;
  1678. fillchar(condition,sizeof(condition),0);
  1679. fillchar(oper,sizeof(oper),0);
  1680. end;
  1681. destructor tai_cpu_abstract.Destroy;
  1682. var
  1683. i : integer;
  1684. begin
  1685. for i:=0 to opercnt-1 do
  1686. begin
  1687. clearop(i);
  1688. dispose(oper[i]);
  1689. end;
  1690. inherited destroy;
  1691. end;
  1692. { ---------------------------------------------------------------------
  1693. Loading of operands.
  1694. ---------------------------------------------------------------------}
  1695. procedure tai_cpu_abstract.allocate_oper(opers:longint);
  1696. begin
  1697. while (opers>opercnt) do
  1698. begin
  1699. new(oper[opercnt]);
  1700. fillchar(oper[opercnt]^,sizeof(toper),0);
  1701. inc(opercnt);
  1702. end;
  1703. end;
  1704. procedure tai_cpu_abstract.loadconst(opidx:longint;l:aint);
  1705. begin
  1706. allocate_oper(opidx+1);
  1707. with oper[opidx]^ do
  1708. begin
  1709. if typ<>top_const then
  1710. clearop(opidx);
  1711. val:=l;
  1712. typ:=top_const;
  1713. end;
  1714. end;
  1715. procedure tai_cpu_abstract.loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  1716. var
  1717. r : treference;
  1718. begin
  1719. reference_reset_symbol(r,s,sofs);
  1720. r.refaddr:=addr_full;
  1721. loadref(opidx,r);
  1722. end;
  1723. procedure tai_cpu_abstract.loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset,forceref:boolean);
  1724. begin
  1725. if not assigned(s) then
  1726. internalerror(200204251);
  1727. allocate_oper(opidx+1);
  1728. with oper[opidx]^ do
  1729. begin
  1730. if typ<>top_local then
  1731. begin
  1732. clearop(opidx);
  1733. new(localoper);
  1734. end;
  1735. with oper[opidx]^.localoper^ do
  1736. begin
  1737. localsym:=s;
  1738. localsymofs:=sofs;
  1739. localindexreg:=indexreg;
  1740. localscale:=scale;
  1741. localgetoffset:=getoffset;
  1742. localforceref:=forceref;
  1743. end;
  1744. typ:=top_local;
  1745. end;
  1746. end;
  1747. procedure tai_cpu_abstract.loadref(opidx:longint;const r:treference);
  1748. begin
  1749. allocate_oper(opidx+1);
  1750. with oper[opidx]^ do
  1751. begin
  1752. if typ<>top_ref then
  1753. begin
  1754. clearop(opidx);
  1755. new(ref);
  1756. end;
  1757. ref^:=r;
  1758. {$ifdef x86}
  1759. { We allow this exception for x86, since overloading this would be
  1760. too much of a a speed penalty}
  1761. if (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  1762. segprefix:=ref^.segment;
  1763. {$endif}
  1764. {$ifdef extdebug}
  1765. if (cs_create_pic in aktmoduleswitches) and
  1766. assigned(r.symbol) and
  1767. (r.refaddr=addr_no) then
  1768. internalerror(200502052);
  1769. {$endif}
  1770. typ:=top_ref;
  1771. if assigned(add_reg_instruction_hook) then
  1772. begin
  1773. add_reg_instruction_hook(self,ref^.base);
  1774. add_reg_instruction_hook(self,ref^.index);
  1775. end;
  1776. { mark symbol as used }
  1777. if assigned(ref^.symbol) then
  1778. ref^.symbol.increfs;
  1779. end;
  1780. end;
  1781. procedure tai_cpu_abstract.loadreg(opidx:longint;r:tregister);
  1782. begin
  1783. allocate_oper(opidx+1);
  1784. with oper[opidx]^ do
  1785. begin
  1786. if typ<>top_reg then
  1787. clearop(opidx);
  1788. reg:=r;
  1789. typ:=top_reg;
  1790. end;
  1791. if assigned(add_reg_instruction_hook) then
  1792. add_reg_instruction_hook(self,r);
  1793. {$ifdef ARM}
  1794. { R15 is the PC on the ARM thus moves to R15 are jumps.
  1795. Due to speed considerations we don't use a virtual overridden method here.
  1796. Because the pc/r15 isn't handled by the reg. allocator this should never cause
  1797. problems with iregs getting r15.
  1798. }
  1799. is_jmp:=(opcode=A_MOV) and (opidx=0) and (r=NR_R15);
  1800. {$endif ARM}
  1801. end;
  1802. procedure tai_cpu_abstract.loadoper(opidx:longint;o:toper);
  1803. begin
  1804. allocate_oper(opidx+1);
  1805. clearop(opidx);
  1806. oper[opidx]^:=o;
  1807. { copy also the reference }
  1808. with oper[opidx]^ do
  1809. begin
  1810. case typ of
  1811. top_reg:
  1812. begin
  1813. if assigned(add_reg_instruction_hook) then
  1814. add_reg_instruction_hook(self,reg);
  1815. end;
  1816. top_ref:
  1817. begin
  1818. new(ref);
  1819. ref^:=o.ref^;
  1820. if assigned(add_reg_instruction_hook) then
  1821. begin
  1822. add_reg_instruction_hook(self,ref^.base);
  1823. add_reg_instruction_hook(self,ref^.index);
  1824. end;
  1825. end;
  1826. {$ifdef ARM}
  1827. top_shifterop:
  1828. begin
  1829. new(shifterop);
  1830. shifterop^:=o.shifterop^;
  1831. if assigned(add_reg_instruction_hook) then
  1832. add_reg_instruction_hook(self,shifterop^.rs);
  1833. end;
  1834. {$endif ARM}
  1835. end;
  1836. end;
  1837. end;
  1838. procedure tai_cpu_abstract.clearop(opidx:longint);
  1839. begin
  1840. with oper[opidx]^ do
  1841. begin
  1842. case typ of
  1843. top_ref:
  1844. dispose(ref);
  1845. top_local:
  1846. dispose(localoper);
  1847. {$ifdef ARM}
  1848. top_shifterop:
  1849. dispose(shifterop);
  1850. top_regset:
  1851. dispose(regset);
  1852. {$endif ARM}
  1853. end;
  1854. typ:=top_none;
  1855. end;
  1856. end;
  1857. { ---------------------------------------------------------------------
  1858. Miscellaneous methods.
  1859. ---------------------------------------------------------------------}
  1860. procedure tai_cpu_abstract.SetCondition(const c:TAsmCond);
  1861. begin
  1862. condition:=c;
  1863. end;
  1864. Function tai_cpu_abstract.getcopy:TLinkedListItem;
  1865. var
  1866. i : longint;
  1867. p : tai_cpu_abstract;
  1868. begin
  1869. p:=tai_cpu_abstract(inherited getcopy);
  1870. { make a copy of the references }
  1871. p.opercnt:=0;
  1872. p.allocate_oper(ops);
  1873. for i:=0 to ops-1 do
  1874. begin
  1875. p.oper[i]^:=oper[i]^;
  1876. case oper[i]^.typ of
  1877. top_local :
  1878. begin
  1879. new(p.oper[i]^.localoper);
  1880. p.oper[i]^.localoper^:=oper[i]^.localoper^;
  1881. end;
  1882. top_ref :
  1883. begin
  1884. new(p.oper[i]^.ref);
  1885. p.oper[i]^.ref^:=oper[i]^.ref^;
  1886. end;
  1887. {$ifdef ARM}
  1888. top_shifterop:
  1889. begin
  1890. new(p.oper[i]^.shifterop);
  1891. p.oper[i]^.shifterop^:=oper[i]^.shifterop^;
  1892. end;
  1893. {$endif ARM}
  1894. end;
  1895. end;
  1896. getcopy:=p;
  1897. end;
  1898. function tai_cpu_abstract.is_same_reg_move(regtype: Tregistertype):boolean;
  1899. begin
  1900. { When the generic RA is used this needs to be overriden, we don't use
  1901. virtual;abstract; to prevent a lot of warnings of unimplemented abstract methods
  1902. when tai_cpu is created (PFV) }
  1903. internalerror(200404091);
  1904. result:=false;
  1905. end;
  1906. function tai_cpu_abstract.spilling_get_operation_type(opnr: longint): topertype;
  1907. begin
  1908. internalerror(200404091);
  1909. result:=operand_readwrite;
  1910. end;
  1911. function tai_cpu_abstract.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
  1912. begin
  1913. result := operand_read;
  1914. end;
  1915. constructor tai_cpu_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  1916. var
  1917. i : integer;
  1918. begin
  1919. inherited ppuload(t,ppufile);
  1920. { hopefully, we don't get problems with big/litte endian here when cross compiling :/ }
  1921. ppufile.getdata(condition,sizeof(tasmcond));
  1922. allocate_oper(ppufile.getbyte);
  1923. for i:=0 to ops-1 do
  1924. ppuloadoper(ppufile,oper[i]^);
  1925. opcode:=tasmop(ppufile.getword);
  1926. {$ifdef x86}
  1927. ppufile.getdata(segprefix,sizeof(Tregister));
  1928. {$endif x86}
  1929. is_jmp:=boolean(ppufile.getbyte);
  1930. end;
  1931. procedure tai_cpu_abstract.ppuwrite(ppufile:tcompilerppufile);
  1932. var
  1933. i : integer;
  1934. begin
  1935. inherited ppuwrite(ppufile);
  1936. ppufile.putdata(condition,sizeof(tasmcond));
  1937. ppufile.putbyte(ops);
  1938. for i:=0 to ops-1 do
  1939. ppuwriteoper(ppufile,oper[i]^);
  1940. ppufile.putword(word(opcode));
  1941. {$ifdef x86}
  1942. ppufile.putdata(segprefix,sizeof(Tregister));
  1943. {$endif x86}
  1944. ppufile.putbyte(byte(is_jmp));
  1945. end;
  1946. procedure tai_cpu_abstract.buildderefimpl;
  1947. var
  1948. i : integer;
  1949. begin
  1950. for i:=0 to ops-1 do
  1951. ppubuildderefimploper(oper[i]^);
  1952. end;
  1953. procedure tai_cpu_abstract.derefimpl;
  1954. var
  1955. i : integer;
  1956. begin
  1957. for i:=0 to ops-1 do
  1958. ppuderefoper(oper[i]^);
  1959. end;
  1960. {****************************************************************************
  1961. tai_align_abstract
  1962. ****************************************************************************}
  1963. constructor tai_align_abstract.Create(b: byte);
  1964. begin
  1965. inherited Create;
  1966. typ:=ait_align;
  1967. if b in [1,2,4,8,16,32] then
  1968. aligntype := b
  1969. else
  1970. aligntype := 1;
  1971. fillsize:=0;
  1972. fillop:=0;
  1973. use_op:=false;
  1974. end;
  1975. constructor tai_align_abstract.Create_op(b: byte; _op: byte);
  1976. begin
  1977. inherited Create;
  1978. typ:=ait_align;
  1979. if b in [1,2,4,8,16,32] then
  1980. aligntype := b
  1981. else
  1982. aligntype := 1;
  1983. fillsize:=0;
  1984. fillop:=_op;
  1985. use_op:=true;
  1986. end;
  1987. constructor tai_align_abstract.Create_zeros(b: byte);
  1988. begin
  1989. inherited Create;
  1990. typ:=ait_align;
  1991. if b in [1,2,4,8,16,32] then
  1992. aligntype := b
  1993. else
  1994. aligntype := 1;
  1995. use_op:=true;
  1996. fillsize:=0;
  1997. fillop:=0;
  1998. end;
  1999. function tai_align_abstract.calculatefillbuf(var buf : tfillbuffer):pchar;
  2000. begin
  2001. if fillsize>sizeof(buf) then
  2002. internalerror(200404293);
  2003. fillchar(buf,high(buf),fillop);
  2004. calculatefillbuf:=pchar(@buf);
  2005. end;
  2006. constructor tai_align_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  2007. begin
  2008. inherited ppuload(t,ppufile);
  2009. aligntype:=ppufile.getbyte;
  2010. fillsize:=0;
  2011. fillop:=ppufile.getbyte;
  2012. use_op:=boolean(ppufile.getbyte);
  2013. end;
  2014. procedure tai_align_abstract.ppuwrite(ppufile:tcompilerppufile);
  2015. begin
  2016. inherited ppuwrite(ppufile);
  2017. ppufile.putbyte(aligntype);
  2018. ppufile.putbyte(fillop);
  2019. ppufile.putbyte(byte(use_op));
  2020. end;
  2021. {*****************************************************************************
  2022. TAAsmOutput
  2023. *****************************************************************************}
  2024. constructor taasmoutput.create;
  2025. begin
  2026. inherited create;
  2027. { make sure the optimizer won't remove the first tai of this list}
  2028. insert(tai_marker.create(marker_blockstart));
  2029. end;
  2030. function taasmoutput.empty : boolean;
  2031. begin
  2032. { there is always a marker_blockstart available,
  2033. see taasmoutput.create }
  2034. result:=(count<=1);
  2035. end;
  2036. function taasmoutput.getlasttaifilepos : pfileposinfo;
  2037. var
  2038. hp : tlinkedlistitem;
  2039. begin
  2040. getlasttaifilepos := nil;
  2041. if assigned(last) then
  2042. begin
  2043. { find the last file information record }
  2044. if not (tai(last).typ in SkipLineInfo) then
  2045. getlasttaifilepos:=@tailineinfo(last).fileinfo
  2046. else
  2047. { go through list backwards to find the first entry
  2048. with line information
  2049. }
  2050. begin
  2051. hp:=tai(last);
  2052. while assigned(hp) and (tai(hp).typ in SkipLineInfo) do
  2053. hp:=hp.Previous;
  2054. { found entry }
  2055. if assigned(hp) then
  2056. getlasttaifilepos:=@tailineinfo(hp).fileinfo
  2057. end;
  2058. end;
  2059. end;
  2060. procedure Taasmoutput.InsertAfter(Item,Loc : TLinkedListItem);
  2061. begin
  2062. { This is not possible because it is not sure that the
  2063. tai at Loc has taifileinfo as parent }
  2064. {if assigned(Loc) then
  2065. tailineinfo(Item).fileinfo:=tailineinfo(Loc).fileinfo;}
  2066. inherited InsertAfter(Item,Loc);
  2067. end;
  2068. begin
  2069. cai_cpu:=tai_cpu_abstract;
  2070. cai_align:=tai_align_abstract;
  2071. end.