aasmtai.pas 71 KB

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