aasmtai.pas 57 KB

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