aasmtai.pas 75 KB

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