2
0

aasmtai.pas 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491
  1. {
  2. Copyright (c) 1998-2006 by Florian Klaempfl
  3. This unit implements an abstract asmoutput class for all processor types
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. { @abstract(This unit implements an abstract asm output class for all processor types)
  18. This unit implements an abstract assembler output class for all processors, these
  19. are then overriden for each assembler writer to actually write the data in these
  20. classes to an assembler file.
  21. }
  22. unit aasmtai;
  23. {$i fpcdefs.inc}
  24. interface
  25. uses
  26. cutils,cclasses,
  27. globtype,systems,
  28. cpuinfo,cpubase,
  29. cgbase,cgutils,
  30. symtype,
  31. aasmbase,aasmdata,ogbase;
  32. type
  33. { keep the number of elements in this enumeration less or equal than 32 as long
  34. as FPC knows only 4 byte and 32 byte sets (FK) }
  35. taitype = (
  36. ait_none,
  37. ait_align,
  38. ait_section,
  39. ait_comment,
  40. ait_string,
  41. ait_instruction,
  42. ait_datablock,
  43. ait_symbol,
  44. { needed to calc the size of a symbol }
  45. ait_symbol_end,
  46. ait_directive,
  47. ait_label,
  48. ait_const,
  49. ait_real_32bit,
  50. ait_real_64bit,
  51. ait_real_80bit,
  52. ait_comp_64bit,
  53. ait_real_128bit,
  54. ait_stab,
  55. ait_force_line,
  56. ait_function_name,
  57. {$ifdef alpha}
  58. { the follow is for the DEC Alpha }
  59. ait_frame,
  60. ait_ent,
  61. {$endif alpha}
  62. {$ifdef ia64}
  63. ait_bundle,
  64. ait_stop,
  65. {$endif ia64}
  66. {$ifdef m68k}
  67. ait_labeled_instruction,
  68. {$endif m68k}
  69. { 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. globals,
  581. fmodule;
  582. const
  583. pputaimarker = 254;
  584. {****************************************************************************
  585. Helpers
  586. ****************************************************************************}
  587. procedure maybe_new_object_file(list:TAsmList);
  588. begin
  589. if (cs_create_smart in current_settings.moduleswitches) and
  590. (not use_smartlink_section) then
  591. list.concat(tai_cutobject.create);
  592. end;
  593. procedure new_section(list:TAsmList;Asectype:TAsmSectiontype;Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  594. begin
  595. list.concat(tai_section.create(Asectype,Aname,Aalign,Asecorder));
  596. list.concat(cai_align.create(Aalign));
  597. end;
  598. procedure section_symbol_start(list:TAsmList;const Aname:string;Asymtyp:Tasmsymtype;
  599. Aglobal:boolean;Asectype:TAsmSectiontype;Aalign:byte);
  600. begin
  601. maybe_new_object_file(list);
  602. list.concat(tai_section.create(Asectype,Aname,Aalign));
  603. list.concat(cai_align.create(Aalign));
  604. if Aglobal or
  605. maybe_smartlink_symbol then
  606. list.concat(tai_symbol.createname_global(Aname,Asymtyp,0))
  607. else
  608. list.concat(tai_symbol.createname(Aname,Asymtyp,0));
  609. end;
  610. procedure section_symbol_end(list:TAsmList;const Aname:string);
  611. begin
  612. list.concat(tai_symbol_end.createname(Aname));
  613. end;
  614. function ppuloadai(ppufile:tcompilerppufile):tai;
  615. var
  616. b : byte;
  617. t : taitype;
  618. begin
  619. { marker }
  620. b:=ppufile.getbyte;
  621. if b<>pputaimarker then
  622. internalerror(200208181);
  623. { load nodetype }
  624. t:=taitype(ppufile.getbyte);
  625. if t<>ait_none then
  626. begin
  627. if t>high(taitype) then
  628. internalerror(200208182);
  629. if not assigned(aiclass[t]) then
  630. internalerror(200208183);
  631. {writeln('taiload: ',taitypestr[t]);}
  632. { generate tai of the correct class }
  633. ppuloadai:=aiclass[t].ppuload(t,ppufile);
  634. end
  635. else
  636. ppuloadai:=nil;
  637. end;
  638. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  639. begin
  640. { marker, read by ppuloadnode }
  641. ppufile.putbyte(pputaimarker);
  642. if assigned(n) then
  643. begin
  644. { type, read by ppuloadnode }
  645. ppufile.putbyte(byte(n.typ));
  646. {writeln('taiwrite: ',taitypestr[n.typ]);}
  647. n.ppuwrite(ppufile);
  648. end
  649. else
  650. ppufile.putbyte(byte(ait_none));
  651. end;
  652. {****************************************************************************
  653. TAI
  654. ****************************************************************************}
  655. constructor tai.Create;
  656. begin
  657. {$ifndef NOOPT}
  658. optinfo:=nil;
  659. {$endif NOOPT}
  660. end;
  661. constructor tai.ppuload(t:taitype;ppufile:tcompilerppufile);
  662. begin
  663. typ:=t;
  664. {$ifndef NOOPT}
  665. optinfo:=nil;
  666. {$endif}
  667. end;
  668. procedure tai.ppuwrite(ppufile:tcompilerppufile);
  669. begin
  670. end;
  671. procedure tai.buildderefimpl;
  672. begin
  673. end;
  674. procedure tai.derefimpl;
  675. begin
  676. end;
  677. {****************************************************************************
  678. TAILINEINFO
  679. ****************************************************************************}
  680. constructor tailineinfo.create;
  681. begin
  682. inherited create;
  683. fileinfo:=current_filepos;
  684. end;
  685. constructor tailineinfo.ppuload(t:taitype;ppufile:tcompilerppufile);
  686. begin
  687. inherited ppuload(t,ppufile);
  688. ppufile.getposinfo(fileinfo);
  689. end;
  690. procedure tailineinfo.ppuwrite(ppufile:tcompilerppufile);
  691. begin
  692. inherited ppuwrite(ppufile);
  693. ppufile.putposinfo(fileinfo);
  694. end;
  695. {****************************************************************************
  696. TAI_SIMPLE
  697. ****************************************************************************}
  698. constructor tai_simple.create(_typ : taitype);
  699. begin
  700. inherited create;
  701. typ:=_typ;
  702. end;
  703. {****************************************************************************
  704. TAI_SECTION
  705. ****************************************************************************}
  706. constructor tai_section.Create(Asectype:TAsmSectiontype;Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  707. begin
  708. inherited Create;
  709. typ:=ait_section;
  710. sectype:=asectype;
  711. secalign:=Aalign;
  712. secorder:=Asecorder;
  713. name:=stringdup(Aname);
  714. sec:=nil;
  715. end;
  716. constructor tai_section.ppuload(t:taitype;ppufile:tcompilerppufile);
  717. begin
  718. inherited ppuload(t,ppufile);
  719. sectype:=TAsmSectiontype(ppufile.getbyte);
  720. secalign:=ppufile.getbyte;
  721. name:=stringdup(ppufile.getstring);
  722. sec:=nil;
  723. end;
  724. destructor tai_section.Destroy;
  725. begin
  726. stringdispose(name);
  727. end;
  728. procedure tai_section.ppuwrite(ppufile:tcompilerppufile);
  729. begin
  730. inherited ppuwrite(ppufile);
  731. ppufile.putbyte(byte(sectype));
  732. ppufile.putbyte(secalign);
  733. ppufile.putstring(name^);
  734. end;
  735. {****************************************************************************
  736. TAI_DATABLOCK
  737. ****************************************************************************}
  738. constructor tai_datablock.Create(const _name : string;_size : aint);
  739. begin
  740. inherited Create;
  741. typ:=ait_datablock;
  742. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,AT_DATA);
  743. { keep things aligned }
  744. if _size<=0 then
  745. _size:=sizeof(aint);
  746. size:=_size;
  747. is_global:=false;
  748. end;
  749. constructor tai_datablock.Create_global(const _name : string;_size : aint);
  750. begin
  751. inherited Create;
  752. typ:=ait_datablock;
  753. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,AT_DATA);
  754. { keep things aligned }
  755. if _size<=0 then
  756. _size:=sizeof(aint);
  757. size:=_size;
  758. is_global:=true;
  759. end;
  760. constructor tai_datablock.ppuload(t:taitype;ppufile:tcompilerppufile);
  761. begin
  762. inherited Create;
  763. sym:=ppufile.getasmsymbol;
  764. size:=ppufile.getaint;
  765. is_global:=boolean(ppufile.getbyte);
  766. end;
  767. procedure tai_datablock.ppuwrite(ppufile:tcompilerppufile);
  768. begin
  769. inherited ppuwrite(ppufile);
  770. ppufile.putasmsymbol(sym);
  771. ppufile.putaint(size);
  772. ppufile.putbyte(byte(is_global));
  773. end;
  774. procedure tai_datablock.derefimpl;
  775. begin
  776. end;
  777. {****************************************************************************
  778. TAI_SYMBOL
  779. ****************************************************************************}
  780. constructor tai_symbol.Create(_sym:tasmsymbol;siz:longint);
  781. begin
  782. inherited Create;
  783. typ:=ait_symbol;
  784. sym:=_sym;
  785. size:=siz;
  786. sym.bind:=AB_LOCAL;
  787. is_global:=false;
  788. end;
  789. constructor tai_symbol.Create_global(_sym:tasmsymbol;siz:longint);
  790. begin
  791. inherited Create;
  792. typ:=ait_symbol;
  793. sym:=_sym;
  794. size:=siz;
  795. sym.bind:=AB_GLOBAL;
  796. is_global:=true;
  797. end;
  798. constructor tai_symbol.Createname(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  799. begin
  800. inherited Create;
  801. typ:=ait_symbol;
  802. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,_symtyp);
  803. size:=siz;
  804. is_global:=false;
  805. end;
  806. constructor tai_symbol.Createname_global(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  807. begin
  808. inherited Create;
  809. typ:=ait_symbol;
  810. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,_symtyp);
  811. size:=siz;
  812. is_global:=true;
  813. end;
  814. constructor tai_symbol.ppuload(t:taitype;ppufile:tcompilerppufile);
  815. begin
  816. inherited ppuload(t,ppufile);
  817. sym:=ppufile.getasmsymbol;
  818. size:=ppufile.getlongint;
  819. is_global:=boolean(ppufile.getbyte);
  820. end;
  821. procedure tai_symbol.ppuwrite(ppufile:tcompilerppufile);
  822. begin
  823. inherited ppuwrite(ppufile);
  824. ppufile.putasmsymbol(sym);
  825. ppufile.putlongint(size);
  826. ppufile.putbyte(byte(is_global));
  827. end;
  828. procedure tai_symbol.derefimpl;
  829. begin
  830. end;
  831. {****************************************************************************
  832. TAI_SYMBOL_END
  833. ****************************************************************************}
  834. constructor tai_symbol_end.Create(_sym:tasmsymbol);
  835. begin
  836. inherited Create;
  837. typ:=ait_symbol_end;
  838. sym:=_sym;
  839. end;
  840. constructor tai_symbol_end.Createname(const _name : string);
  841. begin
  842. inherited Create;
  843. typ:=ait_symbol_end;
  844. sym:=current_asmdata.RefAsmSymbol(_name);
  845. end;
  846. constructor tai_symbol_end.ppuload(t:taitype;ppufile:tcompilerppufile);
  847. begin
  848. inherited ppuload(t,ppufile);
  849. sym:=ppufile.getasmsymbol;
  850. end;
  851. procedure tai_symbol_end.ppuwrite(ppufile:tcompilerppufile);
  852. begin
  853. inherited ppuwrite(ppufile);
  854. ppufile.putasmsymbol(sym);
  855. end;
  856. procedure tai_symbol_end.derefimpl;
  857. begin
  858. end;
  859. {****************************************************************************
  860. TAI_SYMBOL_END
  861. ****************************************************************************}
  862. constructor tai_directive.Create(_directive:TAsmDirective;const _name:string);
  863. begin
  864. inherited Create;
  865. typ:=ait_directive;
  866. name:=stringdup(_name);
  867. directive:=_directive;
  868. end;
  869. destructor tai_directive.Destroy;
  870. begin
  871. stringdispose(name);
  872. end;
  873. constructor tai_directive.ppuload(t:taitype;ppufile:tcompilerppufile);
  874. begin
  875. inherited ppuload(t,ppufile);
  876. name:=stringdup(ppufile.getstring);
  877. directive:=TAsmDirective(ppufile.getbyte);
  878. end;
  879. procedure tai_directive.ppuwrite(ppufile:tcompilerppufile);
  880. begin
  881. inherited ppuwrite(ppufile);
  882. ppufile.putstring(name^);
  883. ppufile.putbyte(byte(directive));
  884. end;
  885. {****************************************************************************
  886. TAI_CONST
  887. ****************************************************************************}
  888. constructor tai_const.Create(_typ:taiconst_type;_value : int64);
  889. begin
  890. inherited Create;
  891. typ:=ait_const;
  892. consttype:=_typ;
  893. value:=_value;
  894. sym:=nil;
  895. endsym:=nil;
  896. end;
  897. constructor tai_const.Create_128bit(_value : int64);
  898. begin
  899. inherited Create;
  900. typ:=ait_const;
  901. consttype:=aitconst_128bit;
  902. value:=_value;
  903. sym:=nil;
  904. endsym:=nil;
  905. end;
  906. constructor tai_const.Create_64bit(_value : int64);
  907. begin
  908. inherited Create;
  909. typ:=ait_const;
  910. consttype:=aitconst_64bit;
  911. value:=_value;
  912. sym:=nil;
  913. endsym:=nil;
  914. end;
  915. constructor tai_const.Create_32bit(_value : longint);
  916. begin
  917. inherited Create;
  918. typ:=ait_const;
  919. consttype:=aitconst_32bit;
  920. value:=_value;
  921. sym:=nil;
  922. endsym:=nil;
  923. end;
  924. constructor tai_const.Create_16bit(_value : word);
  925. begin
  926. inherited Create;
  927. typ:=ait_const;
  928. consttype:=aitconst_16bit;
  929. value:=_value;
  930. sym:=nil;
  931. endsym:=nil;
  932. end;
  933. constructor tai_const.Create_8bit(_value : byte);
  934. begin
  935. inherited Create;
  936. typ:=ait_const;
  937. consttype:=aitconst_8bit;
  938. value:=_value;
  939. sym:=nil;
  940. endsym:=nil;
  941. end;
  942. constructor tai_const.Create_sleb128bit(_value : int64);
  943. begin
  944. inherited Create;
  945. typ:=ait_const;
  946. consttype:=aitconst_sleb128bit;
  947. value:=_value;
  948. sym:=nil;
  949. endsym:=nil;
  950. end;
  951. constructor tai_const.Create_uleb128bit(_value : qword);
  952. begin
  953. inherited Create;
  954. typ:=ait_const;
  955. consttype:=aitconst_uleb128bit;
  956. value:=int64(_value);
  957. sym:=nil;
  958. endsym:=nil;
  959. end;
  960. constructor tai_const.Create_aint(_value : aint);
  961. begin
  962. inherited Create;
  963. typ:=ait_const;
  964. consttype:=aitconst_aint;
  965. value:=_value;
  966. sym:=nil;
  967. endsym:=nil;
  968. end;
  969. constructor tai_const.Create_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  970. begin
  971. inherited Create;
  972. typ:=ait_const;
  973. consttype:=_typ;
  974. sym:=_sym;
  975. endsym:=nil;
  976. value:=0;
  977. { update sym info }
  978. if assigned(sym) then
  979. sym.increfs;
  980. end;
  981. constructor tai_const.Create_sym(_sym:tasmsymbol);
  982. begin
  983. inherited Create;
  984. typ:=ait_const;
  985. consttype:=aitconst_ptr;
  986. { sym is allowed to be nil, this is used to write nil pointers }
  987. sym:=_sym;
  988. endsym:=nil;
  989. value:=0;
  990. { update sym info }
  991. if assigned(sym) then
  992. sym.increfs;
  993. end;
  994. constructor tai_const.Create_sym_offset(_sym:tasmsymbol;ofs:aint);
  995. begin
  996. inherited Create;
  997. typ:=ait_const;
  998. consttype:=aitconst_ptr;
  999. if not assigned(_sym) then
  1000. internalerror(200404121);
  1001. sym:=_sym;
  1002. endsym:=nil;
  1003. value:=ofs;
  1004. { update sym info }
  1005. sym.increfs;
  1006. end;
  1007. constructor tai_const.Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  1008. begin
  1009. inherited Create;
  1010. typ:=ait_const;
  1011. consttype:=_typ;
  1012. sym:=_sym;
  1013. endsym:=_endsym;
  1014. value:=0;
  1015. { update sym info }
  1016. sym.increfs;
  1017. endsym.increfs;
  1018. end;
  1019. constructor tai_const.Create_rva_sym(_sym:tasmsymbol);
  1020. begin
  1021. inherited Create;
  1022. typ:=ait_const;
  1023. consttype:=aitconst_rva_symbol;
  1024. sym:=_sym;
  1025. endsym:=nil;
  1026. value:=0;
  1027. { update sym info }
  1028. sym.increfs;
  1029. end;
  1030. constructor tai_const.Create_indirect_sym(_sym:tasmsymbol);
  1031. begin
  1032. inherited Create;
  1033. typ:=ait_const;
  1034. consttype:=aitconst_indirect_symbol;
  1035. sym:=_sym;
  1036. endsym:=nil;
  1037. value:=0;
  1038. { update sym info }
  1039. sym.increfs;
  1040. end;
  1041. constructor tai_const.Createname(const name:string;ofs:aint);
  1042. begin
  1043. inherited Create;
  1044. typ:=ait_const;
  1045. consttype:=aitconst_ptr;
  1046. sym:=current_asmdata.RefAsmSymbol(name);
  1047. endsym:=nil;
  1048. value:=ofs;
  1049. { update sym info }
  1050. sym.increfs;
  1051. end;
  1052. constructor tai_const.Createname_rva(const name:string);
  1053. begin
  1054. inherited Create;
  1055. typ:=ait_const;
  1056. consttype:=aitconst_rva_symbol;
  1057. sym:=current_asmdata.RefAsmSymbol(name);
  1058. endsym:=nil;
  1059. value:=0;
  1060. { update sym info }
  1061. sym.increfs;
  1062. end;
  1063. constructor tai_const.ppuload(t:taitype;ppufile:tcompilerppufile);
  1064. begin
  1065. inherited ppuload(t,ppufile);
  1066. consttype:=taiconst_type(ppufile.getbyte);
  1067. sym:=ppufile.getasmsymbol;
  1068. endsym:=ppufile.getasmsymbol;
  1069. value:=ppufile.getint64;
  1070. end;
  1071. procedure tai_const.ppuwrite(ppufile:tcompilerppufile);
  1072. begin
  1073. inherited ppuwrite(ppufile);
  1074. ppufile.putbyte(byte(consttype));
  1075. ppufile.putasmsymbol(sym);
  1076. ppufile.putasmsymbol(endsym);
  1077. ppufile.putint64(value);
  1078. end;
  1079. procedure tai_const.derefimpl;
  1080. begin
  1081. end;
  1082. function tai_const.getcopy:tlinkedlistitem;
  1083. begin
  1084. getcopy:=inherited getcopy;
  1085. { we need to increase the reference number }
  1086. sym.increfs;
  1087. if assigned(endsym) then
  1088. endsym.increfs;
  1089. end;
  1090. function tai_const.size:longint;
  1091. begin
  1092. case consttype of
  1093. aitconst_8bit :
  1094. result:=1;
  1095. aitconst_16bit :
  1096. result:=2;
  1097. aitconst_32bit :
  1098. result:=4;
  1099. aitconst_64bit :
  1100. result:=8;
  1101. aitconst_indirect_symbol,
  1102. aitconst_secrel32_symbol,
  1103. aitconst_rva_symbol :
  1104. if target_info.system=system_x86_64_win64 then
  1105. result:=sizeof(longint)
  1106. else
  1107. result:=sizeof(aint);
  1108. aitconst_uleb128bit :
  1109. result:=LengthUleb128(value);
  1110. aitconst_sleb128bit :
  1111. result:=LengthSleb128(value);
  1112. else
  1113. internalerror(200603253);
  1114. end;
  1115. end;
  1116. {****************************************************************************
  1117. TAI_real_32bit
  1118. ****************************************************************************}
  1119. constructor tai_real_32bit.Create(_value : ts32real);
  1120. begin
  1121. inherited Create;
  1122. typ:=ait_real_32bit;
  1123. value:=_value;
  1124. end;
  1125. constructor tai_real_32bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1126. begin
  1127. inherited ppuload(t,ppufile);
  1128. value:=ppufile.getreal;
  1129. end;
  1130. procedure tai_real_32bit.ppuwrite(ppufile:tcompilerppufile);
  1131. begin
  1132. inherited ppuwrite(ppufile);
  1133. ppufile.putreal(value);
  1134. end;
  1135. {****************************************************************************
  1136. TAI_real_64bit
  1137. ****************************************************************************}
  1138. constructor tai_real_64bit.Create(_value : ts64real);
  1139. begin
  1140. inherited Create;
  1141. typ:=ait_real_64bit;
  1142. value:=_value;
  1143. end;
  1144. {$ifdef ARM}
  1145. constructor tai_real_64bit.Create_hiloswapped(_value : ts64real);
  1146. begin
  1147. inherited Create;
  1148. typ:=ait_real_64bit;
  1149. value:=_value;
  1150. formatoptions:=fo_hiloswapped;
  1151. end;
  1152. {$endif ARM}
  1153. constructor tai_real_64bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1154. begin
  1155. inherited ppuload(t,ppufile);
  1156. value:=ppufile.getreal;
  1157. {$ifdef ARM}
  1158. formatoptions:=tformatoptions(ppufile.getbyte);
  1159. {$endif ARM}
  1160. end;
  1161. procedure tai_real_64bit.ppuwrite(ppufile:tcompilerppufile);
  1162. begin
  1163. inherited ppuwrite(ppufile);
  1164. ppufile.putreal(value);
  1165. {$ifdef ARM}
  1166. ppufile.putbyte(byte(formatoptions));
  1167. {$endif ARM}
  1168. end;
  1169. {****************************************************************************
  1170. TAI_real_80bit
  1171. ****************************************************************************}
  1172. constructor tai_real_80bit.Create(_value : ts80real);
  1173. begin
  1174. inherited Create;
  1175. typ:=ait_real_80bit;
  1176. value:=_value;
  1177. end;
  1178. constructor tai_real_80bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1179. begin
  1180. inherited ppuload(t,ppufile);
  1181. value:=ppufile.getreal;
  1182. end;
  1183. procedure tai_real_80bit.ppuwrite(ppufile:tcompilerppufile);
  1184. begin
  1185. inherited ppuwrite(ppufile);
  1186. ppufile.putreal(value);
  1187. end;
  1188. {****************************************************************************
  1189. TAI_real_80bit
  1190. ****************************************************************************}
  1191. constructor tai_real_128bit.Create(_value : ts128real);
  1192. begin
  1193. inherited Create;
  1194. typ:=ait_real_128bit;
  1195. value:=_value;
  1196. end;
  1197. constructor tai_real_128bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1198. begin
  1199. inherited ppuload(t,ppufile);
  1200. value:=ppufile.getreal;
  1201. end;
  1202. procedure tai_real_128bit.ppuwrite(ppufile:tcompilerppufile);
  1203. begin
  1204. inherited ppuwrite(ppufile);
  1205. ppufile.putreal(value);
  1206. end;
  1207. {****************************************************************************
  1208. Tai_comp_64bit
  1209. ****************************************************************************}
  1210. constructor tai_comp_64bit.Create(_value : ts64comp);
  1211. begin
  1212. inherited Create;
  1213. typ:=ait_comp_64bit;
  1214. value:=_value;
  1215. end;
  1216. constructor tai_comp_64bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1217. begin
  1218. inherited ppuload(t,ppufile);
  1219. ppufile.putdata(value,sizeof(value));
  1220. end;
  1221. procedure tai_comp_64bit.ppuwrite(ppufile:tcompilerppufile);
  1222. begin
  1223. inherited ppuwrite(ppufile);
  1224. ppufile.getdata(value,sizeof(value));
  1225. end;
  1226. {****************************************************************************
  1227. TAI_STRING
  1228. ****************************************************************************}
  1229. constructor tai_string.Create(const _str : string);
  1230. begin
  1231. inherited Create;
  1232. typ:=ait_string;
  1233. len:=length(_str);
  1234. getmem(str,len+1);
  1235. strpcopy(str,_str);
  1236. end;
  1237. constructor tai_string.Create_pchar(_str : pchar;length : longint);
  1238. begin
  1239. inherited Create;
  1240. typ:=ait_string;
  1241. str:=_str;
  1242. len:=length;
  1243. end;
  1244. destructor tai_string.destroy;
  1245. begin
  1246. if str<>nil then
  1247. freemem(str);
  1248. inherited Destroy;
  1249. end;
  1250. constructor tai_string.ppuload(t:taitype;ppufile:tcompilerppufile);
  1251. begin
  1252. inherited ppuload(t,ppufile);
  1253. len:=ppufile.getlongint;
  1254. getmem(str,len);
  1255. ppufile.getdata(str^,len);
  1256. end;
  1257. procedure tai_string.ppuwrite(ppufile:tcompilerppufile);
  1258. begin
  1259. inherited ppuwrite(ppufile);
  1260. ppufile.putlongint(len);
  1261. ppufile.putdata(str^,len);
  1262. end;
  1263. function tai_string.getcopy : tlinkedlistitem;
  1264. var
  1265. p : tlinkedlistitem;
  1266. begin
  1267. p:=inherited getcopy;
  1268. getmem(tai_string(p).str,len);
  1269. move(str^,tai_string(p).str^,len);
  1270. getcopy:=p;
  1271. end;
  1272. {****************************************************************************
  1273. TAI_LABEL
  1274. ****************************************************************************}
  1275. constructor tai_label.create(_labsym : tasmlabel);
  1276. begin
  1277. inherited Create;
  1278. typ:=ait_label;
  1279. labsym:=_labsym;
  1280. labsym.is_set:=true;
  1281. is_global:=(labsym.bind=AB_GLOBAL);
  1282. end;
  1283. constructor tai_label.ppuload(t:taitype;ppufile:tcompilerppufile);
  1284. begin
  1285. inherited ppuload(t,ppufile);
  1286. labsym:=tasmlabel(ppufile.getasmsymbol);
  1287. is_global:=boolean(ppufile.getbyte);
  1288. end;
  1289. procedure tai_label.ppuwrite(ppufile:tcompilerppufile);
  1290. begin
  1291. inherited ppuwrite(ppufile);
  1292. ppufile.putasmsymbol(labsym);
  1293. ppufile.putbyte(byte(is_global));
  1294. end;
  1295. procedure tai_label.derefimpl;
  1296. begin
  1297. labsym.is_set:=true;
  1298. end;
  1299. {****************************************************************************
  1300. tai_comment comment to be inserted in the assembler file
  1301. ****************************************************************************}
  1302. constructor tai_comment.Create(_str : pchar);
  1303. begin
  1304. inherited Create;
  1305. typ:=ait_comment;
  1306. str:=_str;
  1307. end;
  1308. destructor tai_comment.destroy;
  1309. begin
  1310. freemem(str);
  1311. inherited Destroy;
  1312. end;
  1313. constructor tai_comment.ppuload(t:taitype;ppufile:tcompilerppufile);
  1314. var
  1315. len : longint;
  1316. begin
  1317. inherited ppuload(t,ppufile);
  1318. len:=ppufile.getlongint;
  1319. getmem(str,len+1);
  1320. ppufile.getdata(str^,len);
  1321. str[len]:=#0;
  1322. end;
  1323. procedure tai_comment.ppuwrite(ppufile:tcompilerppufile);
  1324. var
  1325. len : longint;
  1326. begin
  1327. inherited ppuwrite(ppufile);
  1328. len:=strlen(str);
  1329. ppufile.putlongint(len);
  1330. ppufile.putdata(str^,len);
  1331. end;
  1332. function tai_comment.getcopy : tlinkedlistitem;
  1333. var
  1334. p : tlinkedlistitem;
  1335. begin
  1336. p:=inherited getcopy;
  1337. getmem(tai_comment(p).str,strlen(str)+1);
  1338. move(str^,tai_comment(p).str^,strlen(str)+1);
  1339. getcopy:=p;
  1340. end;
  1341. {****************************************************************************
  1342. TAI_STABS
  1343. ****************************************************************************}
  1344. constructor tai_stab.create(_stabtype:TStabType;_str : pchar);
  1345. begin
  1346. inherited create;
  1347. typ:=ait_stab;
  1348. str:=_str;
  1349. stabtype:=_stabtype;
  1350. end;
  1351. constructor tai_stab.create_str(_stabtype:TStabType;const s:string);
  1352. begin
  1353. self.create(_stabtype,strpnew(s));
  1354. end;
  1355. destructor tai_stab.destroy;
  1356. begin
  1357. freemem(str);
  1358. inherited destroy;
  1359. end;
  1360. {****************************************************************************
  1361. TAI_FORCE_LINE
  1362. ****************************************************************************}
  1363. constructor tai_force_line.create;
  1364. begin
  1365. inherited create;
  1366. typ:=ait_force_line;
  1367. end;
  1368. {****************************************************************************
  1369. TAI_FUNCTION_NAME
  1370. ****************************************************************************}
  1371. constructor tai_function_name.create(const s:string);
  1372. begin
  1373. inherited create;
  1374. typ:=ait_function_name;
  1375. funcname:=stringdup(s);
  1376. end;
  1377. destructor tai_function_name.destroy;
  1378. begin
  1379. stringdispose(funcname);
  1380. inherited destroy;
  1381. end;
  1382. {****************************************************************************
  1383. TAI_CUTOBJECT
  1384. ****************************************************************************}
  1385. constructor tai_cutobject.Create;
  1386. begin
  1387. inherited Create;
  1388. typ:=ait_cutobject;
  1389. place:=cut_normal;
  1390. end;
  1391. constructor tai_cutobject.Create_begin;
  1392. begin
  1393. inherited Create;
  1394. typ:=ait_cutobject;
  1395. place:=cut_begin;
  1396. end;
  1397. constructor tai_cutobject.Create_end;
  1398. begin
  1399. inherited Create;
  1400. typ:=ait_cutobject;
  1401. place:=cut_end;
  1402. end;
  1403. constructor tai_cutobject.ppuload(t:taitype;ppufile:tcompilerppufile);
  1404. begin
  1405. inherited ppuload(t,ppufile);
  1406. place:=TCutPlace(ppufile.getbyte);
  1407. end;
  1408. procedure tai_cutobject.ppuwrite(ppufile:tcompilerppufile);
  1409. begin
  1410. inherited ppuwrite(ppufile);
  1411. ppufile.putbyte(byte(place));
  1412. end;
  1413. {****************************************************************************
  1414. Tai_Marker
  1415. ****************************************************************************}
  1416. constructor Tai_Marker.Create(_Kind: TAsmMarker);
  1417. begin
  1418. Inherited Create;
  1419. typ := ait_marker;
  1420. Kind := _Kind;
  1421. end;
  1422. constructor Tai_Marker.ppuload(t:taitype;ppufile:tcompilerppufile);
  1423. begin
  1424. inherited ppuload(t,ppufile);
  1425. kind:=TAsmMarker(ppufile.getbyte);
  1426. end;
  1427. procedure Tai_Marker.ppuwrite(ppufile:tcompilerppufile);
  1428. begin
  1429. inherited ppuwrite(ppufile);
  1430. ppufile.putbyte(byte(kind));
  1431. end;
  1432. {*****************************************************************************
  1433. tai_tempalloc
  1434. *****************************************************************************}
  1435. constructor tai_tempalloc.alloc(pos,size:longint);
  1436. begin
  1437. inherited Create;
  1438. typ:=ait_tempalloc;
  1439. allocation:=true;
  1440. temppos:=pos;
  1441. tempsize:=size;
  1442. {$ifdef EXTDEBUG}
  1443. problem:=nil;
  1444. {$endif EXTDEBUG}
  1445. end;
  1446. destructor tai_tempalloc.destroy;
  1447. begin
  1448. {$ifdef EXTDEBUG}
  1449. stringdispose(problem);
  1450. {$endif EXTDEBUG}
  1451. inherited destroy;
  1452. end;
  1453. constructor tai_tempalloc.dealloc(pos,size:longint);
  1454. begin
  1455. inherited Create;
  1456. typ:=ait_tempalloc;
  1457. allocation:=false;
  1458. temppos:=pos;
  1459. tempsize:=size;
  1460. {$ifdef EXTDEBUG}
  1461. problem:=nil;
  1462. {$endif EXTDEBUG}
  1463. end;
  1464. {$ifdef EXTDEBUG}
  1465. constructor tai_tempalloc.allocinfo(pos,size:longint;const st:string);
  1466. begin
  1467. inherited Create;
  1468. typ:=ait_tempalloc;
  1469. allocation:=false;
  1470. temppos:=pos;
  1471. tempsize:=size;
  1472. problem:=stringdup(st);
  1473. end;
  1474. {$endif EXTDEBUG}
  1475. constructor tai_tempalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  1476. begin
  1477. inherited ppuload(t,ppufile);
  1478. temppos:=ppufile.getlongint;
  1479. tempsize:=ppufile.getlongint;
  1480. allocation:=boolean(ppufile.getbyte);
  1481. {$ifdef EXTDEBUG}
  1482. problem:=nil;
  1483. {$endif EXTDEBUG}
  1484. end;
  1485. procedure tai_tempalloc.ppuwrite(ppufile:tcompilerppufile);
  1486. begin
  1487. inherited ppuwrite(ppufile);
  1488. ppufile.putlongint(temppos);
  1489. ppufile.putlongint(tempsize);
  1490. ppufile.putbyte(byte(allocation));
  1491. end;
  1492. {*****************************************************************************
  1493. tai_regalloc
  1494. *****************************************************************************}
  1495. constructor tai_regalloc.alloc(r : tregister;ainstr:tai);
  1496. begin
  1497. inherited create;
  1498. typ:=ait_regalloc;
  1499. ratype:=ra_alloc;
  1500. reg:=r;
  1501. { ainstr must be an instruction }
  1502. if assigned(ainstr) and
  1503. (ainstr.typ<>ait_instruction) then
  1504. internalerror(200411011);
  1505. instr:=ainstr;
  1506. end;
  1507. constructor tai_regalloc.dealloc(r : tregister;ainstr:tai);
  1508. begin
  1509. inherited create;
  1510. typ:=ait_regalloc;
  1511. ratype:=ra_dealloc;
  1512. reg:=r;
  1513. { ainstr must be an instruction }
  1514. if assigned(ainstr) and
  1515. (ainstr.typ<>ait_instruction) then
  1516. internalerror(200411012);
  1517. instr:=ainstr;
  1518. end;
  1519. constructor tai_regalloc.sync(r : tregister);
  1520. begin
  1521. inherited create;
  1522. typ:=ait_regalloc;
  1523. ratype:=ra_sync;
  1524. reg:=r;
  1525. end;
  1526. constructor tai_regalloc.resize(r : tregister);
  1527. begin
  1528. inherited create;
  1529. typ:=ait_regalloc;
  1530. ratype:=ra_resize;
  1531. reg:=r;
  1532. end;
  1533. constructor tai_regalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  1534. begin
  1535. inherited ppuload(t,ppufile);
  1536. ppufile.getdata(reg,sizeof(Tregister));
  1537. ratype:=tregalloctype(ppufile.getbyte);
  1538. end;
  1539. procedure tai_regalloc.ppuwrite(ppufile:tcompilerppufile);
  1540. begin
  1541. inherited ppuwrite(ppufile);
  1542. ppufile.putdata(reg,sizeof(Tregister));
  1543. ppufile.putbyte(byte(ratype));
  1544. end;
  1545. {****************************************************************************
  1546. tai_file
  1547. ****************************************************************************}
  1548. constructor tai_file.Create(_str : string);
  1549. begin
  1550. inherited Create;
  1551. typ:=ait_file;
  1552. getmem(str,length(_str)+1);
  1553. move(_str[1],str^,length(_str));
  1554. str[length(_str)]:=#0;
  1555. end;
  1556. destructor tai_file.destroy;
  1557. begin
  1558. freemem(str);
  1559. inherited Destroy;
  1560. end;
  1561. constructor tai_file.ppuload(t:taitype;ppufile:tcompilerppufile);
  1562. var
  1563. len : longint;
  1564. begin
  1565. inherited ppuload(t,ppufile);
  1566. len:=ppufile.getlongint;
  1567. getmem(str,len+1);
  1568. ppufile.getdata(str^,len);
  1569. str[len]:=#0;
  1570. idx:=ppufile.getlongint;
  1571. end;
  1572. procedure tai_file.ppuwrite(ppufile:tcompilerppufile);
  1573. var
  1574. len : longint;
  1575. begin
  1576. inherited ppuwrite(ppufile);
  1577. len:=strlen(str);
  1578. ppufile.putlongint(len);
  1579. ppufile.putdata(str^,len);
  1580. ppufile.putlongint(idx);
  1581. end;
  1582. function tai_file.getcopy : tlinkedlistitem;
  1583. var
  1584. p : tlinkedlistitem;
  1585. begin
  1586. p:=inherited getcopy;
  1587. getmem(tai_comment(p).str,strlen(str)+1);
  1588. move(str^,tai_comment(p).str^,strlen(str)+1);
  1589. getcopy:=p;
  1590. end;
  1591. {****************************************************************************
  1592. tai_loc
  1593. ****************************************************************************}
  1594. constructor tai_loc.Create(_fileidx : tai_file;_line,_column : longint);
  1595. begin
  1596. inherited Create;
  1597. typ:=ait_loc;
  1598. fileentry:=_fileidx;
  1599. line:=_line;
  1600. column:=_column;
  1601. end;
  1602. constructor tai_loc.ppuload(t:taitype;ppufile:tcompilerppufile);
  1603. begin
  1604. inherited ppuload(t,ppufile);
  1605. {!!!! fileidx:=ppufile.getlongint; }
  1606. line:=ppufile.getlongint;
  1607. column:=ppufile.getlongint;
  1608. end;
  1609. procedure tai_loc.ppuwrite(ppufile:tcompilerppufile);
  1610. begin
  1611. inherited ppuwrite(ppufile);
  1612. {!!!!! ppufile.putlongint(fileidx); }
  1613. ppufile.putlongint(line);
  1614. ppufile.putlongint(column);
  1615. end;
  1616. {*****************************************************************************
  1617. TaiInstruction
  1618. *****************************************************************************}
  1619. constructor tai_cpu_abstract.Create(op : tasmop);
  1620. begin
  1621. inherited create;
  1622. typ:=ait_instruction;
  1623. is_jmp:=false;
  1624. opcode:=op;
  1625. ops:=0;
  1626. fillchar(condition,sizeof(condition),0);
  1627. fillchar(oper,sizeof(oper),0);
  1628. end;
  1629. destructor tai_cpu_abstract.Destroy;
  1630. var
  1631. i : integer;
  1632. begin
  1633. for i:=0 to opercnt-1 do
  1634. freeop(i);
  1635. inherited destroy;
  1636. end;
  1637. { ---------------------------------------------------------------------
  1638. Loading of operands.
  1639. ---------------------------------------------------------------------}
  1640. procedure tai_cpu_abstract.allocate_oper(opers:longint);
  1641. begin
  1642. while (opers>opercnt) do
  1643. begin
  1644. new(oper[opercnt]);
  1645. fillchar(oper[opercnt]^,sizeof(toper),0);
  1646. inc(opercnt);
  1647. end;
  1648. end;
  1649. procedure tai_cpu_abstract.loadconst(opidx:longint;l:aint);
  1650. begin
  1651. allocate_oper(opidx+1);
  1652. with oper[opidx]^ do
  1653. begin
  1654. if typ<>top_const then
  1655. clearop(opidx);
  1656. val:=l;
  1657. typ:=top_const;
  1658. end;
  1659. end;
  1660. procedure tai_cpu_abstract.loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  1661. var
  1662. r : treference;
  1663. begin
  1664. reference_reset_symbol(r,s,sofs);
  1665. r.refaddr:=addr_full;
  1666. loadref(opidx,r);
  1667. end;
  1668. procedure tai_cpu_abstract.loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset,forceref:boolean);
  1669. begin
  1670. if not assigned(s) then
  1671. internalerror(200204251);
  1672. allocate_oper(opidx+1);
  1673. with oper[opidx]^ do
  1674. begin
  1675. if typ<>top_local then
  1676. begin
  1677. clearop(opidx);
  1678. new(localoper);
  1679. end;
  1680. with oper[opidx]^.localoper^ do
  1681. begin
  1682. localsym:=s;
  1683. localsymofs:=sofs;
  1684. localindexreg:=indexreg;
  1685. localscale:=scale;
  1686. localgetoffset:=getoffset;
  1687. localforceref:=forceref;
  1688. end;
  1689. typ:=top_local;
  1690. end;
  1691. end;
  1692. procedure tai_cpu_abstract.loadref(opidx:longint;const r:treference);
  1693. begin
  1694. allocate_oper(opidx+1);
  1695. with oper[opidx]^ do
  1696. begin
  1697. if typ<>top_ref then
  1698. begin
  1699. clearop(opidx);
  1700. new(ref);
  1701. end;
  1702. ref^:=r;
  1703. {$ifdef x86}
  1704. { We allow this exception for x86, since overloading this would be
  1705. too much of a a speed penalty}
  1706. if (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  1707. segprefix:=ref^.segment;
  1708. {$endif}
  1709. {$ifdef extdebug}
  1710. if (cs_create_pic in current_settings.moduleswitches) and
  1711. assigned(r.symbol) and
  1712. (r.refaddr=addr_no) then
  1713. internalerror(200502052);
  1714. {$endif}
  1715. typ:=top_ref;
  1716. if assigned(add_reg_instruction_hook) then
  1717. begin
  1718. add_reg_instruction_hook(self,ref^.base);
  1719. add_reg_instruction_hook(self,ref^.index);
  1720. end;
  1721. { mark symbol as used }
  1722. if assigned(ref^.symbol) then
  1723. ref^.symbol.increfs;
  1724. end;
  1725. end;
  1726. procedure tai_cpu_abstract.loadreg(opidx:longint;r:tregister);
  1727. begin
  1728. allocate_oper(opidx+1);
  1729. with oper[opidx]^ do
  1730. begin
  1731. if typ<>top_reg then
  1732. clearop(opidx);
  1733. reg:=r;
  1734. typ:=top_reg;
  1735. end;
  1736. if assigned(add_reg_instruction_hook) then
  1737. add_reg_instruction_hook(self,r);
  1738. {$ifdef ARM}
  1739. { R15 is the PC on the ARM thus moves to R15 are jumps.
  1740. Due to speed considerations we don't use a virtual overridden method here.
  1741. Because the pc/r15 isn't handled by the reg. allocator this should never cause
  1742. problems with iregs getting r15.
  1743. }
  1744. is_jmp:=(opcode=A_MOV) and (opidx=0) and (r=NR_R15);
  1745. {$endif ARM}
  1746. end;
  1747. procedure tai_cpu_abstract.loadoper(opidx:longint;o:toper);
  1748. begin
  1749. allocate_oper(opidx+1);
  1750. clearop(opidx);
  1751. oper[opidx]^:=o;
  1752. { copy also the reference }
  1753. with oper[opidx]^ do
  1754. begin
  1755. case typ of
  1756. top_reg:
  1757. begin
  1758. if assigned(add_reg_instruction_hook) then
  1759. add_reg_instruction_hook(self,reg);
  1760. end;
  1761. top_ref:
  1762. begin
  1763. new(ref);
  1764. ref^:=o.ref^;
  1765. if assigned(add_reg_instruction_hook) then
  1766. begin
  1767. add_reg_instruction_hook(self,ref^.base);
  1768. add_reg_instruction_hook(self,ref^.index);
  1769. end;
  1770. end;
  1771. {$ifdef ARM}
  1772. top_shifterop:
  1773. begin
  1774. new(shifterop);
  1775. shifterop^:=o.shifterop^;
  1776. if assigned(add_reg_instruction_hook) then
  1777. add_reg_instruction_hook(self,shifterop^.rs);
  1778. end;
  1779. {$endif ARM}
  1780. end;
  1781. end;
  1782. end;
  1783. procedure tai_cpu_abstract.clearop(opidx:longint);
  1784. begin
  1785. with oper[opidx]^ do
  1786. begin
  1787. case typ of
  1788. top_ref:
  1789. dispose(ref);
  1790. top_local:
  1791. dispose(localoper);
  1792. {$ifdef ARM}
  1793. top_shifterop:
  1794. dispose(shifterop);
  1795. top_regset:
  1796. dispose(regset);
  1797. {$endif ARM}
  1798. end;
  1799. typ:=top_none;
  1800. end;
  1801. end;
  1802. procedure tai_cpu_abstract.freeop(opidx:longint);
  1803. begin
  1804. clearop(opidx);
  1805. dispose(oper[opidx]);
  1806. end;
  1807. { ---------------------------------------------------------------------
  1808. Miscellaneous methods.
  1809. ---------------------------------------------------------------------}
  1810. procedure tai_cpu_abstract.SetCondition(const c:TAsmCond);
  1811. begin
  1812. condition:=c;
  1813. end;
  1814. Function tai_cpu_abstract.getcopy:TLinkedListItem;
  1815. var
  1816. i : longint;
  1817. p : tai_cpu_abstract;
  1818. begin
  1819. p:=tai_cpu_abstract(inherited getcopy);
  1820. { make a copy of the references }
  1821. p.opercnt:=0;
  1822. p.allocate_oper(ops);
  1823. for i:=0 to ops-1 do
  1824. begin
  1825. p.oper[i]^:=oper[i]^;
  1826. case oper[i]^.typ of
  1827. top_local :
  1828. begin
  1829. new(p.oper[i]^.localoper);
  1830. p.oper[i]^.localoper^:=oper[i]^.localoper^;
  1831. end;
  1832. top_ref :
  1833. begin
  1834. new(p.oper[i]^.ref);
  1835. p.oper[i]^.ref^:=oper[i]^.ref^;
  1836. end;
  1837. {$ifdef ARM}
  1838. top_shifterop:
  1839. begin
  1840. new(p.oper[i]^.shifterop);
  1841. p.oper[i]^.shifterop^:=oper[i]^.shifterop^;
  1842. end;
  1843. {$endif ARM}
  1844. end;
  1845. end;
  1846. getcopy:=p;
  1847. end;
  1848. function tai_cpu_abstract.is_same_reg_move(regtype: Tregistertype):boolean;
  1849. begin
  1850. { When the generic RA is used this needs to be overriden, we don't use
  1851. virtual;abstract; to prevent a lot of warnings of unimplemented abstract methods
  1852. when tai_cpu is created (PFV) }
  1853. internalerror(200404091);
  1854. result:=false;
  1855. end;
  1856. function tai_cpu_abstract.spilling_get_operation_type(opnr: longint): topertype;
  1857. begin
  1858. internalerror(200404091);
  1859. result:=operand_readwrite;
  1860. end;
  1861. function tai_cpu_abstract.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
  1862. begin
  1863. result := operand_read;
  1864. end;
  1865. constructor tai_cpu_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  1866. var
  1867. i : integer;
  1868. begin
  1869. inherited ppuload(t,ppufile);
  1870. { hopefully, we don't get problems with big/litte endian here when cross compiling :/ }
  1871. ppufile.getdata(condition,sizeof(tasmcond));
  1872. allocate_oper(ppufile.getbyte);
  1873. for i:=0 to ops-1 do
  1874. ppuloadoper(ppufile,oper[i]^);
  1875. opcode:=tasmop(ppufile.getword);
  1876. {$ifdef x86}
  1877. ppufile.getdata(segprefix,sizeof(Tregister));
  1878. {$endif x86}
  1879. is_jmp:=boolean(ppufile.getbyte);
  1880. end;
  1881. procedure tai_cpu_abstract.ppuwrite(ppufile:tcompilerppufile);
  1882. var
  1883. i : integer;
  1884. begin
  1885. inherited ppuwrite(ppufile);
  1886. ppufile.putdata(condition,sizeof(tasmcond));
  1887. ppufile.putbyte(ops);
  1888. for i:=0 to ops-1 do
  1889. ppuwriteoper(ppufile,oper[i]^);
  1890. ppufile.putword(word(opcode));
  1891. {$ifdef x86}
  1892. ppufile.putdata(segprefix,sizeof(Tregister));
  1893. {$endif x86}
  1894. ppufile.putbyte(byte(is_jmp));
  1895. end;
  1896. procedure tai_cpu_abstract.buildderefimpl;
  1897. var
  1898. i : integer;
  1899. begin
  1900. for i:=0 to ops-1 do
  1901. ppubuildderefimploper(oper[i]^);
  1902. end;
  1903. procedure tai_cpu_abstract.derefimpl;
  1904. var
  1905. i : integer;
  1906. begin
  1907. for i:=0 to ops-1 do
  1908. ppuderefoper(oper[i]^);
  1909. end;
  1910. procedure tai_cpu_abstract.resetpass1;
  1911. begin
  1912. end;
  1913. procedure tai_cpu_abstract.resetpass2;
  1914. begin
  1915. end;
  1916. function tai_cpu_abstract.Pass1(objdata:TObjData):longint;
  1917. begin
  1918. result:=0;
  1919. end;
  1920. procedure tai_cpu_abstract.Pass2(objdata:TObjData);
  1921. begin
  1922. end;
  1923. procedure tai_cpu_abstract.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  1924. begin
  1925. o.typ:=toptype(ppufile.getbyte);
  1926. o.ot:=ppufile.getlongint;
  1927. case o.typ of
  1928. top_reg :
  1929. ppufile.getdata(o.reg,sizeof(Tregister));
  1930. top_ref :
  1931. begin
  1932. new(o.ref);
  1933. {$ifdef x86}
  1934. ppufile.getdata(o.ref^.segment,sizeof(Tregister));
  1935. {$endif x86}
  1936. ppufile.getdata(o.ref^.base,sizeof(Tregister));
  1937. ppufile.getdata(o.ref^.index,sizeof(Tregister));
  1938. ppufile.getdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));
  1939. o.ref^.scalefactor:=ppufile.getbyte;
  1940. o.ref^.offset:=ppufile.getaint;
  1941. o.ref^.symbol:=ppufile.getasmsymbol;
  1942. o.ref^.relsymbol:=ppufile.getasmsymbol;
  1943. end;
  1944. top_const :
  1945. o.val:=ppufile.getaint;
  1946. top_local :
  1947. begin
  1948. new(o.localoper);
  1949. with o.localoper^ do
  1950. begin
  1951. ppufile.getderef(localsymderef);
  1952. localsymofs:=ppufile.getaint;
  1953. localindexreg:=tregister(ppufile.getlongint);
  1954. localscale:=ppufile.getbyte;
  1955. localgetoffset:=(ppufile.getbyte<>0);
  1956. end;
  1957. end;
  1958. else
  1959. internalerror(2007010210);
  1960. end;
  1961. end;
  1962. procedure tai_cpu_abstract.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  1963. begin
  1964. ppufile.putbyte(byte(o.typ));
  1965. ppufile.putlongint(o.ot);
  1966. case o.typ of
  1967. top_reg :
  1968. ppufile.putdata(o.reg,sizeof(Tregister));
  1969. top_ref :
  1970. begin
  1971. {$ifdef x86}
  1972. ppufile.putdata(o.ref^.segment,sizeof(Tregister));
  1973. {$endif x86}
  1974. ppufile.putdata(o.ref^.base,sizeof(Tregister));
  1975. ppufile.putdata(o.ref^.index,sizeof(Tregister));
  1976. ppufile.putdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));
  1977. ppufile.putbyte(o.ref^.scalefactor);
  1978. ppufile.putaint(o.ref^.offset);
  1979. ppufile.putasmsymbol(o.ref^.symbol);
  1980. ppufile.putasmsymbol(o.ref^.relsymbol);
  1981. end;
  1982. top_const :
  1983. ppufile.putaint(o.val);
  1984. top_local :
  1985. begin
  1986. with o.localoper^ do
  1987. begin
  1988. ppufile.putderef(localsymderef);
  1989. ppufile.putaint(localsymofs);
  1990. ppufile.putlongint(longint(localindexreg));
  1991. ppufile.putbyte(localscale);
  1992. ppufile.putbyte(byte(localgetoffset));
  1993. end;
  1994. end;
  1995. else
  1996. internalerror(2007010211);
  1997. end;
  1998. end;
  1999. {****************************************************************************
  2000. tai_align_abstract
  2001. ****************************************************************************}
  2002. constructor tai_align_abstract.Create(b: byte);
  2003. begin
  2004. inherited Create;
  2005. typ:=ait_align;
  2006. if b in [1,2,4,8,16,32] then
  2007. aligntype := b
  2008. else
  2009. aligntype := 1;
  2010. fillsize:=0;
  2011. fillop:=0;
  2012. use_op:=false;
  2013. end;
  2014. constructor tai_align_abstract.Create_op(b: byte; _op: byte);
  2015. begin
  2016. inherited Create;
  2017. typ:=ait_align;
  2018. if b in [1,2,4,8,16,32] then
  2019. aligntype := b
  2020. else
  2021. aligntype := 1;
  2022. fillsize:=0;
  2023. fillop:=_op;
  2024. use_op:=true;
  2025. end;
  2026. constructor tai_align_abstract.Create_zeros(b: byte);
  2027. begin
  2028. inherited Create;
  2029. typ:=ait_align;
  2030. if b in [1,2,4,8,16,32] then
  2031. aligntype := b
  2032. else
  2033. aligntype := 1;
  2034. use_op:=true;
  2035. fillsize:=0;
  2036. fillop:=0;
  2037. end;
  2038. function tai_align_abstract.calculatefillbuf(var buf : tfillbuffer):pchar;
  2039. begin
  2040. if fillsize>sizeof(buf) then
  2041. internalerror(200404293);
  2042. fillchar(buf,high(buf),fillop);
  2043. calculatefillbuf:=pchar(@buf);
  2044. end;
  2045. constructor tai_align_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  2046. begin
  2047. inherited ppuload(t,ppufile);
  2048. aligntype:=ppufile.getbyte;
  2049. fillsize:=0;
  2050. fillop:=ppufile.getbyte;
  2051. use_op:=boolean(ppufile.getbyte);
  2052. end;
  2053. procedure tai_align_abstract.ppuwrite(ppufile:tcompilerppufile);
  2054. begin
  2055. inherited ppuwrite(ppufile);
  2056. ppufile.putbyte(aligntype);
  2057. ppufile.putbyte(fillop);
  2058. ppufile.putbyte(byte(use_op));
  2059. end;
  2060. begin
  2061. cai_cpu:=tai_cpu_abstract;
  2062. cai_align:=tai_align_abstract;
  2063. end.