aasmtai.pas 56 KB

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