aasmtai.pas 76 KB

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