aasmtai.pas 74 KB

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