aasmtai.pas 74 KB

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