aasmtai.pas 71 KB

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