aasmtai.pas 71 KB

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