aasmtai.pas 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements an abstract asmoutput class for all processor types
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. { @abstract(This unit implements an abstract asm output class for all processor types)
  19. This unit implements an abstract assembler output class for all processors, these
  20. are then overriden for each assembler writer to actually write the data in these
  21. classes to an assembler file.
  22. }
  23. unit aasmtai;
  24. {$i fpcdefs.inc}
  25. interface
  26. uses
  27. cutils,cclasses,
  28. globtype,globals,systems,
  29. cpuinfo,cpubase,
  30. cgbase,
  31. symppu,symtype,
  32. aasmbase;
  33. type
  34. taitype = (
  35. ait_none,
  36. ait_align,
  37. ait_section,
  38. ait_comment,
  39. ait_direct,
  40. ait_string,
  41. ait_instruction,
  42. ait_datablock,
  43. ait_symbol,
  44. ait_symbol_end, { needed to calc the size of a symbol }
  45. ait_label,
  46. ait_const_32bit,
  47. ait_const_16bit,
  48. ait_const_8bit,
  49. ait_const_symbol,
  50. { the following is only used by the win32 version of the compiler }
  51. { and only the GNU AS Win32 is able to write it }
  52. ait_const_rva,
  53. ait_real_32bit,
  54. ait_real_64bit,
  55. ait_real_80bit,
  56. ait_comp_64bit,
  57. ait_real_128bit,
  58. {$ifdef GDB}
  59. ait_stabn,
  60. ait_stabs,
  61. ait_force_line,
  62. ait_stab_function_name,
  63. {$endif GDB}
  64. {$ifdef alpha}
  65. { the follow is for the DEC Alpha }
  66. ait_frame,
  67. ait_ent,
  68. {$endif alpha}
  69. {$ifdef ia64}
  70. ait_bundle,
  71. ait_stop,
  72. {$endif ia64}
  73. {$ifdef m68k}
  74. ait_labeled_instruction,
  75. {$endif m68k}
  76. {$ifdef SPARC}
  77. ait_labeled_instruction,
  78. {$endif SPARC}
  79. ait_cut, { used to split into tiny assembler files }
  80. ait_regalloc,
  81. ait_tempalloc,
  82. ait_marker { used to mark assembler blocks and inlined functions }
  83. );
  84. const
  85. taitypestr : array[taitype] of string[14] = (
  86. '<none>',
  87. 'align',
  88. 'section',
  89. 'comment',
  90. 'direct',
  91. 'string',
  92. 'instruction',
  93. 'datablock',
  94. 'symbol',
  95. 'symbol_end',
  96. 'label',
  97. 'const_32bit',
  98. 'const_16bit',
  99. 'const_8bit',
  100. 'const_symbol',
  101. 'const_rva',
  102. 'real_32bit',
  103. 'real_64bit',
  104. 'real_80bit',
  105. 'comp_64bit',
  106. 'real_128bit',
  107. {$ifdef GDB}
  108. 'stabn',
  109. 'stabs',
  110. 'force_line',
  111. 'stab_funcname',
  112. {$endif GDB}
  113. {$ifdef alpha}
  114. { the follow is for the DEC Alpha }
  115. 'frame',
  116. 'ent',
  117. {$endif alpha}
  118. {$ifdef ia64}
  119. 'bundle',
  120. 'stop',
  121. {$endif ia64}
  122. {$ifdef m68k}
  123. 'labeled_instr',
  124. {$endif m68k}
  125. {$ifdef SPARC}
  126. 'labeled_instr',
  127. {$endif SPARC}
  128. 'cut',
  129. 'regalloc',
  130. 'tempalloc',
  131. 'marker'
  132. );
  133. type
  134. { Types of operand }
  135. toptype=(top_none,top_reg,top_ref,top_const,top_symbol,top_bool,top_local);
  136. toper=record
  137. ot : longint;
  138. case typ : toptype of
  139. top_none : ();
  140. top_reg : (reg:tregister);
  141. top_ref : (ref:preference);
  142. top_const : (val:aword);
  143. top_symbol : (sym:tasmsymbol;symofs:longint);
  144. top_bool : (b:boolean);
  145. { local varsym that will be inserted in pass_2 }
  146. top_local : (localsym:pointer;localsymderef:tderef;localsymofs:longint);
  147. end;
  148. { ait_* types which don't result in executable code or which don't influence }
  149. { the way the program runs/behaves, but which may be encountered by the }
  150. { optimizer (= if it's sometimes added to the exprasm list). Update if you add }
  151. { a new ait type! }
  152. const
  153. SkipInstr = [ait_comment, ait_symbol,ait_section
  154. {$ifdef GDB}
  155. ,ait_stabs, ait_stabn, ait_stab_function_name, ait_force_line
  156. {$endif GDB}
  157. ,ait_regalloc, ait_tempalloc, ait_symbol_end];
  158. { ait_* types which do not have line information (and hence which are of type
  159. tai, otherwise, they are of type tailineinfo }
  160. { ait_* types which do not have line information (and hence which are of type
  161. tai, otherwise, they are of type tailineinfo }
  162. SkipLineInfo =[ait_label,
  163. ait_regalloc,ait_tempalloc,
  164. {$ifdef GDB}
  165. ait_stabn,ait_stabs,ait_stab_function_name,
  166. {$endif GDB}
  167. ait_cut,ait_marker,ait_align,ait_section,ait_comment,
  168. ait_const_8bit,ait_const_16bit,ait_const_32bit,
  169. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit
  170. ];
  171. type
  172. { cut type, required for alphanumeric ordering of the assembler filenames }
  173. TCutPlace=(cut_normal,cut_begin,cut_end);
  174. TMarker = (NoPropInfoStart,NoPropInfoEnd,
  175. AsmBlockStart,AsmBlockEnd,
  176. InlineStart,InlineEnd,marker_blockstart);
  177. { Buffer type used for alignment }
  178. tfillbuffer = array[0..63] of char;
  179. Tspill_temp_list=array[0..255] of Treference;
  180. { abstract assembler item }
  181. tai = class(TLinkedListItem)
  182. {$ifndef NOOPT}
  183. { pointer to record with optimizer info about this tai object }
  184. optinfo : pointer;
  185. {$endif NOOPT}
  186. typ : taitype;
  187. constructor Create;
  188. constructor ppuload(t:taitype;ppufile:tcompilerppufile);virtual;
  189. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  190. procedure derefimpl;virtual;
  191. end;
  192. { abstract assembler item with line information }
  193. tailineinfo = class(tai)
  194. fileinfo : tfileposinfo;
  195. constructor Create;
  196. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  197. procedure ppuwrite(ppufile:tcompilerppufile);override;
  198. end;
  199. taiclass = class of tai;
  200. taiclassarray = array[taitype] of taiclass;
  201. { Generates an assembler string }
  202. tai_string = class(tailineinfo)
  203. str : pchar;
  204. { extra len so the string can contain an \0 }
  205. len : longint;
  206. constructor Create(const _str : string);
  207. constructor Create_pchar(_str : pchar);
  208. constructor Create_length_pchar(_str : pchar;length : longint);
  209. destructor Destroy;override;
  210. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  211. procedure ppuwrite(ppufile:tcompilerppufile);override;
  212. function getcopy:tlinkedlistitem;override;
  213. end;
  214. { Generates a common label }
  215. tai_symbol = class(tailineinfo)
  216. is_global : boolean;
  217. sym : tasmsymbol;
  218. size : longint;
  219. constructor Create(_sym:tasmsymbol;siz:longint);
  220. constructor Create_Global(_sym:tasmsymbol;siz:longint);
  221. constructor Createname(const _name : string;siz:longint);
  222. constructor Createname_global(const _name : string;siz:longint);
  223. constructor Createdataname(const _name : string;siz:longint);
  224. constructor Createdataname_global(const _name : string;siz:longint);
  225. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  226. procedure ppuwrite(ppufile:tcompilerppufile);override;
  227. procedure derefimpl;override;
  228. end;
  229. tai_symbol_end = class(tailineinfo)
  230. sym : tasmsymbol;
  231. constructor Create(_sym:tasmsymbol);
  232. constructor Createname(const _name : string);
  233. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  234. procedure ppuwrite(ppufile:tcompilerppufile);override;
  235. procedure derefimpl;override;
  236. end;
  237. { Generates an assembler label }
  238. tai_label = class(tai)
  239. is_global : boolean;
  240. l : tasmlabel;
  241. constructor Create(_l : tasmlabel);
  242. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  243. procedure ppuwrite(ppufile:tcompilerppufile);override;
  244. procedure derefimpl;override;
  245. end;
  246. { Directly output data to final assembler file }
  247. tai_direct = class(tailineinfo)
  248. str : pchar;
  249. constructor Create(_str : pchar);
  250. destructor Destroy; override;
  251. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  252. procedure ppuwrite(ppufile:tcompilerppufile);override;
  253. function getcopy:tlinkedlistitem;override;
  254. end;
  255. { Generates an assembler comment }
  256. tai_comment = class(tai)
  257. str : pchar;
  258. constructor Create(_str : pchar);
  259. destructor Destroy; override;
  260. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  261. procedure ppuwrite(ppufile:tcompilerppufile);override;
  262. function getcopy:tlinkedlistitem;override;
  263. end;
  264. { Generates a section / segment directive }
  265. tai_section = class(tai)
  266. sec : TSection;
  267. constructor Create(s : TSection);
  268. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  269. procedure ppuwrite(ppufile:tcompilerppufile);override;
  270. end;
  271. { Generates an uninitializised data block }
  272. tai_datablock = class(tailineinfo)
  273. is_global : boolean;
  274. sym : tasmsymbol;
  275. size : longint;
  276. constructor Create(const _name : string;_size : longint);
  277. constructor Create_global(const _name : string;_size : longint);
  278. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  279. procedure ppuwrite(ppufile:tcompilerppufile);override;
  280. procedure derefimpl;override;
  281. end;
  282. { Generates a long integer (32 bit) }
  283. tai_const = class(tai)
  284. value : longint;
  285. constructor Create_32bit(_value : longint);
  286. constructor Create_16bit(_value : word);
  287. constructor Create_8bit(_value : byte);
  288. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  289. procedure ppuwrite(ppufile:tcompilerppufile);override;
  290. end;
  291. tai_const_symbol = class(tailineinfo)
  292. sym : tasmsymbol;
  293. offset : longint;
  294. constructor Create(_sym:tasmsymbol);
  295. constructor Create_offset(_sym:tasmsymbol;ofs:longint);
  296. constructor Create_rva(_sym:tasmsymbol);
  297. constructor Createname(const name:string);
  298. constructor Createname_offset(const name:string;ofs:longint);
  299. constructor Createname_rva(const name:string);
  300. constructor Createdataname(const name:string);
  301. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  302. procedure ppuwrite(ppufile:tcompilerppufile);override;
  303. procedure derefimpl;override;
  304. function getcopy:tlinkedlistitem;override;
  305. end;
  306. { Generates a single float (32 bit real) }
  307. tai_real_32bit = class(tai)
  308. value : ts32real;
  309. constructor Create(_value : ts32real);
  310. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  311. procedure ppuwrite(ppufile:tcompilerppufile);override;
  312. end;
  313. { Generates a double float (64 bit real) }
  314. tai_real_64bit = class(tai)
  315. value : ts64real;
  316. constructor Create(_value : ts64real);
  317. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  318. procedure ppuwrite(ppufile:tcompilerppufile);override;
  319. end;
  320. { Generates an extended float (80 bit real) }
  321. tai_real_80bit = class(tai)
  322. value : ts80real;
  323. constructor Create(_value : ts80real);
  324. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  325. procedure ppuwrite(ppufile:tcompilerppufile);override;
  326. end;
  327. { Generates a comp int (integer over 64 bits)
  328. This is Intel 80x86 specific, and is not
  329. really supported on other processors.
  330. }
  331. tai_comp_64bit = class(tai)
  332. value : ts64comp;
  333. constructor Create(_value : ts64comp);
  334. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  335. procedure ppuwrite(ppufile:tcompilerppufile);override;
  336. end;
  337. { Insert a cut to split assembler into several smaller files }
  338. tai_cut = class(tai)
  339. place : tcutplace;
  340. constructor Create;
  341. constructor Create_begin;
  342. constructor Create_end;
  343. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  344. procedure ppuwrite(ppufile:tcompilerppufile);override;
  345. end;
  346. { Insert a marker for assembler and inline blocks }
  347. tai_marker = class(tai)
  348. Kind: TMarker;
  349. Constructor Create(_Kind: TMarker);
  350. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  351. procedure ppuwrite(ppufile:tcompilerppufile);override;
  352. end;
  353. tai_tempalloc = class(tai)
  354. allocation : boolean;
  355. {$ifdef EXTDEBUG}
  356. problem : pstring;
  357. {$endif EXTDEBUG}
  358. temppos,
  359. tempsize : longint;
  360. constructor alloc(pos,size:longint);
  361. constructor dealloc(pos,size:longint);
  362. {$ifdef EXTDEBUG}
  363. constructor allocinfo(pos,size:longint;const st:string);
  364. {$endif EXTDEBUG}
  365. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  366. destructor destroy;override;
  367. procedure ppuwrite(ppufile:tcompilerppufile);override;
  368. end;
  369. tai_regalloc = class(tai)
  370. allocation : boolean;
  371. reg : tregister;
  372. constructor alloc(r : tregister);
  373. constructor dealloc(r : tregister);
  374. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  375. procedure ppuwrite(ppufile:tcompilerppufile);override;
  376. end;
  377. Taasmoutput=class;
  378. Trggetproc=procedure(list:Taasmoutput;position:Tai;subreg:Tsubregister;var result:Tregister) of object;
  379. Trgungetproc=procedure(list:Taasmoutput;position:Tai;r:Tregister) of object;
  380. { Class template for assembler instructions
  381. }
  382. taicpu_abstract = class(tailineinfo)
  383. protected
  384. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);virtual;abstract;
  385. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);virtual;abstract;
  386. procedure ppuderefoper(var o:toper);virtual;abstract;
  387. public
  388. { Condition flags for instruction }
  389. condition : TAsmCond;
  390. { Number of operands to instruction }
  391. ops : byte;
  392. { Operands of instruction }
  393. oper : array[0..max_operands-1] of toper;
  394. { Actual opcode of instruction }
  395. opcode : tasmop;
  396. {$ifdef x86}
  397. segprefix : tregister;
  398. {$endif x86}
  399. { true if instruction is a jmp }
  400. is_jmp : boolean; { is this instruction a jump? (needed for optimizer) }
  401. Constructor Create(op : tasmop);
  402. Destructor Destroy;override;
  403. function getcopy:TLinkedListItem;override;
  404. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  405. procedure ppuwrite(ppufile:tcompilerppufile);override;
  406. procedure derefimpl;override;
  407. procedure SetCondition(const c:TAsmCond);
  408. procedure loadconst(opidx:longint;l:aword);
  409. procedure loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  410. procedure loadlocal(opidx:longint;s:pointer;sofs:longint);
  411. procedure loadref(opidx:longint;const r:treference);
  412. procedure loadreg(opidx:longint;r:tregister);
  413. procedure loadoper(opidx:longint;o:toper);
  414. procedure clearop(opidx:longint);
  415. function is_nop:boolean;virtual;abstract;
  416. function is_move:boolean;virtual;abstract;
  417. { register allocator }
  418. function get_insert_pos(p:Tai;huntfor1,huntfor2,huntfor3:Tsuperregister;var unusedregsint:Tsuperregisterset):Tai;
  419. procedure forward_allocation(p:Tai;var unusedregsint:Tsuperregisterset);
  420. function spill_registers(list:Taasmoutput;
  421. rgget:Trggetproc;
  422. rgunget:Trgungetproc;
  423. r:tsuperregisterset;
  424. var unusedregsint:Tsuperregisterset;
  425. const spilltemplist:Tspill_temp_list):boolean;virtual;
  426. function spilling_decode_loadstore(op: tasmop; var counterpart: tasmop; var wasload: boolean): boolean;virtual;abstract;
  427. function spilling_create_loadstore(op: tasmop; r:tregister; const ref:treference): tai;virtual;abstract;
  428. function spilling_create_load(const ref:treference;r:tregister): tai;virtual;abstract;
  429. function spilling_create_store(r:tregister; const ref:treference): tai;virtual;abstract;
  430. end;
  431. { alignment for operator }
  432. tai_align_abstract = class(tai)
  433. aligntype : byte; { 1 = no align, 2 = word align, 4 = dword align }
  434. fillsize : byte; { real size to fill }
  435. fillop : byte; { value to fill with - optional }
  436. use_op : boolean;
  437. constructor Create(b:byte);
  438. constructor Create_op(b: byte; _op: byte);
  439. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  440. procedure ppuwrite(ppufile:tcompilerppufile);override;
  441. function calculatefillbuf(var buf : tfillbuffer):pchar;virtual;
  442. end;
  443. Ttranstable=array[Tsuperregister] of Tsuperregister;
  444. taasmoutput = class(tlinkedlist)
  445. constructor create;
  446. function getlasttaifilepos : pfileposinfo;
  447. // procedure translate_registers(const table:Ttranstable);
  448. procedure InsertAfter(Item,Loc : TLinkedListItem);
  449. procedure translate_registers(regtype:tregistertype;const table:Ttranstable);
  450. end;
  451. var
  452. { array with all class types for tais }
  453. aiclass : taiclassarray;
  454. { Current expression list }
  455. exprasmlist : taasmoutput;
  456. { labels for BREAK and CONTINUE }
  457. aktbreaklabel,aktcontinuelabel : tasmlabel;
  458. { label when the result is true or false }
  459. truelabel,falselabel : tasmlabel;
  460. { default lists }
  461. datasegment,codesegment,bsssegment,
  462. debuglist,withdebuglist,consts,
  463. importssection,exportssection,
  464. resourcesection,rttilist,
  465. resourcestringlist : taasmoutput;
  466. function ppuloadai(ppufile:tcompilerppufile):tai;
  467. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  468. implementation
  469. uses
  470. {$ifdef delphi}
  471. sysutils,
  472. {$else}
  473. strings,
  474. {$endif}
  475. verbose;
  476. const
  477. pputaimarker = 254;
  478. {****************************************************************************
  479. Helpers
  480. ****************************************************************************}
  481. function ppuloadai(ppufile:tcompilerppufile):tai;
  482. var
  483. b : byte;
  484. t : taitype;
  485. begin
  486. { marker }
  487. b:=ppufile.getbyte;
  488. if b<>pputaimarker then
  489. internalerror(200208181);
  490. { load nodetype }
  491. t:=taitype(ppufile.getbyte);
  492. if t<>ait_none then
  493. begin
  494. if t>high(taitype) then
  495. internalerror(200208182);
  496. if not assigned(aiclass[t]) then
  497. internalerror(200208183);
  498. {writeln('taiload: ',taitypestr[t]);}
  499. { generate tai of the correct class }
  500. ppuloadai:=aiclass[t].ppuload(t,ppufile);
  501. end
  502. else
  503. ppuloadai:=nil;
  504. end;
  505. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  506. begin
  507. { marker, read by ppuloadnode }
  508. ppufile.putbyte(pputaimarker);
  509. if assigned(n) then
  510. begin
  511. { type, read by ppuloadnode }
  512. ppufile.putbyte(byte(n.typ));
  513. {writeln('taiwrite: ',taitypestr[n.typ]);}
  514. n.ppuwrite(ppufile);
  515. end
  516. else
  517. ppufile.putbyte(byte(ait_none));
  518. end;
  519. {****************************************************************************
  520. TAI
  521. ****************************************************************************}
  522. constructor tai.Create;
  523. begin
  524. {$ifndef NOOPT}
  525. optinfo:=nil;
  526. {$endif NOOPT}
  527. end;
  528. constructor tai.ppuload(t:taitype;ppufile:tcompilerppufile);
  529. begin
  530. typ:=t;
  531. {$ifndef NOOPT}
  532. optinfo:=nil;
  533. {$endif}
  534. end;
  535. procedure tai.ppuwrite(ppufile:tcompilerppufile);
  536. begin
  537. end;
  538. procedure tai.derefimpl;
  539. begin
  540. end;
  541. {****************************************************************************
  542. TAILINEINFO
  543. ****************************************************************************}
  544. constructor tailineinfo.create;
  545. begin
  546. inherited create;
  547. fileinfo:=aktfilepos;
  548. end;
  549. constructor tailineinfo.ppuload(t:taitype;ppufile:tcompilerppufile);
  550. begin
  551. inherited ppuload(t,ppufile);
  552. ppufile.getposinfo(fileinfo);
  553. end;
  554. procedure tailineinfo.ppuwrite(ppufile:tcompilerppufile);
  555. begin
  556. inherited ppuwrite(ppufile);
  557. ppufile.putposinfo(fileinfo);
  558. end;
  559. {****************************************************************************
  560. TAI_SECTION
  561. ****************************************************************************}
  562. constructor tai_section.Create(s : TSection);
  563. begin
  564. inherited Create;
  565. typ:=ait_section;
  566. sec:=s;
  567. end;
  568. constructor tai_section.ppuload(t:taitype;ppufile:tcompilerppufile);
  569. begin
  570. inherited ppuload(t,ppufile);
  571. sec:=tsection(ppufile.getbyte);
  572. end;
  573. procedure tai_section.ppuwrite(ppufile:tcompilerppufile);
  574. begin
  575. inherited ppuwrite(ppufile);
  576. ppufile.putbyte(byte(sec));
  577. end;
  578. {****************************************************************************
  579. TAI_DATABLOCK
  580. ****************************************************************************}
  581. constructor tai_datablock.Create(const _name : string;_size : longint);
  582. begin
  583. inherited Create;
  584. typ:=ait_datablock;
  585. sym:=objectlibrary.newasmsymboltype(_name,AB_LOCAL,AT_DATA);
  586. { keep things aligned }
  587. if _size<=0 then
  588. _size:=4;
  589. size:=_size;
  590. is_global:=false;
  591. end;
  592. constructor tai_datablock.Create_global(const _name : string;_size : longint);
  593. begin
  594. inherited Create;
  595. typ:=ait_datablock;
  596. sym:=objectlibrary.newasmsymboltype(_name,AB_GLOBAL,AT_DATA);
  597. { keep things aligned }
  598. if _size<=0 then
  599. _size:=4;
  600. size:=_size;
  601. is_global:=true;
  602. end;
  603. constructor tai_datablock.ppuload(t:taitype;ppufile:tcompilerppufile);
  604. begin
  605. inherited Create;
  606. sym:=ppufile.getasmsymbol;
  607. size:=ppufile.getlongint;
  608. is_global:=boolean(ppufile.getbyte);
  609. end;
  610. procedure tai_datablock.ppuwrite(ppufile:tcompilerppufile);
  611. begin
  612. inherited ppuwrite(ppufile);
  613. ppufile.putasmsymbol(sym);
  614. ppufile.putlongint(size);
  615. ppufile.putbyte(byte(is_global));
  616. end;
  617. procedure tai_datablock.derefimpl;
  618. begin
  619. objectlibrary.DerefAsmsymbol(sym);
  620. end;
  621. {****************************************************************************
  622. TAI_SYMBOL
  623. ****************************************************************************}
  624. constructor tai_symbol.Create(_sym:tasmsymbol;siz:longint);
  625. begin
  626. inherited Create;
  627. typ:=ait_symbol;
  628. sym:=_sym;
  629. size:=siz;
  630. sym.defbind:=AB_LOCAL;
  631. is_global:=false;
  632. end;
  633. constructor tai_symbol.Create_global(_sym:tasmsymbol;siz:longint);
  634. begin
  635. inherited Create;
  636. typ:=ait_symbol;
  637. sym:=_sym;
  638. size:=siz;
  639. sym.defbind:=AB_GLOBAL;
  640. is_global:=true;
  641. end;
  642. constructor tai_symbol.Createname(const _name : string;siz:longint);
  643. begin
  644. inherited Create;
  645. typ:=ait_symbol;
  646. sym:=objectlibrary.newasmsymboltype(_name,AB_LOCAL,AT_FUNCTION);
  647. size:=siz;
  648. is_global:=false;
  649. end;
  650. constructor tai_symbol.Createname_global(const _name : string;siz:longint);
  651. begin
  652. inherited Create;
  653. typ:=ait_symbol;
  654. sym:=objectlibrary.newasmsymboltype(_name,AB_GLOBAL,AT_FUNCTION);
  655. size:=siz;
  656. is_global:=true;
  657. end;
  658. constructor tai_symbol.Createdataname(const _name : string;siz:longint);
  659. begin
  660. inherited Create;
  661. typ:=ait_symbol;
  662. sym:=objectlibrary.newasmsymboltype(_name,AB_LOCAL,AT_DATA);
  663. size:=siz;
  664. is_global:=false;
  665. end;
  666. constructor tai_symbol.Createdataname_global(const _name : string;siz:longint);
  667. begin
  668. inherited Create;
  669. typ:=ait_symbol;
  670. sym:=objectlibrary.newasmsymboltype(_name,AB_GLOBAL,AT_DATA);
  671. size:=siz;
  672. is_global:=true;
  673. end;
  674. constructor tai_symbol.ppuload(t:taitype;ppufile:tcompilerppufile);
  675. begin
  676. inherited ppuload(t,ppufile);
  677. sym:=ppufile.getasmsymbol;
  678. size:=ppufile.getlongint;
  679. is_global:=boolean(ppufile.getbyte);
  680. end;
  681. procedure tai_symbol.ppuwrite(ppufile:tcompilerppufile);
  682. begin
  683. inherited ppuwrite(ppufile);
  684. ppufile.putasmsymbol(sym);
  685. ppufile.putlongint(size);
  686. ppufile.putbyte(byte(is_global));
  687. end;
  688. procedure tai_symbol.derefimpl;
  689. begin
  690. objectlibrary.DerefAsmsymbol(sym);
  691. end;
  692. {****************************************************************************
  693. TAI_SYMBOL
  694. ****************************************************************************}
  695. constructor tai_symbol_end.Create(_sym:tasmsymbol);
  696. begin
  697. inherited Create;
  698. typ:=ait_symbol_end;
  699. sym:=_sym;
  700. end;
  701. constructor tai_symbol_end.Createname(const _name : string);
  702. begin
  703. inherited Create;
  704. typ:=ait_symbol_end;
  705. sym:=objectlibrary.newasmsymboltype(_name,AB_GLOBAL,AT_NONE);
  706. end;
  707. constructor tai_symbol_end.ppuload(t:taitype;ppufile:tcompilerppufile);
  708. begin
  709. inherited ppuload(t,ppufile);
  710. sym:=ppufile.getasmsymbol;
  711. end;
  712. procedure tai_symbol_end.ppuwrite(ppufile:tcompilerppufile);
  713. begin
  714. inherited ppuwrite(ppufile);
  715. ppufile.putasmsymbol(sym);
  716. end;
  717. procedure tai_symbol_end.derefimpl;
  718. begin
  719. objectlibrary.DerefAsmsymbol(sym);
  720. end;
  721. {****************************************************************************
  722. TAI_CONST
  723. ****************************************************************************}
  724. constructor tai_const.Create_32bit(_value : longint);
  725. begin
  726. inherited Create;
  727. typ:=ait_const_32bit;
  728. value:=_value;
  729. end;
  730. constructor tai_const.Create_16bit(_value : word);
  731. begin
  732. inherited Create;
  733. typ:=ait_const_16bit;
  734. value:=_value;
  735. end;
  736. constructor tai_const.Create_8bit(_value : byte);
  737. begin
  738. inherited Create;
  739. typ:=ait_const_8bit;
  740. value:=_value;
  741. end;
  742. constructor tai_const.ppuload(t:taitype;ppufile:tcompilerppufile);
  743. begin
  744. inherited ppuload(t,ppufile);
  745. value:=ppufile.getlongint;
  746. end;
  747. procedure tai_const.ppuwrite(ppufile:tcompilerppufile);
  748. begin
  749. inherited ppuwrite(ppufile);
  750. ppufile.putlongint(value);
  751. end;
  752. {****************************************************************************
  753. TAI_CONST_SYMBOL
  754. ****************************************************************************}
  755. constructor tai_const_symbol.Create(_sym:tasmsymbol);
  756. begin
  757. inherited Create;
  758. typ:=ait_const_symbol;
  759. sym:=_sym;
  760. offset:=0;
  761. { update sym info }
  762. sym.increfs;
  763. end;
  764. constructor tai_const_symbol.Create_offset(_sym:tasmsymbol;ofs:longint);
  765. begin
  766. inherited Create;
  767. typ:=ait_const_symbol;
  768. sym:=_sym;
  769. offset:=ofs;
  770. { update sym info }
  771. sym.increfs;
  772. end;
  773. constructor tai_const_symbol.Create_rva(_sym:tasmsymbol);
  774. begin
  775. inherited Create;
  776. typ:=ait_const_rva;
  777. sym:=_sym;
  778. offset:=0;
  779. { update sym info }
  780. sym.increfs;
  781. end;
  782. constructor tai_const_symbol.Createname(const name:string);
  783. begin
  784. inherited Create;
  785. typ:=ait_const_symbol;
  786. sym:=objectlibrary.newasmsymbol(name);
  787. offset:=0;
  788. { update sym info }
  789. sym.increfs;
  790. end;
  791. constructor tai_const_symbol.Createname_offset(const name:string;ofs:longint);
  792. begin
  793. inherited Create;
  794. typ:=ait_const_symbol;
  795. sym:=objectlibrary.newasmsymbol(name);
  796. offset:=ofs;
  797. { update sym info }
  798. sym.increfs;
  799. end;
  800. constructor tai_const_symbol.Createname_rva(const name:string);
  801. begin
  802. inherited Create;
  803. typ:=ait_const_rva;
  804. sym:=objectlibrary.newasmsymbol(name);
  805. offset:=0;
  806. { update sym info }
  807. sym.increfs;
  808. end;
  809. constructor tai_const_symbol.Createdataname(const name:string);
  810. begin
  811. inherited Create;
  812. typ:=ait_const_symbol;
  813. sym:=objectlibrary.newasmsymboltype(name,AB_EXTERNAL,AT_DATA);
  814. offset:=0;
  815. { update sym info }
  816. sym.increfs;
  817. end;
  818. constructor tai_const_symbol.ppuload(t:taitype;ppufile:tcompilerppufile);
  819. begin
  820. inherited ppuload(t,ppufile);
  821. sym:=ppufile.getasmsymbol;
  822. offset:=ppufile.getlongint;
  823. end;
  824. procedure tai_const_symbol.ppuwrite(ppufile:tcompilerppufile);
  825. begin
  826. inherited ppuwrite(ppufile);
  827. ppufile.putasmsymbol(sym);
  828. ppufile.putlongint(offset);
  829. end;
  830. procedure tai_const_symbol.derefimpl;
  831. begin
  832. objectlibrary.DerefAsmsymbol(sym);
  833. end;
  834. function tai_const_symbol.getcopy:tlinkedlistitem;
  835. begin
  836. getcopy:=inherited getcopy;
  837. { we need to increase the reference number }
  838. sym.increfs;
  839. end;
  840. {****************************************************************************
  841. TAI_real_32bit
  842. ****************************************************************************}
  843. constructor tai_real_32bit.Create(_value : ts32real);
  844. begin
  845. inherited Create;
  846. typ:=ait_real_32bit;
  847. value:=_value;
  848. end;
  849. constructor tai_real_32bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  850. begin
  851. inherited ppuload(t,ppufile);
  852. value:=ppufile.getreal;
  853. end;
  854. procedure tai_real_32bit.ppuwrite(ppufile:tcompilerppufile);
  855. begin
  856. inherited ppuwrite(ppufile);
  857. ppufile.putreal(value);
  858. end;
  859. {****************************************************************************
  860. TAI_real_64bit
  861. ****************************************************************************}
  862. constructor tai_real_64bit.Create(_value : ts64real);
  863. begin
  864. inherited Create;
  865. typ:=ait_real_64bit;
  866. value:=_value;
  867. end;
  868. constructor tai_real_64bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  869. begin
  870. inherited ppuload(t,ppufile);
  871. value:=ppufile.getreal;
  872. end;
  873. procedure tai_real_64bit.ppuwrite(ppufile:tcompilerppufile);
  874. begin
  875. inherited ppuwrite(ppufile);
  876. ppufile.putreal(value);
  877. end;
  878. {****************************************************************************
  879. TAI_real_80bit
  880. ****************************************************************************}
  881. constructor tai_real_80bit.Create(_value : ts80real);
  882. begin
  883. inherited Create;
  884. typ:=ait_real_80bit;
  885. value:=_value;
  886. end;
  887. constructor tai_real_80bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  888. begin
  889. inherited ppuload(t,ppufile);
  890. value:=ppufile.getreal;
  891. end;
  892. procedure tai_real_80bit.ppuwrite(ppufile:tcompilerppufile);
  893. begin
  894. inherited ppuwrite(ppufile);
  895. ppufile.putreal(value);
  896. end;
  897. {****************************************************************************
  898. Tai_comp_64bit
  899. ****************************************************************************}
  900. constructor tai_comp_64bit.Create(_value : ts64comp);
  901. begin
  902. inherited Create;
  903. typ:=ait_comp_64bit;
  904. value:=_value;
  905. end;
  906. constructor tai_comp_64bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  907. begin
  908. inherited ppuload(t,ppufile);
  909. ppufile.putdata(value,sizeof(value));
  910. end;
  911. procedure tai_comp_64bit.ppuwrite(ppufile:tcompilerppufile);
  912. begin
  913. inherited ppuwrite(ppufile);
  914. ppufile.getdata(value,sizeof(value));
  915. end;
  916. {****************************************************************************
  917. TAI_STRING
  918. ****************************************************************************}
  919. constructor tai_string.Create(const _str : string);
  920. begin
  921. inherited Create;
  922. typ:=ait_string;
  923. len:=length(_str);
  924. getmem(str,len+1);
  925. strpcopy(str,_str);
  926. end;
  927. constructor tai_string.Create_pchar(_str : pchar);
  928. begin
  929. inherited Create;
  930. typ:=ait_string;
  931. str:=_str;
  932. len:=strlen(_str);
  933. end;
  934. constructor tai_string.Create_length_pchar(_str : pchar;length : longint);
  935. begin
  936. inherited Create;
  937. typ:=ait_string;
  938. str:=_str;
  939. len:=length;
  940. end;
  941. destructor tai_string.destroy;
  942. begin
  943. { you can have #0 inside the strings so }
  944. if str<>nil then
  945. freemem(str,len+1);
  946. inherited Destroy;
  947. end;
  948. constructor tai_string.ppuload(t:taitype;ppufile:tcompilerppufile);
  949. begin
  950. inherited ppuload(t,ppufile);
  951. len:=ppufile.getlongint;
  952. getmem(str,len+1);
  953. ppufile.getdata(str^,len);
  954. str[len]:=#0;
  955. end;
  956. procedure tai_string.ppuwrite(ppufile:tcompilerppufile);
  957. begin
  958. inherited ppuwrite(ppufile);
  959. ppufile.putlongint(len);
  960. ppufile.putdata(str^,len);
  961. end;
  962. function tai_string.getcopy : tlinkedlistitem;
  963. var
  964. p : tlinkedlistitem;
  965. begin
  966. p:=inherited getcopy;
  967. getmem(tai_string(p).str,len+1);
  968. move(str^,tai_string(p).str^,len+1);
  969. getcopy:=p;
  970. end;
  971. {****************************************************************************
  972. TAI_LABEL
  973. ****************************************************************************}
  974. constructor tai_label.create(_l : tasmlabel);
  975. begin
  976. inherited Create;
  977. typ:=ait_label;
  978. l:=_l;
  979. l.is_set:=true;
  980. is_global:=(l.defbind=AB_GLOBAL);
  981. end;
  982. constructor tai_label.ppuload(t:taitype;ppufile:tcompilerppufile);
  983. begin
  984. inherited ppuload(t,ppufile);
  985. l:=tasmlabel(ppufile.getasmsymbol);
  986. is_global:=boolean(ppufile.getbyte);
  987. end;
  988. procedure tai_label.ppuwrite(ppufile:tcompilerppufile);
  989. begin
  990. inherited ppuwrite(ppufile);
  991. ppufile.putasmsymbol(l);
  992. ppufile.putbyte(byte(is_global));
  993. end;
  994. procedure tai_label.derefimpl;
  995. begin
  996. objectlibrary.DerefAsmsymbol(tasmsymbol(l));
  997. l.is_set:=true;
  998. end;
  999. {****************************************************************************
  1000. TAI_DIRECT
  1001. ****************************************************************************}
  1002. constructor tai_direct.Create(_str : pchar);
  1003. begin
  1004. inherited Create;
  1005. typ:=ait_direct;
  1006. str:=_str;
  1007. end;
  1008. destructor tai_direct.destroy;
  1009. begin
  1010. strdispose(str);
  1011. inherited Destroy;
  1012. end;
  1013. constructor tai_direct.ppuload(t:taitype;ppufile:tcompilerppufile);
  1014. var
  1015. len : longint;
  1016. begin
  1017. inherited ppuload(t,ppufile);
  1018. len:=ppufile.getlongint;
  1019. getmem(str,len+1);
  1020. ppufile.getdata(str^,len);
  1021. str[len]:=#0;
  1022. end;
  1023. procedure tai_direct.ppuwrite(ppufile:tcompilerppufile);
  1024. var
  1025. len : longint;
  1026. begin
  1027. inherited ppuwrite(ppufile);
  1028. len:=strlen(str);
  1029. ppufile.putlongint(len);
  1030. ppufile.putdata(str^,len);
  1031. end;
  1032. function tai_direct.getcopy : tlinkedlistitem;
  1033. var
  1034. p : tlinkedlistitem;
  1035. begin
  1036. p:=inherited getcopy;
  1037. getmem(tai_direct(p).str,strlen(str)+1);
  1038. move(str^,tai_direct(p).str^,strlen(str)+1);
  1039. getcopy:=p;
  1040. end;
  1041. {****************************************************************************
  1042. tai_comment comment to be inserted in the assembler file
  1043. ****************************************************************************}
  1044. constructor tai_comment.Create(_str : pchar);
  1045. begin
  1046. inherited Create;
  1047. typ:=ait_comment;
  1048. str:=_str;
  1049. end;
  1050. destructor tai_comment.destroy;
  1051. begin
  1052. strdispose(str);
  1053. inherited Destroy;
  1054. end;
  1055. constructor tai_comment.ppuload(t:taitype;ppufile:tcompilerppufile);
  1056. var
  1057. len : longint;
  1058. begin
  1059. inherited ppuload(t,ppufile);
  1060. len:=ppufile.getlongint;
  1061. getmem(str,len+1);
  1062. ppufile.getdata(str^,len);
  1063. str[len]:=#0;
  1064. end;
  1065. procedure tai_comment.ppuwrite(ppufile:tcompilerppufile);
  1066. var
  1067. len : longint;
  1068. begin
  1069. inherited ppuwrite(ppufile);
  1070. len:=strlen(str);
  1071. ppufile.putlongint(len);
  1072. ppufile.putdata(str^,len);
  1073. end;
  1074. function tai_comment.getcopy : tlinkedlistitem;
  1075. var
  1076. p : tlinkedlistitem;
  1077. begin
  1078. p:=inherited getcopy;
  1079. getmem(tai_comment(p).str,strlen(str)+1);
  1080. move(str^,tai_comment(p).str^,strlen(str)+1);
  1081. getcopy:=p;
  1082. end;
  1083. {****************************************************************************
  1084. TAI_CUT
  1085. ****************************************************************************}
  1086. constructor tai_cut.Create;
  1087. begin
  1088. inherited Create;
  1089. typ:=ait_cut;
  1090. place:=cut_normal;
  1091. end;
  1092. constructor tai_cut.Create_begin;
  1093. begin
  1094. inherited Create;
  1095. typ:=ait_cut;
  1096. place:=cut_begin;
  1097. end;
  1098. constructor tai_cut.Create_end;
  1099. begin
  1100. inherited Create;
  1101. typ:=ait_cut;
  1102. place:=cut_end;
  1103. end;
  1104. constructor tai_cut.ppuload(t:taitype;ppufile:tcompilerppufile);
  1105. begin
  1106. inherited ppuload(t,ppufile);
  1107. place:=TCutPlace(ppufile.getbyte);
  1108. end;
  1109. procedure tai_cut.ppuwrite(ppufile:tcompilerppufile);
  1110. begin
  1111. inherited ppuwrite(ppufile);
  1112. ppufile.putbyte(byte(place));
  1113. end;
  1114. {****************************************************************************
  1115. Tai_Marker
  1116. ****************************************************************************}
  1117. constructor Tai_Marker.Create(_Kind: TMarker);
  1118. begin
  1119. Inherited Create;
  1120. typ := ait_marker;
  1121. Kind := _Kind;
  1122. end;
  1123. constructor Tai_Marker.ppuload(t:taitype;ppufile:tcompilerppufile);
  1124. begin
  1125. inherited ppuload(t,ppufile);
  1126. kind:=TMarker(ppufile.getbyte);
  1127. end;
  1128. procedure Tai_Marker.ppuwrite(ppufile:tcompilerppufile);
  1129. begin
  1130. inherited ppuwrite(ppufile);
  1131. ppufile.putbyte(byte(kind));
  1132. end;
  1133. {*****************************************************************************
  1134. tai_tempalloc
  1135. *****************************************************************************}
  1136. constructor tai_tempalloc.alloc(pos,size:longint);
  1137. begin
  1138. inherited Create;
  1139. typ:=ait_tempalloc;
  1140. allocation:=true;
  1141. temppos:=pos;
  1142. tempsize:=size;
  1143. {$ifdef EXTDEBUG}
  1144. problem:=nil;
  1145. {$endif EXTDEBUG}
  1146. end;
  1147. destructor tai_tempalloc.destroy;
  1148. begin
  1149. {$ifdef EXTDEBUG}
  1150. stringdispose(problem);
  1151. {$endif EXTDEBUG}
  1152. inherited destroy;
  1153. end;
  1154. constructor tai_tempalloc.dealloc(pos,size:longint);
  1155. begin
  1156. inherited Create;
  1157. typ:=ait_tempalloc;
  1158. allocation:=false;
  1159. temppos:=pos;
  1160. tempsize:=size;
  1161. {$ifdef EXTDEBUG}
  1162. problem:=nil;
  1163. {$endif EXTDEBUG}
  1164. end;
  1165. {$ifdef EXTDEBUG}
  1166. constructor tai_tempalloc.allocinfo(pos,size:longint;const st:string);
  1167. begin
  1168. inherited Create;
  1169. typ:=ait_tempalloc;
  1170. allocation:=false;
  1171. temppos:=pos;
  1172. tempsize:=size;
  1173. problem:=stringdup(st);
  1174. end;
  1175. {$endif EXTDEBUG}
  1176. constructor tai_tempalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  1177. begin
  1178. inherited ppuload(t,ppufile);
  1179. temppos:=ppufile.getlongint;
  1180. tempsize:=ppufile.getlongint;
  1181. allocation:=boolean(ppufile.getbyte);
  1182. {$ifdef EXTDEBUG}
  1183. problem:=nil;
  1184. {$endif EXTDEBUG}
  1185. end;
  1186. procedure tai_tempalloc.ppuwrite(ppufile:tcompilerppufile);
  1187. begin
  1188. inherited ppuwrite(ppufile);
  1189. ppufile.putlongint(temppos);
  1190. ppufile.putlongint(tempsize);
  1191. ppufile.putbyte(byte(allocation));
  1192. end;
  1193. {*****************************************************************************
  1194. tai_regalloc
  1195. *****************************************************************************}
  1196. constructor tai_regalloc.alloc(r : tregister);
  1197. begin
  1198. inherited create;
  1199. typ:=ait_regalloc;
  1200. allocation:=true;
  1201. reg:=r;
  1202. end;
  1203. constructor tai_regalloc.dealloc(r : tregister);
  1204. begin
  1205. inherited create;
  1206. typ:=ait_regalloc;
  1207. allocation:=false;
  1208. reg:=r;
  1209. end;
  1210. constructor tai_regalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  1211. begin
  1212. inherited ppuload(t,ppufile);
  1213. ppufile.getdata(reg,sizeof(Tregister));
  1214. allocation:=boolean(ppufile.getbyte);
  1215. end;
  1216. procedure tai_regalloc.ppuwrite(ppufile:tcompilerppufile);
  1217. begin
  1218. inherited ppuwrite(ppufile);
  1219. ppufile.putdata(reg,sizeof(Tregister));
  1220. ppufile.putbyte(byte(allocation));
  1221. end;
  1222. {*****************************************************************************
  1223. TaiInstruction
  1224. *****************************************************************************}
  1225. constructor taicpu_abstract.Create(op : tasmop);
  1226. begin
  1227. inherited create;
  1228. typ:=ait_instruction;
  1229. is_jmp:=false;
  1230. opcode:=op;
  1231. ops:=0;
  1232. fillchar(condition,sizeof(condition),0);
  1233. fillchar(oper,sizeof(oper),0);
  1234. end;
  1235. destructor taicpu_abstract.Destroy;
  1236. var
  1237. i : longint;
  1238. begin
  1239. for i:=0 to ops-1 do
  1240. case oper[i].typ of
  1241. top_ref:
  1242. dispose(oper[i].ref);
  1243. {$ifdef ARM}
  1244. top_shifterop:
  1245. dispose(oper[i].shifterop);
  1246. {$endif ARM}
  1247. end;
  1248. inherited destroy;
  1249. end;
  1250. { ---------------------------------------------------------------------
  1251. Loading of operands.
  1252. ---------------------------------------------------------------------}
  1253. procedure taicpu_abstract.loadconst(opidx:longint;l:aword);
  1254. begin
  1255. if opidx>=ops then
  1256. ops:=opidx+1;
  1257. with oper[opidx] do
  1258. begin
  1259. if typ<>top_const then
  1260. clearop(opidx);
  1261. val:=l;
  1262. typ:=top_const;
  1263. end;
  1264. end;
  1265. procedure taicpu_abstract.loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  1266. begin
  1267. if not assigned(s) then
  1268. internalerror(200204251);
  1269. if opidx>=ops then
  1270. ops:=opidx+1;
  1271. with oper[opidx] do
  1272. begin
  1273. if typ<>top_symbol then
  1274. clearop(opidx);
  1275. sym:=s;
  1276. symofs:=sofs;
  1277. typ:=top_symbol;
  1278. end;
  1279. s.increfs;
  1280. end;
  1281. procedure taicpu_abstract.loadlocal(opidx:longint;s:pointer;sofs:longint);
  1282. begin
  1283. if not assigned(s) then
  1284. internalerror(200204251);
  1285. if opidx>=ops then
  1286. ops:=opidx+1;
  1287. with oper[opidx] do
  1288. begin
  1289. if typ<>top_local then
  1290. clearop(opidx);
  1291. localsym:=s;
  1292. localsymofs:=sofs;
  1293. typ:=top_local;
  1294. end;
  1295. end;
  1296. procedure taicpu_abstract.loadref(opidx:longint;const r:treference);
  1297. begin
  1298. if opidx>=ops then
  1299. ops:=opidx+1;
  1300. with oper[opidx] do
  1301. begin
  1302. if typ<>top_ref then
  1303. begin
  1304. clearop(opidx);
  1305. new(ref);
  1306. end;
  1307. ref^:=r;
  1308. {$ifdef i386}
  1309. { We allow this exception for i386, since overloading this would be
  1310. too much of a a speed penalty}
  1311. if (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  1312. segprefix:=ref^.segment;
  1313. {$endif}
  1314. typ:=top_ref;
  1315. { mark symbol as used }
  1316. if assigned(ref^.symbol) then
  1317. ref^.symbol.increfs;
  1318. end;
  1319. end;
  1320. procedure taicpu_abstract.loadreg(opidx:longint;r:tregister);
  1321. begin
  1322. if opidx>=ops then
  1323. ops:=opidx+1;
  1324. with oper[opidx] do
  1325. begin
  1326. if typ<>top_reg then
  1327. clearop(opidx);
  1328. reg:=r;
  1329. typ:=top_reg;
  1330. end;
  1331. end;
  1332. procedure taicpu_abstract.loadoper(opidx:longint;o:toper);
  1333. begin
  1334. if opidx>=ops then
  1335. ops:=opidx+1;
  1336. clearop(opidx);
  1337. oper[opidx]:=o;
  1338. { copy also the reference }
  1339. case oper[opidx].typ of
  1340. top_ref:
  1341. begin
  1342. new(oper[opidx].ref);
  1343. oper[opidx].ref^:=o.ref^;
  1344. end;
  1345. {$ifdef ARM}
  1346. top_shifterop:
  1347. begin
  1348. new(oper[opidx].shifterop);
  1349. oper[opidx].shifterop^:=o.shifterop^;
  1350. end;
  1351. {$endif ARM}
  1352. end;
  1353. end;
  1354. procedure taicpu_abstract.clearop(opidx:longint);
  1355. begin
  1356. with oper[opidx] do
  1357. case typ of
  1358. top_ref:
  1359. dispose(ref);
  1360. {$ifdef ARM}
  1361. top_shifterop:
  1362. dispose(shifterop);
  1363. {$endif ARM}
  1364. end;
  1365. end;
  1366. { ---------------------------------------------------------------------
  1367. Register allocator methods.
  1368. ---------------------------------------------------------------------}
  1369. function taicpu_abstract.get_insert_pos(p:Tai;huntfor1,huntfor2,huntfor3:Tsuperregister;var unusedregsint:Tsuperregisterset):Tai;
  1370. var
  1371. back : Tsuperregisterset;
  1372. supreg : tsuperregister;
  1373. begin
  1374. back:=unusedregsint;
  1375. get_insert_pos:=p;
  1376. while (p<>nil) and (p.typ=ait_regalloc) do
  1377. begin
  1378. supreg:=getsupreg(Tai_regalloc(p).reg);
  1379. {Rewind the register allocation.}
  1380. if Tai_regalloc(p).allocation then
  1381. include(unusedregsint,supreg)
  1382. else
  1383. begin
  1384. exclude(unusedregsint,supreg);
  1385. if supreg=huntfor1 then
  1386. begin
  1387. get_insert_pos:=Tai(p.previous);
  1388. back:=unusedregsint;
  1389. end;
  1390. if supreg=huntfor2 then
  1391. begin
  1392. get_insert_pos:=Tai(p.previous);
  1393. back:=unusedregsint;
  1394. end;
  1395. if supreg=huntfor3 then
  1396. begin
  1397. get_insert_pos:=Tai(p.previous);
  1398. back:=unusedregsint;
  1399. end;
  1400. end;
  1401. p:=Tai(p.previous);
  1402. end;
  1403. unusedregsint:=back;
  1404. end;
  1405. procedure taicpu_abstract.forward_allocation(p:Tai;var unusedregsint:Tsuperregisterset);
  1406. begin
  1407. {Forward the register allocation again.}
  1408. while (p<>self) do
  1409. begin
  1410. if p.typ<>ait_regalloc then
  1411. internalerror(200305311);
  1412. if Tai_regalloc(p).allocation then
  1413. exclude(unusedregsint,getsupreg(Tai_regalloc(p).reg))
  1414. else
  1415. include(unusedregsint,getsupreg(Tai_regalloc(p).reg));
  1416. p:=Tai(p.next);
  1417. end;
  1418. end;
  1419. function taicpu_abstract.spill_registers(list:Taasmoutput;
  1420. rgget:Trggetproc;
  1421. rgunget:Trgungetproc;
  1422. r:Tsuperregisterset;
  1423. var unusedregsint:Tsuperregisterset;
  1424. const spilltemplist:Tspill_temp_list): boolean;
  1425. var
  1426. i:byte;
  1427. supreg, reg1, reg2, reg3: Tsuperregister;
  1428. helpreg:Tregister;
  1429. helpins:Tai;
  1430. op:Tasmop;
  1431. pos:Tai;
  1432. wasload: boolean;
  1433. begin
  1434. spill_registers:=false;
  1435. if (ops = 2) and
  1436. (oper[1].typ=top_ref) and
  1437. { oper[1] can also be ref in case of "lis r3,symbol@ha" or so }
  1438. spilling_decode_loadstore(opcode,op,wasload) then
  1439. begin
  1440. { the register that's being stored/loaded }
  1441. supreg:=getsupreg(oper[0].reg);
  1442. if supreg in r then
  1443. begin
  1444. // Example:
  1445. // l?? r20d, 8(r1) ; r20d must be spilled into -60(r1)
  1446. //
  1447. // Change into:
  1448. //
  1449. // l?? r21d, 8(r1)
  1450. // st? r21d, -60(r1)
  1451. //
  1452. // And:
  1453. //
  1454. // st? r20d, 8(r1) ; r20d must be spilled into -60(r1)
  1455. //
  1456. // Change into:
  1457. //
  1458. // l?? r21d, -60(r1)
  1459. // st? r21d, 8(r1)
  1460. pos := get_insert_pos(Tai(previous),supreg,
  1461. getsupreg(oper[1].ref^.base),
  1462. getsupreg(oper[1].ref^.index),
  1463. unusedregsint);
  1464. rgget(list,pos,R_SUBWHOLE,helpreg);
  1465. spill_registers := true;
  1466. if wasload then
  1467. begin
  1468. helpins:=spilling_create_loadstore(opcode,helpreg,oper[1].ref^);
  1469. loadref(1,spilltemplist[supreg]);
  1470. opcode := op;
  1471. end
  1472. else
  1473. helpins:=spilling_create_loadstore(op,helpreg,spilltemplist[supreg]);
  1474. if pos=nil then
  1475. list.insertafter(helpins,list.first)
  1476. else
  1477. list.insertafter(helpins,pos.next);
  1478. loadreg(0,helpreg);
  1479. rgunget(list,helpins,helpreg);
  1480. forward_allocation(tai(helpins.next),unusedregsint);
  1481. {
  1482. writeln('spilling!');
  1483. list.insertafter(tai_comment.Create(strpnew('Spilling!')),helpins);
  1484. }
  1485. end;
  1486. { now the registers used in the reference }
  1487. { a) base }
  1488. supreg := getsupreg(oper[1].ref^.base);
  1489. if supreg in r then
  1490. begin
  1491. if wasload then
  1492. pos:=get_insert_pos(Tai(previous),getsupreg(oper[1].ref^.index),getsupreg(oper[0].reg),0,unusedregsint)
  1493. else
  1494. pos:=get_insert_pos(Tai(previous),getsupreg(oper[1].ref^.index),0,0,unusedregsint);
  1495. rgget(list,pos,R_SUBWHOLE,helpreg);
  1496. spill_registers:=true;
  1497. helpins:=spilling_create_load(spilltemplist[supreg],helpreg);
  1498. if pos=nil then
  1499. list.insertafter(helpins,list.first)
  1500. else
  1501. list.insertafter(helpins,pos.next);
  1502. oper[1].ref^.base:=helpreg;
  1503. rgunget(list,helpins,helpreg);
  1504. forward_allocation(Tai(helpins.next),unusedregsint);
  1505. {
  1506. writeln('spilling!');
  1507. list.insertafter(tai_comment.Create(strpnew('Spilling!')),helpins);
  1508. }
  1509. end;
  1510. { b) index }
  1511. supreg := getsupreg(oper[1].ref^.index);
  1512. if supreg in r then
  1513. begin
  1514. if wasload then
  1515. pos:=get_insert_pos(Tai(previous),getsupreg(oper[1].ref^.base),getsupreg(oper[0].reg),0,unusedregsint)
  1516. else
  1517. pos:=get_insert_pos(Tai(previous),getsupreg(oper[1].ref^.base),0,0,unusedregsint);
  1518. rgget(list,pos,R_SUBWHOLE,helpreg);
  1519. spill_registers:=true;
  1520. helpins:=spilling_create_load(spilltemplist[supreg],helpreg);
  1521. if pos=nil then
  1522. list.insertafter(helpins,list.first)
  1523. else
  1524. list.insertafter(helpins,pos.next);
  1525. oper[1].ref^.index:=helpreg;
  1526. rgunget(list,helpins,helpreg);
  1527. forward_allocation(Tai(helpins.next),unusedregsint);
  1528. {
  1529. writeln('spilling!');
  1530. list.insertafter(tai_comment.Create(strpnew('Spilling!')),helpins);
  1531. }
  1532. end;
  1533. { load/store is done }
  1534. exit;
  1535. end;
  1536. { all other instructions the compiler generates are the same (I hope): }
  1537. { operand 0 is a register and is the destination, the others are sources }
  1538. { and can be either registers or constants }
  1539. { exception: branches (is_jmp isn't always set for them) }
  1540. if oper[0].typ <> top_reg then
  1541. exit;
  1542. reg1 := getsupreg(oper[0].reg);
  1543. if oper[1].typ = top_reg then
  1544. reg2 := getsupreg(oper[1].reg)
  1545. else
  1546. reg2 := 0;
  1547. if (ops >= 3) and
  1548. (oper[2].typ = top_reg) then
  1549. reg3 := getsupreg(oper[2].reg)
  1550. else
  1551. reg3 := 0;
  1552. supreg:=reg1;
  1553. if supreg in r then
  1554. begin
  1555. // Example:
  1556. // add r20d, r21d, r22d ; r20d must be spilled into -60(r1)
  1557. //
  1558. // Change into:
  1559. //
  1560. // lwz r23d, -60(r1)
  1561. // add r23d, r21d, r22d
  1562. // stw r23d, -60(r1)
  1563. pos := get_insert_pos(Tai(previous),reg1,reg2,reg3,unusedregsint);
  1564. rgget(list,pos,R_SUBWHOLE,helpreg);
  1565. spill_registers := true;
  1566. helpins:=spilling_create_store(helpreg,spilltemplist[supreg]);
  1567. list.insertafter(helpins,self);
  1568. helpins:=spilling_create_load(spilltemplist[supreg],helpreg);
  1569. if pos=nil then
  1570. list.insertafter(helpins,list.first)
  1571. else
  1572. list.insertafter(helpins,pos.next);
  1573. loadreg(0,helpreg);
  1574. rgunget(list,helpins,helpreg);
  1575. forward_allocation(tai(helpins.next),unusedregsint);
  1576. {
  1577. writeln('spilling!');
  1578. list.insertafter(tai_comment.Create(strpnew('Spilling!')),helpins);
  1579. }
  1580. end;
  1581. for i := 1 to 2 do
  1582. if (oper[i].typ = top_reg) then
  1583. begin
  1584. supreg:=getsupreg(oper[i].reg);
  1585. if supreg in r then
  1586. begin
  1587. // Example:
  1588. // add r20d, r21d, r22d ; r20d must be spilled into -60(r1)
  1589. //
  1590. // Change into:
  1591. //
  1592. // lwz r23d, -60(r1)
  1593. // add r23d, r21d, r22d
  1594. // stw r23d, -60(r1)
  1595. pos := get_insert_pos(Tai(previous),reg1,reg2,reg3,unusedregsint);
  1596. rgget(list,pos,R_SUBWHOLE,helpreg);
  1597. spill_registers := true;
  1598. helpins:=spilling_create_load(spilltemplist[supreg],helpreg);
  1599. if pos=nil then
  1600. list.insertafter(helpins,list.first)
  1601. else
  1602. list.insertafter(helpins,pos.next);
  1603. loadreg(i,helpreg);
  1604. rgunget(list,helpins,helpreg);
  1605. forward_allocation(tai(helpins.next),unusedregsint);
  1606. {
  1607. writeln('spilling!');
  1608. list.insertafter(tai_comment.Create(strpnew('Spilling!')),helpins);
  1609. }
  1610. end;
  1611. end;
  1612. end;
  1613. { ---------------------------------------------------------------------
  1614. Miscellaneous methods.
  1615. ---------------------------------------------------------------------}
  1616. procedure taicpu_abstract.SetCondition(const c:TAsmCond);
  1617. begin
  1618. condition:=c;
  1619. end;
  1620. Function taicpu_abstract.getcopy:TLinkedListItem;
  1621. var
  1622. i : longint;
  1623. p : TLinkedListItem;
  1624. begin
  1625. p:=inherited getcopy;
  1626. { make a copy of the references }
  1627. for i:=1 to ops do
  1628. if (taicpu_abstract(p).oper[i-1].typ=top_ref) then
  1629. begin
  1630. new(taicpu_abstract(p).oper[i-1].ref);
  1631. taicpu_abstract(p).oper[i-1].ref^:=oper[i-1].ref^;
  1632. end;
  1633. getcopy:=p;
  1634. end;
  1635. constructor taicpu_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  1636. var
  1637. i : integer;
  1638. begin
  1639. inherited ppuload(t,ppufile);
  1640. { hopefully, we don't get problems with big/litte endian here when cross compiling :/ }
  1641. ppufile.getdata(condition,sizeof(tasmcond));
  1642. ops:=ppufile.getbyte;
  1643. for i:=1 to ops do
  1644. ppuloadoper(ppufile,oper[i-1]);
  1645. opcode:=tasmop(ppufile.getword);
  1646. {$ifdef i386}
  1647. ppufile.getdata(segprefix,sizeof(Tregister));
  1648. {$endif i386}
  1649. is_jmp:=boolean(ppufile.getbyte);
  1650. end;
  1651. procedure taicpu_abstract.ppuwrite(ppufile:tcompilerppufile);
  1652. var
  1653. i : integer;
  1654. begin
  1655. inherited ppuwrite(ppufile);
  1656. ppufile.putdata(condition,sizeof(tasmcond));
  1657. ppufile.putbyte(ops);
  1658. for i:=1 to ops do
  1659. ppuwriteoper(ppufile,oper[i-1]);
  1660. ppufile.putword(word(opcode));
  1661. {$ifdef i386}
  1662. ppufile.putdata(segprefix,sizeof(Tregister));
  1663. {$endif i386}
  1664. ppufile.putbyte(byte(is_jmp));
  1665. end;
  1666. procedure taicpu_abstract.derefimpl;
  1667. var i:byte;
  1668. begin
  1669. for i:=1 to ops do
  1670. ppuderefoper(oper[i-1]);
  1671. end;
  1672. {****************************************************************************
  1673. tai_align_abstract
  1674. ****************************************************************************}
  1675. constructor tai_align_abstract.Create(b: byte);
  1676. begin
  1677. inherited Create;
  1678. typ:=ait_align;
  1679. if b in [1,2,4,8,16,32] then
  1680. aligntype := b
  1681. else
  1682. aligntype := 1;
  1683. fillsize:=0;
  1684. fillop:=0;
  1685. use_op:=false;
  1686. end;
  1687. constructor tai_align_abstract.Create_op(b: byte; _op: byte);
  1688. begin
  1689. inherited Create;
  1690. typ:=ait_align;
  1691. if b in [1,2,4,8,16,32] then
  1692. aligntype := b
  1693. else
  1694. aligntype := 1;
  1695. fillsize:=0;
  1696. fillop:=_op;
  1697. use_op:=true;
  1698. end;
  1699. function tai_align_abstract.calculatefillbuf(var buf : tfillbuffer):pchar;
  1700. begin
  1701. fillchar(buf,high(buf),fillop);
  1702. calculatefillbuf:=pchar(@buf);
  1703. end;
  1704. constructor tai_align_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  1705. begin
  1706. inherited ppuload(t,ppufile);
  1707. aligntype:=ppufile.getbyte;
  1708. fillsize:=0;
  1709. fillop:=ppufile.getbyte;
  1710. use_op:=boolean(ppufile.getbyte);
  1711. end;
  1712. procedure tai_align_abstract.ppuwrite(ppufile:tcompilerppufile);
  1713. begin
  1714. inherited ppuwrite(ppufile);
  1715. ppufile.putbyte(aligntype);
  1716. ppufile.putbyte(fillop);
  1717. ppufile.putbyte(byte(use_op));
  1718. end;
  1719. {*****************************************************************************
  1720. TAAsmOutput
  1721. *****************************************************************************}
  1722. constructor taasmoutput.create;
  1723. begin
  1724. inherited create;
  1725. { make sure the optimizer won't remove the first tai of this list}
  1726. insert(tai_marker.create(marker_blockstart));
  1727. end;
  1728. function taasmoutput.getlasttaifilepos : pfileposinfo;
  1729. var
  1730. hp : tlinkedlistitem;
  1731. begin
  1732. getlasttaifilepos := nil;
  1733. if assigned(last) then
  1734. begin
  1735. { find the last file information record }
  1736. if not (tai(last).typ in SkipLineInfo) then
  1737. getlasttaifilepos:=@tailineinfo(last).fileinfo
  1738. else
  1739. { go through list backwards to find the first entry
  1740. with line information
  1741. }
  1742. begin
  1743. hp:=tai(last);
  1744. while assigned(hp) and (tai(hp).typ in SkipLineInfo) do
  1745. hp:=hp.Previous;
  1746. { found entry }
  1747. if assigned(hp) then
  1748. getlasttaifilepos:=@tailineinfo(hp).fileinfo
  1749. end;
  1750. end;
  1751. end;
  1752. procedure Taasmoutput.InsertAfter(Item,Loc : TLinkedListItem);
  1753. begin
  1754. if assigned(Loc) then
  1755. tailineinfo(Item).fileinfo:=tailineinfo(Loc).fileinfo;
  1756. inherited InsertAfter(Item,Loc);
  1757. end;
  1758. procedure Taasmoutput.translate_registers(regtype:tregistertype;const table:Ttranstable);
  1759. var p,q:Tai;
  1760. i:shortint;
  1761. r:Preference;
  1762. {$ifdef arm}
  1763. so:pshifterop;
  1764. {$endif arm}
  1765. begin
  1766. p:=Tai(first);
  1767. while assigned(p) do
  1768. begin
  1769. case p.typ of
  1770. ait_regalloc:
  1771. if (getregtype(Tai_regalloc(p).reg)=regtype) then
  1772. setsupreg(Tai_regalloc(p).reg,table[getsupreg(Tai_regalloc(p).reg)]);
  1773. ait_instruction:
  1774. begin
  1775. for i:=0 to Taicpu_abstract(p).ops-1 do
  1776. case Taicpu_abstract(p).oper[i].typ of
  1777. Top_reg:
  1778. if (getregtype(Taicpu_abstract(p).oper[i].reg)=regtype) then
  1779. setsupreg(Taicpu_abstract(p).oper[i].reg,table[getsupreg(Taicpu_abstract(p).oper[i].reg)]);
  1780. Top_ref:
  1781. begin
  1782. if regtype=R_INTREGISTER then
  1783. begin
  1784. r:=Taicpu_abstract(p).oper[i].ref;
  1785. if r^.base<>NR_NO then
  1786. setsupreg(r^.base,table[getsupreg(r^.base)]);
  1787. if r^.index<>NR_NO then
  1788. setsupreg(r^.index,table[getsupreg(r^.index)]);
  1789. end;
  1790. end;
  1791. {$ifdef arm}
  1792. Top_shifterop:
  1793. begin
  1794. so:=Taicpu_abstract(p).oper[i].shifterop;
  1795. if so^.rs<>NR_NO then
  1796. setsupreg(so^.rs,table[getsupreg(so^.rs)]);
  1797. end;
  1798. {$endif arm}
  1799. end;
  1800. { Maybe the operation can be removed when
  1801. it is a move and both arguments are the same }
  1802. if Taicpu_abstract(p).is_nop then
  1803. begin
  1804. q:=p;
  1805. p:=Tai(p.next);
  1806. remove(q);
  1807. continue;
  1808. end;
  1809. end;
  1810. end;
  1811. p:=Tai(p.next);
  1812. end;
  1813. end;
  1814. end.
  1815. {
  1816. $Log$
  1817. Revision 1.43 2003-10-11 16:06:42 florian
  1818. * fixed some MMX<->SSE
  1819. * started to fix ppc, needs an overhaul
  1820. + stabs info improve for spilling, not sure if it works correctly/completly
  1821. - MMX_SUPPORT removed from Makefile.fpc
  1822. Revision 1.42 2003/10/10 17:48:13 peter
  1823. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  1824. * tregisteralloctor renamed to trgobj
  1825. * removed rgobj from a lot of units
  1826. * moved location_* and reference_* to cgobj
  1827. * first things for mmx register allocation
  1828. Revision 1.41 2003/10/01 20:34:48 peter
  1829. * procinfo unit contains tprocinfo
  1830. * cginfo renamed to cgbase
  1831. * moved cgmessage to verbose
  1832. * fixed ppc and sparc compiles
  1833. Revision 1.40 2003/09/23 17:56:05 peter
  1834. * locals and paras are allocated in the code generation
  1835. * tvarsym.localloc contains the location of para/local when
  1836. generating code for the current procedure
  1837. Revision 1.39 2003/09/07 22:09:34 peter
  1838. * preparations for different default calling conventions
  1839. * various RA fixes
  1840. Revision 1.38 2003/09/04 00:15:28 florian
  1841. * first bunch of adaptions of arm compiler for new register type
  1842. Revision 1.37 2003/09/03 15:55:00 peter
  1843. * NEWRA branch merged
  1844. Revision 1.36 2003/09/03 11:18:36 florian
  1845. * fixed arm concatcopy
  1846. + arm support in the common compiler sources added
  1847. * moved some generic cg code around
  1848. + tfputype added
  1849. * ...
  1850. Revision 1.35.2.5 2003/08/31 21:08:16 peter
  1851. * first batch of sparc fixes
  1852. Revision 1.35.2.4 2003/08/29 17:28:59 peter
  1853. * next batch of updates
  1854. Revision 1.35.2.3 2003/08/28 18:35:07 peter
  1855. * tregister changed to cardinal
  1856. Revision 1.35.2.2 2003/08/27 20:23:55 peter
  1857. * remove old ra code
  1858. Revision 1.35.2.1 2003/08/27 19:55:54 peter
  1859. * first tregister patch
  1860. Revision 1.35 2003/08/21 14:47:41 peter
  1861. * remove convert_registers
  1862. Revision 1.34 2003/08/20 20:29:06 daniel
  1863. * Some more R_NO changes
  1864. * Preventive code to loadref added
  1865. Revision 1.33 2003/08/17 20:47:47 daniel
  1866. * Notranslation changed into -sr functionality
  1867. Revision 1.32 2003/08/17 16:59:20 jonas
  1868. * fixed regvars so they work with newra (at least for ppc)
  1869. * fixed some volatile register bugs
  1870. + -dnotranslation option for -dnewra, which causes the registers not to
  1871. be translated from virtual to normal registers. Requires support in
  1872. the assembler writer as well, which is only implemented in aggas/
  1873. agppcgas currently
  1874. Revision 1.31 2003/08/11 21:18:20 peter
  1875. * start of sparc support for newra
  1876. Revision 1.30 2003/07/02 16:43:48 jonas
  1877. * always add dummy marker object at the start of an assembler list, so
  1878. the optimizer can't remove the first object
  1879. Revision 1.29 2003/06/03 13:01:59 daniel
  1880. * Register allocator finished
  1881. Revision 1.28 2003/05/12 18:13:57 peter
  1882. * create rtti label using newasmsymboldata and update binding
  1883. only when calling tai_symbol.create
  1884. * tai_symbol.create_global added
  1885. Revision 1.27 2003/04/25 20:59:33 peter
  1886. * removed funcretn,funcretsym, function result is now in varsym
  1887. and aliases for result and function name are added using absolutesym
  1888. * vs_hidden parameter for funcret passed in parameter
  1889. * vs_hidden fixes
  1890. * writenode changed to printnode and released from extdebug
  1891. * -vp option added to generate a tree.log with the nodetree
  1892. * nicer printnode for statements, callnode
  1893. Revision 1.26 2002/04/25 16:12:09 florian
  1894. * fixed more problems with cpubase and x86-64
  1895. Revision 1.25 2003/04/25 08:25:26 daniel
  1896. * Ifdefs around a lot of calls to cleartempgen
  1897. * Fixed registers that are allocated but not freed in several nodes
  1898. * Tweak to register allocator to cause less spills
  1899. * 8-bit registers now interfere with esi,edi and ebp
  1900. Compiler can now compile rtl successfully when using new register
  1901. allocator
  1902. Revision 1.24 2003/04/24 13:03:01 florian
  1903. * comp is now written with its bit pattern to the ppu instead as an extended
  1904. Revision 1.23 2003/04/22 14:33:38 peter
  1905. * removed some notes/hints
  1906. Revision 1.22 2003/04/22 10:09:34 daniel
  1907. + Implemented the actual register allocator
  1908. + Scratch registers unavailable when new register allocator used
  1909. + maybe_save/maybe_restore unavailable when new register allocator used
  1910. Revision 1.21 2003/02/19 22:00:14 daniel
  1911. * Code generator converted to new register notation
  1912. - Horribily outdated todo.txt removed
  1913. Revision 1.20 2003/01/30 21:46:20 peter
  1914. * tai_const_symbol.createdataname added
  1915. Revision 1.19 2003/01/21 08:48:08 daniel
  1916. * Another 200301081 fixed
  1917. Revision 1.18 2003/01/09 20:40:59 daniel
  1918. * Converted some code in cgx86.pas to new register numbering
  1919. Revision 1.17 2003/01/09 15:49:56 daniel
  1920. * Added register conversion
  1921. Revision 1.16 2003/01/08 18:43:56 daniel
  1922. * Tregister changed into a record
  1923. Revision 1.15 2003/01/05 13:36:53 florian
  1924. * x86-64 compiles
  1925. + very basic support for float128 type (x86-64 only)
  1926. Revision 1.14 2002/12/06 17:50:21 peter
  1927. * symbol count fix merged
  1928. Revision 1.13 2002/11/17 16:31:55 carl
  1929. * memory optimization (3-4%) : cleanup of tai fields,
  1930. cleanup of tdef and tsym fields.
  1931. * make it work for m68k
  1932. Revision 1.12 2002/11/15 16:29:30 peter
  1933. * made tasmsymbol.refs private (merged)
  1934. Revision 1.11 2002/11/15 01:58:45 peter
  1935. * merged changes from 1.0.7 up to 04-11
  1936. - -V option for generating bug report tracing
  1937. - more tracing for option parsing
  1938. - errors for cdecl and high()
  1939. - win32 import stabs
  1940. - win32 records<=8 are returned in eax:edx (turned off by default)
  1941. - heaptrc update
  1942. - more info for temp management in .s file with EXTDEBUG
  1943. Revision 1.10 2002/11/09 15:38:03 carl
  1944. + NOOPT removed the optinfo field
  1945. Revision 1.9 2002/10/05 12:43:23 carl
  1946. * fixes for Delphi 6 compilation
  1947. (warning : Some features do not work under Delphi)
  1948. Revision 1.8 2002/08/19 19:36:42 peter
  1949. * More fixes for cross unit inlining, all tnodes are now implemented
  1950. * Moved pocall_internconst to po_internconst because it is not a
  1951. calling type at all and it conflicted when inlining of these small
  1952. functions was requested
  1953. Revision 1.7 2002/08/18 20:06:23 peter
  1954. * inlining is now also allowed in interface
  1955. * renamed write/load to ppuwrite/ppuload
  1956. * tnode storing in ppu
  1957. * nld,ncon,nbas are already updated for storing in ppu
  1958. Revision 1.6 2002/08/16 05:21:09 florian
  1959. * powerpc compilation fix
  1960. Revision 1.5 2002/08/15 19:10:35 peter
  1961. * first things tai,tnode storing in ppu
  1962. Revision 1.4 2002/08/11 14:32:25 peter
  1963. * renamed current_library to objectlibrary
  1964. Revision 1.3 2002/08/11 13:24:10 peter
  1965. * saving of asmsymbols in ppu supported
  1966. * asmsymbollist global is removed and moved into a new class
  1967. tasmlibrarydata that will hold the info of a .a file which
  1968. corresponds with a single module. Added librarydata to tmodule
  1969. to keep the library info stored for the module. In the future the
  1970. objectfiles will also be stored to the tasmlibrarydata class
  1971. * all getlabel/newasmsymbol and friends are moved to the new class
  1972. Revision 1.2 2002/08/05 18:27:48 carl
  1973. + more more more documentation
  1974. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  1975. Revision 1.1 2002/07/01 18:46:20 peter
  1976. * internal linker
  1977. * reorganized aasm layer
  1978. Revision 1.27 2002/05/18 13:34:04 peter
  1979. * readded missing revisions
  1980. Revision 1.25 2002/05/14 19:34:38 peter
  1981. * removed old logs and updated copyright year
  1982. Revision 1.24 2002/05/14 17:28:08 peter
  1983. * synchronized cpubase between powerpc and i386
  1984. * moved more tables from cpubase to cpuasm
  1985. * tai_align_abstract moved to tainst, cpuasm must define
  1986. the tai_align class now, which may be empty
  1987. Revision 1.23 2002/04/15 18:54:34 carl
  1988. - removed tcpuflags
  1989. Revision 1.22 2002/04/07 13:18:19 carl
  1990. + more documentation
  1991. Revision 1.21 2002/04/07 10:17:40 carl
  1992. - remove packenumfixed (requires version 1.0.2 or later to compile now!)
  1993. + changing some comments so its commented automatically
  1994. Revision 1.20 2002/03/24 19:04:31 carl
  1995. + patch for SPARC from Mazen NEIFER
  1996. }