aasmtai.pas 67 KB

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