aasmtai.pas 57 KB

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