aasmtai.pas 58 KB

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