aasmtai.pas 75 KB

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