aasmtai.pas 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204
  1. {
  2. Copyright (c) 1998-2006 by Florian Klaempfl
  3. This unit implements an abstract asmoutput class for all processor types
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. { @abstract(This unit implements an abstract asm output class for all processor types)
  18. This unit implements an abstract assembler output class for all processors, these
  19. are then overridden for each assembler writer to actually write the data in these
  20. classes to an assembler file.
  21. }
  22. unit aasmtai;
  23. {$i fpcdefs.inc}
  24. interface
  25. uses
  26. cutils,cclasses,
  27. globtype,systems,
  28. cpuinfo,cpubase,
  29. cgbase,cgutils,
  30. symtype,
  31. aasmbase,aasmdata,ogbase
  32. {$ifdef jvm}
  33. ,widestr
  34. {$endif jvm}
  35. ;
  36. type
  37. { keep the number of elements in this enumeration less or equal than 32 as long
  38. as FPC knows only 4 byte and 32 byte sets (FK) }
  39. taitype = (
  40. ait_none,
  41. ait_align,
  42. ait_section,
  43. ait_comment,
  44. ait_string,
  45. ait_instruction,
  46. ait_datablock,
  47. ait_symbol,
  48. { needed to calc the size of a symbol }
  49. ait_symbol_end,
  50. ait_directive,
  51. ait_label,
  52. ait_const,
  53. ait_real_32bit,
  54. ait_real_64bit,
  55. ait_real_80bit,
  56. ait_comp_64bit,
  57. ait_real_128bit,
  58. ait_stab,
  59. ait_force_line,
  60. ait_function_name,
  61. {$ifdef alpha}
  62. { the follow is for the DEC Alpha }
  63. ait_frame,
  64. {$endif alpha}
  65. {$ifdef ia64}
  66. ait_bundle,
  67. ait_stop,
  68. {$endif ia64}
  69. {$ifdef m68k}
  70. ait_labeled_instruction,
  71. {$endif m68k}
  72. {$ifdef arm}
  73. ait_thumb_func,
  74. ait_thumb_set,
  75. {$endif arm}
  76. ait_set,
  77. ait_weak,
  78. { used to split into tiny assembler files }
  79. ait_cutobject,
  80. ait_regalloc,
  81. ait_tempalloc,
  82. { used to mark assembler blocks and inlined functions }
  83. ait_marker,
  84. { used to describe a new location of a variable }
  85. ait_varloc,
  86. {$ifdef JVM}
  87. { JVM only }
  88. ait_jvar, { debug information for a local variable }
  89. ait_jcatch, { exception catch clause }
  90. {$endif JVM}
  91. { SEH directives used in ARM,MIPS and x86_64 COFF targets }
  92. ait_seh_directive
  93. );
  94. taiconst_type = (
  95. aitconst_128bit,
  96. aitconst_64bit,
  97. aitconst_32bit,
  98. aitconst_16bit,
  99. aitconst_8bit,
  100. aitconst_sleb128bit,
  101. aitconst_uleb128bit,
  102. { win32 only }
  103. aitconst_rva_symbol,
  104. aitconst_secrel32_symbol,
  105. { darwin only }
  106. { From gcc/config/darwin.c (darwin_asm_output_dwarf_delta):
  107. ***
  108. Output a difference of two labels that will be an assembly time
  109. constant if the two labels are local. (.long lab1-lab2 will be
  110. very different if lab1 is at the boundary between two sections; it
  111. will be relocated according to the second section, not the first,
  112. so one ends up with a difference between labels in different
  113. sections, which is bad in the dwarf2 eh context for instance.)
  114. ***
  115. We cannot use this everywhere, because older versions of the
  116. darwin assembler don't support the construct used for these
  117. relsyms (nor do they support dwarf, for that matter)
  118. }
  119. aitconst_darwin_dwarf_delta64,
  120. aitconst_darwin_dwarf_delta32,
  121. { ARM Thumb-2 only }
  122. aitconst_half16bit, { used for table jumps. The actual value is the 16bit value shifted left once }
  123. { for use by dwarf debugger information }
  124. aitconst_16bit_unaligned,
  125. aitconst_32bit_unaligned,
  126. aitconst_64bit_unaligned,
  127. { i8086 far pointer; emits: 'DW symbol, SEG symbol' }
  128. aitconst_farptr,
  129. { offset of symbol's GOT slot in GOT }
  130. aitconst_got,
  131. { offset of symbol itself from GOT }
  132. aitconst_gotoff_symbol
  133. );
  134. const
  135. {$if defined(cpu64bitaddr)}
  136. aitconst_ptr = aitconst_64bit;
  137. aitconst_ptr_unaligned = aitconst_64bit_unaligned;
  138. {$elseif defined(cpu32bitaddr)}
  139. aitconst_ptr = aitconst_32bit;
  140. aitconst_ptr_unaligned = aitconst_32bit_unaligned;
  141. {$elseif defined(cpu16bitaddr)}
  142. aitconst_ptr = aitconst_16bit;
  143. aitconst_ptr_unaligned = aitconst_16bit_unaligned;
  144. {$endif}
  145. {$if defined(cpu64bitalu)}
  146. aitconst_aint = aitconst_64bit;
  147. {$elseif defined(cpu32bitalu)}
  148. aitconst_aint = aitconst_32bit;
  149. {$elseif defined(cpu16bitalu)}
  150. aitconst_aint = aitconst_16bit;
  151. {$elseif defined(cpu8bitalu)}
  152. aitconst_aint = aitconst_8bit;
  153. {$endif}
  154. taitypestr : array[taitype] of string[24] = (
  155. '<none>',
  156. 'align',
  157. 'section',
  158. 'comment',
  159. 'string',
  160. 'instruction',
  161. 'datablock',
  162. 'symbol',
  163. 'symbol_end',
  164. 'symbol_directive',
  165. 'label',
  166. 'const',
  167. 'real_32bit',
  168. 'real_64bit',
  169. 'real_80bit',
  170. 'comp_64bit',
  171. 'real_128bit',
  172. 'stab',
  173. 'force_line',
  174. 'function_name',
  175. {$ifdef alpha}
  176. { the follow is for the DEC Alpha }
  177. 'frame',
  178. {$endif alpha}
  179. {$ifdef ia64}
  180. 'bundle',
  181. 'stop',
  182. {$endif ia64}
  183. {$ifdef m68k}
  184. 'labeled_instr',
  185. {$endif m68k}
  186. {$ifdef arm}
  187. 'thumb_func',
  188. 'thumb_set',
  189. {$endif arm}
  190. 'set',
  191. 'weak',
  192. 'cut',
  193. 'regalloc',
  194. 'tempalloc',
  195. 'marker',
  196. 'varloc',
  197. {$ifdef JVM}
  198. 'jvar',
  199. 'jcatch',
  200. {$endif JVM}
  201. 'seh_directive'
  202. );
  203. type
  204. { Types of operand }
  205. toptype=(top_none,top_reg,top_ref,top_const,top_bool,top_local
  206. {$ifdef arm}
  207. { ARM only }
  208. ,top_regset
  209. ,top_modeflags
  210. ,top_specialreg
  211. {$endif arm}
  212. {$if defined(arm) or defined(aarch64)}
  213. ,top_conditioncode
  214. ,top_shifterop
  215. {$endif defined(arm) or defined(aarch64)}
  216. {$ifdef m68k}
  217. { m68k only }
  218. ,top_regset
  219. {$endif m68k}
  220. {$ifdef jvm}
  221. { jvm only}
  222. ,top_single
  223. ,top_double
  224. ,top_string
  225. ,top_wstring
  226. {$endif jvm}
  227. );
  228. { kinds of operations that an instruction can perform on an operand }
  229. topertype = (operand_read,operand_write,operand_readwrite);
  230. tlocaloper = record
  231. localsym : pointer;
  232. localsymderef : tderef;
  233. localsymofs : longint;
  234. localindexreg : tregister;
  235. localscale : byte;
  236. localgetoffset,
  237. localforceref : boolean
  238. end;
  239. plocaloper = ^tlocaloper;
  240. { please keep the size of this record <=12 bytes and keep it properly aligned }
  241. toper = record
  242. ot : longint;
  243. case typ : toptype of
  244. top_none : ();
  245. top_reg : (reg:tregister);
  246. top_ref : (ref:preference);
  247. top_const : (val:tcgint);
  248. top_bool : (b:boolean);
  249. { local varsym that will be inserted in pass_generate_code }
  250. top_local : (localoper:plocaloper);
  251. {$ifdef arm}
  252. top_regset : (regset:^tcpuregisterset; regtyp: tregistertype; subreg: tsubregister; usermode: boolean);
  253. top_modeflags : (modeflags : tcpumodeflags);
  254. top_specialreg : (specialreg:tregister; specialflags:tspecialregflags);
  255. {$endif arm}
  256. {$if defined(arm) or defined(aarch64)}
  257. top_shifterop : (shifterop : pshifterop);
  258. top_conditioncode : (cc : TAsmCond);
  259. {$endif defined(arm) or defined(aarch64)}
  260. {$ifdef m68k}
  261. top_regset : (dataregset,addrregset,fpuregset:^tcpuregisterset);
  262. {$endif m68k}
  263. {$ifdef jvm}
  264. top_single : (sval:single);
  265. top_double : (dval:double);
  266. top_string : (pcvallen: aint; pcval: pchar);
  267. top_wstring : (pwstrval: pcompilerwidestring);
  268. {$endif jvm}
  269. end;
  270. poper=^toper;
  271. const
  272. { ait_* types which don't result in executable code or which don't influence
  273. the way the program runs/behaves, but which may be encountered by the
  274. optimizer (= if it's sometimes added to the exprasm list). Update if you add
  275. a new ait type! }
  276. SkipInstr = [ait_comment, ait_symbol,ait_section
  277. ,ait_stab, ait_function_name, ait_force_line
  278. ,ait_regalloc, ait_tempalloc, ait_symbol_end
  279. ,ait_directive
  280. ,ait_varloc,
  281. {$ifdef JVM}
  282. ait_jvar, ait_jcatch,
  283. {$endif JVM}
  284. ait_seh_directive];
  285. { ait_* types which do not have line information (and hence which are of type
  286. tai, otherwise, they are of type tailineinfo }
  287. SkipLineInfo =[ait_label,
  288. ait_regalloc,ait_tempalloc,
  289. ait_stab,ait_function_name,
  290. ait_cutobject,ait_marker,ait_varloc,ait_align,ait_section,ait_comment,
  291. ait_const,ait_directive,
  292. {$ifdef arm}
  293. ait_thumb_func,
  294. ait_thumb_set,
  295. {$endif arm}
  296. ait_set,ait_weak,
  297. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_real_128bit,
  298. ait_symbol,
  299. {$ifdef JVM}
  300. ait_jvar, ait_jcatch,
  301. {$endif JVM}
  302. ait_seh_directive
  303. ];
  304. type
  305. { cut type, required for alphanumeric ordering of the assembler filenames }
  306. TCutPlace=(cut_normal,cut_begin,cut_end);
  307. TAsmMarker = (
  308. mark_NoPropInfoStart,mark_NoPropInfoEnd,
  309. mark_AsmBlockStart,mark_AsmBlockEnd,
  310. mark_NoLineInfoStart,mark_NoLineInfoEnd,mark_BlockStart,
  311. mark_Position
  312. );
  313. TRegAllocType = (ra_alloc,ra_dealloc,ra_sync,ra_resize,ra_markused);
  314. TStabType = (stab_stabs,stab_stabn,stab_stabd,
  315. { AIX/XCOFF stab types }
  316. stab_stabx,
  317. { begin/end include file }
  318. stabx_bi,stabx_ei,
  319. { begin/end function }
  320. stabx_bf, stabx_ef,
  321. { begin/end static data block }
  322. stabx_bs, stabx_es,
  323. { line spec, function start/end label }
  324. stabx_line, stabx_function);
  325. TAsmDirective=(
  326. asd_indirect_symbol,
  327. asd_extern,asd_nasm_import, asd_toc_entry,
  328. asd_reference,asd_no_dead_strip,asd_weak_reference,asd_lazy_reference,
  329. asd_weak_definition,
  330. { for Jasmin }
  331. asd_jclass,asd_jinterface,asd_jsuper,asd_jfield,asd_jlimit,asd_jline,
  332. { .ent/.end for MIPS and Alpha }
  333. asd_ent,asd_ent_end,
  334. { supported by recent clang-based assemblers for data-in-code }
  335. asd_data_region, asd_end_data_region
  336. );
  337. TAsmSehDirective=(
  338. ash_proc,ash_endproc,
  339. ash_endprologue,ash_handler,ash_handlerdata,
  340. ash_eh,ash_32,ash_no32,
  341. ash_setframe,ash_stackalloc,ash_pushreg,
  342. ash_savereg,ash_savexmm,ash_pushframe
  343. );
  344. const
  345. regallocstr : array[tregalloctype] of string[10]=('allocated','released','sync','resized','used');
  346. tempallocstr : array[boolean] of string[10]=('released','allocated');
  347. stabtypestr : array[TStabType] of string[8]=(
  348. 'stabs','stabn','stabd',
  349. 'stabx',
  350. 'bi','ei',
  351. 'bf','ef',
  352. 'bs','es',
  353. 'line','function');
  354. directivestr : array[TAsmDirective] of string[23]=(
  355. 'indirect_symbol',
  356. 'extern','nasm_import', 'tc', 'reference',
  357. 'no_dead_strip','weak_reference','lazy_reference','weak_definition',
  358. { for Jasmin }
  359. 'class','interface','super','field','limit','line',
  360. { .ent/.end for MIPS and Alpha }
  361. 'ent','end',
  362. { supported by recent clang-based assemblers for data-in-code }
  363. 'data_region','end_data_region'
  364. );
  365. sehdirectivestr : array[TAsmSehDirective] of string[16]=(
  366. '.seh_proc','.seh_endproc',
  367. '.seh_endprologue','.seh_handler','.seh_handlerdata',
  368. '.seh_eh','.seh_32','seh_no32',
  369. '.seh_setframe','.seh_stackalloc','.seh_pushreg',
  370. '.seh_savereg','.seh_savexmm','.seh_pushframe'
  371. );
  372. type
  373. { abstract assembler item }
  374. tai = class(TLinkedListItem)
  375. {$ifndef NOOPT}
  376. { pointer to record with optimizer info about this tai object }
  377. optinfo : pointer;
  378. {$endif NOOPT}
  379. typ : taitype;
  380. constructor Create;
  381. constructor ppuload(t:taitype;ppufile:tcompilerppufile);virtual;
  382. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  383. procedure buildderefimpl;virtual;
  384. procedure derefimpl;virtual;
  385. end;
  386. { abstract assembler item with line information }
  387. tailineinfo = class(tai)
  388. fileinfo : tfileposinfo;
  389. constructor Create;
  390. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  391. procedure ppuwrite(ppufile:tcompilerppufile);override;
  392. end;
  393. tai_simple = class(tai)
  394. constructor create(_typ : taitype);
  395. end;
  396. taiclass = class of tai;
  397. taiclassarray = array[taitype] of taiclass;
  398. { Generates an assembler string }
  399. tai_string = class(tailineinfo)
  400. str : pchar;
  401. { extra len so the string can contain an \0 }
  402. len : longint;
  403. constructor Create(const _str : string);
  404. constructor Create_pchar(_str : pchar;length : longint);
  405. destructor Destroy;override;
  406. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  407. procedure ppuwrite(ppufile:tcompilerppufile);override;
  408. function getcopy:tlinkedlistitem;override;
  409. end;
  410. { Generates a common label }
  411. tai_symbol = class(tai)
  412. sym : tasmsymbol;
  413. value : puint;
  414. size : longint;
  415. is_global,
  416. has_value : boolean;
  417. constructor Create(_sym:tasmsymbol;siz:longint);
  418. constructor Create_Global(_sym:tasmsymbol;siz:longint);
  419. constructor Createname(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  420. constructor Createname_global(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  421. constructor Createname_global_value(const _name : string;_symtyp:Tasmsymtype;siz:longint;val:ptruint);
  422. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  423. procedure ppuwrite(ppufile:tcompilerppufile);override;
  424. procedure derefimpl;override;
  425. end;
  426. tai_symbol_end = class(tailineinfo)
  427. sym : tasmsymbol;
  428. constructor Create(_sym:tasmsymbol);
  429. constructor Createname(const _name : string);
  430. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  431. procedure ppuwrite(ppufile:tcompilerppufile);override;
  432. procedure derefimpl;override;
  433. end;
  434. tai_directive = class(tailineinfo)
  435. name : ansistring;
  436. directive : TAsmDirective;
  437. constructor Create(_directive:TAsmDirective;const _name:ansistring);
  438. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  439. procedure ppuwrite(ppufile:tcompilerppufile);override;
  440. end;
  441. { Generates an assembler label }
  442. tai_label = class(tai)
  443. labsym : tasmlabel;
  444. {$ifdef arm}
  445. { set to true when the label has been moved by insertpcrelativedata to the correct location
  446. so one label can be used multiple times }
  447. moved : boolean;
  448. { true, if a label has been already inserted, this is important for arm thumb where no negative
  449. pc relative offsets are allowed }
  450. inserted : boolean;
  451. {$endif arm}
  452. constructor Create(_labsym : tasmlabel);
  453. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  454. procedure ppuwrite(ppufile:tcompilerppufile);override;
  455. procedure derefimpl;override;
  456. end;
  457. { Generates an assembler comment }
  458. tai_comment = class(tai)
  459. str : pchar;
  460. constructor Create(_str : pchar);
  461. destructor Destroy; override;
  462. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  463. procedure ppuwrite(ppufile:tcompilerppufile);override;
  464. function getcopy:tlinkedlistitem;override;
  465. end;
  466. { Generates a section / segment directive }
  467. tai_section = class(tai)
  468. sectype : TAsmSectiontype;
  469. secorder : TasmSectionorder;
  470. secalign : byte;
  471. name : pshortstring;
  472. sec : TObjSection; { used in binary writer }
  473. destructor Destroy;override;
  474. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  475. procedure ppuwrite(ppufile:tcompilerppufile);override;
  476. {$push}{$warnings off}
  477. private
  478. { this constructor is made private on purpose }
  479. { because sections should be created via new_section() }
  480. constructor Create(Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  481. {$pop}
  482. end;
  483. { Generates an uninitializised data block }
  484. tai_datablock = class(tailineinfo)
  485. is_global : boolean;
  486. sym : tasmsymbol;
  487. size : asizeint;
  488. constructor Create(const _name : string;_size : aint);
  489. constructor Create_global(const _name : string;_size : asizeint);
  490. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  491. procedure ppuwrite(ppufile:tcompilerppufile);override;
  492. procedure derefimpl;override;
  493. end;
  494. { Generates an integer const }
  495. { tai_const }
  496. tai_const = class(tai)
  497. sym,
  498. endsym : tasmsymbol;
  499. { if symbols and offset are provided the symofs is used,
  500. the value is calculated during assembling }
  501. symofs,
  502. value : int64;
  503. consttype : taiconst_type;
  504. { we use for the 128bit int64/qword for now because I can't imagine a
  505. case where we need 128 bit now (FK) }
  506. constructor Create(_typ:taiconst_type;_value : int64);
  507. constructor Create_128bit(_value : int64);
  508. constructor Create_64bit(_value : int64);
  509. constructor Create_32bit(_value : longint);
  510. constructor Create_16bit(_value : word);
  511. constructor Create_64bit_unaligned(_value : int64);
  512. constructor Create_32bit_unaligned(_value : longint);
  513. constructor Create_16bit_unaligned(_value : word);
  514. constructor Create_8bit(_value : byte);
  515. constructor Create_char(size: integer; _value: dword);
  516. constructor Create_sleb128bit(_value : int64);
  517. constructor Create_uleb128bit(_value : qword);
  518. constructor Create_aint(_value : aint);
  519. constructor Create_pint(_value : pint);
  520. constructor Create_pint_unaligned(_value : pint);
  521. constructor Create_sym(_sym:tasmsymbol);
  522. {$ifdef i8086}
  523. constructor Create_sym_near(_sym:tasmsymbol);
  524. constructor Create_sym_far(_sym:tasmsymbol);
  525. {$endif i8086}
  526. constructor Create_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  527. constructor Create_sym_offset(_sym:tasmsymbol;ofs:aint);
  528. constructor Create_type_sym_offset(_typ:taiconst_type;_sym:tasmsymbol;ofs:aint);
  529. constructor Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  530. constructor Create_rel_sym_offset(_typ : taiconst_type; _sym,_endsym : tasmsymbol; _ofs : int64);
  531. constructor Create_rva_sym(_sym:tasmsymbol);
  532. constructor Createname(const name:string;ofs:aint);
  533. constructor Createname(const name:string;_symtyp:Tasmsymtype;ofs:aint);
  534. constructor Create_type_name(_typ:taiconst_type;const name:string;ofs:aint);
  535. constructor Create_type_name(_typ:taiconst_type;const name:string;_symtyp:Tasmsymtype;ofs:aint);
  536. constructor Create_nil_codeptr;
  537. constructor Create_nil_dataptr;
  538. constructor Create_int_codeptr(_value: int64);
  539. constructor Create_int_dataptr(_value: int64);
  540. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  541. procedure ppuwrite(ppufile:tcompilerppufile);override;
  542. procedure derefimpl;override;
  543. function getcopy:tlinkedlistitem;override;
  544. function size:longint;
  545. end;
  546. { Generates a single float (32 bit real) }
  547. tai_real_32bit = class(tai)
  548. value : ts32real;
  549. constructor Create(_value : ts32real);
  550. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  551. procedure ppuwrite(ppufile:tcompilerppufile);override;
  552. end;
  553. tformatoptions = (fo_none,fo_hiloswapped);
  554. { Generates a double float (64 bit real) }
  555. tai_real_64bit = class(tai)
  556. value : ts64real;
  557. {$ifdef ARM}
  558. formatoptions : tformatoptions;
  559. constructor Create_hiloswapped(_value : ts64real);
  560. {$endif ARM}
  561. constructor Create(_value : ts64real);
  562. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  563. procedure ppuwrite(ppufile:tcompilerppufile);override;
  564. end;
  565. { Generates an extended float (80 bit real) }
  566. tai_real_80bit = class(tai)
  567. value : ts80real;
  568. savesize : byte;
  569. constructor Create(_value : ts80real; _savesize: byte);
  570. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  571. procedure ppuwrite(ppufile:tcompilerppufile);override;
  572. end;
  573. { Generates an float128 (128 bit real) }
  574. tai_real_128bit = class(tai)
  575. value : ts128real;
  576. constructor Create(_value : ts128real);
  577. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  578. procedure ppuwrite(ppufile:tcompilerppufile);override;
  579. end;
  580. { Generates a comp int (integer over 64 bits)
  581. This is Intel 80x86 specific, and is not
  582. really supported on other processors.
  583. }
  584. tai_comp_64bit = class(tai)
  585. value : ts64comp;
  586. constructor Create(_value : ts64comp);
  587. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  588. procedure ppuwrite(ppufile:tcompilerppufile);override;
  589. end;
  590. { tai_stab }
  591. tai_stab = class(tai)
  592. str : pchar;
  593. stabtype : TStabType;
  594. constructor Create(_stabtype:TStabType;_str : pchar);
  595. constructor Create_str(_stabtype:TStabType;const s:string);
  596. constructor create_ansistr(_stabtype: TStabType; const s: ansistring);
  597. destructor Destroy;override;
  598. end;
  599. tai_force_line = class(tailineinfo)
  600. constructor Create;
  601. end;
  602. tai_function_name = class(tai)
  603. funcname : pshortstring;
  604. constructor create(const s:string);
  605. destructor destroy;override;
  606. end;
  607. { Insert a cut to split assembler into several smaller files }
  608. tai_cutobject = class(tai)
  609. place : tcutplace;
  610. constructor Create;
  611. constructor Create_begin;
  612. constructor Create_end;
  613. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  614. procedure ppuwrite(ppufile:tcompilerppufile);override;
  615. end;
  616. { Insert a marker for assembler and inline blocks }
  617. tai_marker = class(tai)
  618. Kind: TAsmMarker;
  619. Constructor Create(_Kind: TAsmMarker);
  620. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  621. procedure ppuwrite(ppufile:tcompilerppufile);override;
  622. end;
  623. tai_tempalloc = class(tai)
  624. allocation : boolean;
  625. {$ifdef EXTDEBUG}
  626. problem : pshortstring;
  627. {$endif EXTDEBUG}
  628. temppos,
  629. tempsize : longint;
  630. constructor alloc(pos,size:longint);
  631. constructor dealloc(pos,size:longint);
  632. {$ifdef EXTDEBUG}
  633. constructor allocinfo(pos,size:longint;const st:string);
  634. {$endif EXTDEBUG}
  635. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  636. destructor destroy;override;
  637. procedure ppuwrite(ppufile:tcompilerppufile);override;
  638. end;
  639. tai_regalloc = class(tai)
  640. reg : tregister;
  641. ratype : TRegAllocType;
  642. { tells BuildLabelTableAndFixRegAlloc that the deallocation should be kept }
  643. keep : boolean;
  644. { reg(de)alloc belongs to this instruction, this
  645. is only used for automatic inserted (de)alloc for
  646. imaginary register and required for spilling code }
  647. instr : tai;
  648. constructor alloc(r : tregister;ainstr:tai);
  649. constructor dealloc(r : tregister;ainstr:tai);
  650. constructor sync(r : tregister);
  651. constructor resize(r : tregister);
  652. constructor markused(r : tregister);
  653. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  654. procedure ppuwrite(ppufile:tcompilerppufile);override;
  655. end;
  656. tadd_reg_instruction_proc=procedure(instr:Tai;r:tregister) of object;
  657. { Class template for assembler instructions
  658. }
  659. tai_cpu_abstract = class(tailineinfo)
  660. protected
  661. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);virtual;
  662. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);virtual;
  663. procedure ppubuildderefimploper(var o:toper);virtual;abstract;
  664. procedure ppuderefoper(var o:toper);virtual;abstract;
  665. public
  666. { Condition flags for instruction }
  667. condition : TAsmCond;
  668. { Number of operands to instruction }
  669. ops : byte;
  670. { Number of allocate oper structures }
  671. opercnt : byte;
  672. { Operands of instruction }
  673. oper : array[0..max_operands-1] of poper;
  674. { Actual opcode of instruction }
  675. opcode : tasmop;
  676. {$ifdef x86}
  677. segprefix : tregister;
  678. {$endif x86}
  679. { true if instruction is a jmp }
  680. is_jmp : boolean; { is this instruction a jump? (needed for optimizer) }
  681. Constructor Create(op : tasmop);virtual;
  682. Destructor Destroy;override;
  683. function getcopy:TLinkedListItem;override;
  684. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  685. procedure ppuwrite(ppufile:tcompilerppufile);override;
  686. procedure buildderefimpl;override;
  687. procedure derefimpl;override;
  688. procedure SetCondition(const c:TAsmCond);
  689. procedure allocate_oper(opers:longint);
  690. procedure loadconst(opidx:longint;l:aint);
  691. procedure loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  692. procedure loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset,forceref:boolean);
  693. procedure loadref(opidx:longint;const r:treference);
  694. procedure loadreg(opidx:longint;r:tregister);
  695. procedure loadoper(opidx:longint;o:toper);
  696. procedure clearop(opidx:longint);
  697. procedure freeop(opidx:longint);
  698. { register allocator }
  699. function is_same_reg_move(regtype: Tregistertype):boolean;virtual;
  700. function spilling_get_operation_type(opnr: longint): topertype;virtual;
  701. function spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;virtual;
  702. function Pass1(objdata:TObjData):longint;virtual;
  703. procedure Pass2(objdata:TObjData);virtual;
  704. procedure resetpass1; virtual;
  705. procedure resetpass2; virtual;
  706. end;
  707. tai_cpu_class = class of tai_cpu_abstract;
  708. { Buffer type used for alignment }
  709. tfillbuffer = array[0..63] of char;
  710. { alignment for operator }
  711. tai_align_abstract = class(tai)
  712. aligntype : byte; { 1 = no align, 2 = word align, 4 = dword align }
  713. fillsize : byte; { real size to fill }
  714. fillop : byte; { value to fill with - optional }
  715. use_op : boolean;
  716. constructor Create(b:byte);virtual;
  717. constructor Create_op(b: byte; _op: byte);virtual;
  718. constructor Create_zeros(b:byte);
  719. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  720. procedure ppuwrite(ppufile:tcompilerppufile);override;
  721. function calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;virtual;
  722. end;
  723. tai_align_class = class of tai_align_abstract;
  724. tai_varloc = class(tai)
  725. oldlocation,
  726. oldlocationhi,
  727. newlocation,
  728. newlocationhi : tregister;
  729. varsym : tsym;
  730. constructor create(sym : tsym;loc : tregister);
  731. constructor create64(sym : tsym;loc,lochi : tregister);
  732. {$ifdef cpu64bitalu}
  733. constructor create128(sym : tsym;loc,lochi : tregister);
  734. {$endif cpu64bitalu}
  735. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  736. procedure ppuwrite(ppufile:tcompilerppufile);override;
  737. procedure buildderefimpl;override;
  738. procedure derefimpl;override;
  739. end;
  740. TSehDirectiveDatatype=(sd_none,sd_string,sd_reg,sd_offset,sd_regoffset);
  741. TSehDirectiveData=record
  742. case typ: TSehDirectiveDatatype of
  743. sd_none: ();
  744. sd_string: (name:pshortstring;flags:byte);
  745. sd_reg,sd_offset,sd_regoffset: (reg:TRegister;offset:dword);
  746. end;
  747. tai_seh_directive = class(tai)
  748. kind: TAsmSehDirective;
  749. data: TSehDirectiveData;
  750. constructor create(_kind:TAsmSehDirective);
  751. constructor create_name(_kind:TAsmSehDirective;const _name: string);
  752. constructor create_reg(_kind:TAsmSehDirective;r:TRegister);
  753. constructor create_offset(_kind:TAsmSehDirective;ofs:dword);
  754. constructor create_reg_offset(_kind:TAsmSehDirective;r:TRegister;ofs:dword);
  755. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  756. destructor destroy;override;
  757. procedure ppuwrite(ppufile:tcompilerppufile);override;
  758. procedure generate_code(objdata:TObjData);virtual;
  759. property datatype: TSehDirectiveDatatype read data.typ;
  760. end;
  761. tai_seh_directive_class=class of tai_seh_directive;
  762. {$ifdef JVM}
  763. { JVM variable live range description }
  764. tai_jvar = class(tai)
  765. stackslot: longint;
  766. desc: pshortstring;
  767. startlab,stoplab: tasmsymbol;
  768. constructor Create(_stackslot: longint; const _desc: shortstring; _startlab, _stoplab: TAsmSymbol);
  769. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  770. procedure ppuwrite(ppufile:tcompilerppufile);override;
  771. destructor destroy;override;
  772. end;
  773. tai_jvar_class = class of tai_jvar;
  774. { JVM exception catch description }
  775. tai_jcatch = class(tai)
  776. name: pshortstring;
  777. startlab,stoplab,handlerlab: tasmsymbol;
  778. constructor Create(const _name: shortstring; _startlab, _stoplab, _handlerlab: TAsmSymbol);
  779. destructor destroy;override;
  780. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  781. procedure ppuwrite(ppufile:tcompilerppufile);override;
  782. end;
  783. tai_jcatch_class = class of tai_jcatch;
  784. {$endif JVM}
  785. tai_set = class(tai)
  786. sym,
  787. value: pshortstring;
  788. constructor create(const asym, avalue: string);
  789. destructor destroy;override;
  790. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  791. procedure ppuwrite(ppufile:tcompilerppufile);override;
  792. end;
  793. {$ifdef arm}
  794. tai_thumb_set = class(tai_set)
  795. constructor create(const asym, avalue: string);
  796. end;
  797. {$endif arm}
  798. tai_weak = class(tai)
  799. sym: pshortstring;
  800. constructor create(const asym: string);
  801. destructor destroy;override;
  802. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  803. procedure ppuwrite(ppufile:tcompilerppufile);override;
  804. end;
  805. var
  806. { array with all class types for tais }
  807. aiclass : taiclassarray;
  808. { target specific tais, possibly overwritten in target specific aasmcpu }
  809. cai_align : tai_align_class = tai_align_abstract;
  810. cai_cpu : tai_cpu_class = tai_cpu_abstract;
  811. cai_seh_directive: tai_seh_directive_class = tai_seh_directive;
  812. { hook to notify uses of registers }
  813. add_reg_instruction_hook : tadd_reg_instruction_proc;
  814. procedure maybe_new_object_file(list:TAsmList);
  815. procedure new_section(list:TAsmList;Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  816. procedure section_symbol_start(list:TAsmList;const Aname:string;Asymtyp:Tasmsymtype;
  817. Aglobal:boolean;Asectype:TAsmSectiontype;Aalign:byte);
  818. procedure section_symbol_end(list:TAsmList;const Aname:string);
  819. function ppuloadai(ppufile:tcompilerppufile):tai;
  820. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  821. implementation
  822. uses
  823. SysUtils,
  824. verbose,
  825. globals;
  826. const
  827. pputaimarker = 254;
  828. {****************************************************************************
  829. Helpers
  830. ****************************************************************************}
  831. procedure maybe_new_object_file(list:TAsmList);
  832. begin
  833. if create_smartlink_library then
  834. list.concat(tai_cutobject.create);
  835. end;
  836. procedure new_section(list:TAsmList;Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  837. begin
  838. list.concat(tai_section.create(Asectype,Aname,Aalign,Asecorder));
  839. list.concat(cai_align.create(Aalign));
  840. end;
  841. procedure section_symbol_start(list:TAsmList;const Aname:string;Asymtyp:Tasmsymtype;
  842. Aglobal:boolean;Asectype:TAsmSectiontype;Aalign:byte);
  843. begin
  844. maybe_new_object_file(list);
  845. new_section(list,Asectype,Aname,Aalign);
  846. if Aglobal or
  847. create_smartlink then
  848. list.concat(tai_symbol.createname_global(Aname,Asymtyp,0))
  849. else
  850. list.concat(tai_symbol.createname(Aname,Asymtyp,0));
  851. end;
  852. procedure section_symbol_end(list:TAsmList;const Aname:string);
  853. begin
  854. list.concat(tai_symbol_end.createname(Aname));
  855. end;
  856. function ppuloadai(ppufile:tcompilerppufile):tai;
  857. var
  858. b : byte;
  859. t : taitype;
  860. begin
  861. { marker }
  862. b:=ppufile.getbyte;
  863. if b<>pputaimarker then
  864. internalerror(200208181);
  865. { load nodetype }
  866. t:=taitype(ppufile.getbyte);
  867. if t<>ait_none then
  868. begin
  869. if t>high(taitype) then
  870. internalerror(200208182);
  871. if not assigned(aiclass[t]) then
  872. internalerror(200208183);
  873. {writeln('taiload: ',taitypestr[t]);}
  874. { generate tai of the correct class }
  875. ppuloadai:=aiclass[t].ppuload(t,ppufile);
  876. end
  877. else
  878. ppuloadai:=nil;
  879. end;
  880. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  881. begin
  882. { marker, read by ppuloadnode }
  883. ppufile.putbyte(pputaimarker);
  884. if assigned(n) then
  885. begin
  886. { type, read by ppuloadnode }
  887. ppufile.putbyte(byte(n.typ));
  888. {writeln('taiwrite: ',taitypestr[n.typ]);}
  889. n.ppuwrite(ppufile);
  890. end
  891. else
  892. ppufile.putbyte(byte(ait_none));
  893. end;
  894. constructor tai_weak.create(const asym: string);
  895. begin
  896. inherited create;
  897. typ:=ait_weak;
  898. sym:=stringdup(asym);
  899. end;
  900. destructor tai_weak.destroy;
  901. begin
  902. stringdispose(sym);
  903. inherited destroy;
  904. end;
  905. constructor tai_weak.ppuload(t: taitype; ppufile: tcompilerppufile);
  906. begin
  907. inherited ppuload(t,ppufile);
  908. sym:=stringdup(ppufile.getstring);
  909. end;
  910. procedure tai_weak.ppuwrite(ppufile: tcompilerppufile);
  911. begin
  912. inherited ppuwrite(ppufile);
  913. ppufile.putstring(sym^);
  914. end;
  915. {$ifdef arm}
  916. constructor tai_thumb_set.create(const asym, avalue: string);
  917. begin
  918. inherited create(asym, avalue);
  919. typ:=ait_thumb_set;
  920. end;
  921. {$endif arm}
  922. constructor tai_set.create(const asym, avalue: string);
  923. begin
  924. inherited create;
  925. typ:=ait_set;
  926. sym:=stringdup(asym);
  927. value:=stringdup(avalue);
  928. end;
  929. destructor tai_set.destroy;
  930. begin
  931. stringdispose(sym);
  932. stringdispose(value);
  933. inherited destroy;
  934. end;
  935. constructor tai_set.ppuload(t: taitype; ppufile: tcompilerppufile);
  936. begin
  937. inherited ppuload(t,ppufile);
  938. sym:=stringdup(ppufile.getstring);
  939. value:=stringdup(ppufile.getstring);
  940. end;
  941. procedure tai_set.ppuwrite(ppufile: tcompilerppufile);
  942. begin
  943. inherited ppuwrite(ppufile);
  944. ppufile.putstring(sym^);
  945. ppufile.putstring(value^);
  946. end;
  947. constructor tai_varloc.create(sym: tsym; loc: tregister);
  948. begin
  949. inherited Create;
  950. typ:=ait_varloc;
  951. newlocation:=loc;
  952. newlocationhi:=NR_NO;
  953. varsym:=sym;
  954. oldlocationhi:=NR_NO;
  955. end;
  956. constructor tai_varloc.create64(sym: tsym; loc, lochi: tregister);
  957. begin
  958. inherited Create;
  959. typ:=ait_varloc;
  960. newlocation:=loc;
  961. newlocationhi:=lochi;
  962. varsym:=sym;
  963. end;
  964. {$ifdef cpu64bitalu}
  965. constructor tai_varloc.create128(sym: tsym; loc, lochi: tregister);
  966. begin
  967. inherited Create;
  968. typ:=ait_varloc;
  969. newlocation:=loc;
  970. newlocationhi:=lochi;
  971. varsym:=sym;
  972. end;
  973. {$endif cpu64bitalu}
  974. constructor tai_varloc.ppuload(t: taitype; ppufile: tcompilerppufile);
  975. begin
  976. inherited ppuload(t, ppufile);
  977. end;
  978. procedure tai_varloc.ppuwrite(ppufile: tcompilerppufile);
  979. begin
  980. inherited ppuwrite(ppufile);
  981. end;
  982. procedure tai_varloc.buildderefimpl;
  983. begin
  984. inherited buildderefimpl;
  985. end;
  986. procedure tai_varloc.derefimpl;
  987. begin
  988. inherited derefimpl;
  989. end;
  990. {****************************************************************************
  991. TAI
  992. ****************************************************************************}
  993. constructor tai.Create;
  994. begin
  995. {$ifndef NOOPT}
  996. optinfo:=nil;
  997. {$endif NOOPT}
  998. end;
  999. constructor tai.ppuload(t:taitype;ppufile:tcompilerppufile);
  1000. begin
  1001. typ:=t;
  1002. {$ifndef NOOPT}
  1003. optinfo:=nil;
  1004. {$endif}
  1005. end;
  1006. procedure tai.ppuwrite(ppufile:tcompilerppufile);
  1007. begin
  1008. end;
  1009. procedure tai.buildderefimpl;
  1010. begin
  1011. end;
  1012. procedure tai.derefimpl;
  1013. begin
  1014. end;
  1015. {****************************************************************************
  1016. TAILINEINFO
  1017. ****************************************************************************}
  1018. constructor tailineinfo.create;
  1019. begin
  1020. inherited create;
  1021. fileinfo:=current_filepos;
  1022. end;
  1023. constructor tailineinfo.ppuload(t:taitype;ppufile:tcompilerppufile);
  1024. begin
  1025. inherited ppuload(t,ppufile);
  1026. ppufile.getposinfo(fileinfo);
  1027. end;
  1028. procedure tailineinfo.ppuwrite(ppufile:tcompilerppufile);
  1029. begin
  1030. inherited ppuwrite(ppufile);
  1031. ppufile.putposinfo(fileinfo);
  1032. end;
  1033. {****************************************************************************
  1034. TAI_SIMPLE
  1035. ****************************************************************************}
  1036. constructor tai_simple.create(_typ : taitype);
  1037. begin
  1038. inherited create;
  1039. typ:=_typ;
  1040. end;
  1041. {****************************************************************************
  1042. TAI_SECTION
  1043. ****************************************************************************}
  1044. constructor tai_section.Create(Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  1045. begin
  1046. inherited Create;
  1047. typ:=ait_section;
  1048. sectype:=asectype;
  1049. secalign:=Aalign;
  1050. secorder:=Asecorder;
  1051. name:=stringdup(Aname);
  1052. sec:=nil;
  1053. end;
  1054. constructor tai_section.ppuload(t:taitype;ppufile:tcompilerppufile);
  1055. begin
  1056. inherited ppuload(t,ppufile);
  1057. sectype:=TAsmSectiontype(ppufile.getbyte);
  1058. secalign:=ppufile.getbyte;
  1059. name:=stringdup(ppufile.getstring);
  1060. sec:=nil;
  1061. end;
  1062. destructor tai_section.Destroy;
  1063. begin
  1064. stringdispose(name);
  1065. end;
  1066. procedure tai_section.ppuwrite(ppufile:tcompilerppufile);
  1067. begin
  1068. inherited ppuwrite(ppufile);
  1069. ppufile.putbyte(byte(sectype));
  1070. ppufile.putbyte(secalign);
  1071. ppufile.putstring(name^);
  1072. end;
  1073. {****************************************************************************
  1074. TAI_DATABLOCK
  1075. ****************************************************************************}
  1076. constructor tai_datablock.Create(const _name : string;_size : aint);
  1077. begin
  1078. inherited Create;
  1079. typ:=ait_datablock;
  1080. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,AT_DATA);
  1081. { keep things aligned }
  1082. if _size<=0 then
  1083. _size:=sizeof(aint);
  1084. size:=_size;
  1085. is_global:=false;
  1086. end;
  1087. constructor tai_datablock.Create_global(const _name : string;_size : asizeint);
  1088. begin
  1089. inherited Create;
  1090. typ:=ait_datablock;
  1091. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,AT_DATA);
  1092. { keep things aligned }
  1093. if _size<=0 then
  1094. _size:=sizeof(aint);
  1095. size:=_size;
  1096. is_global:=true;
  1097. end;
  1098. constructor tai_datablock.ppuload(t:taitype;ppufile:tcompilerppufile);
  1099. begin
  1100. inherited Create;
  1101. sym:=ppufile.getasmsymbol;
  1102. size:=ppufile.getaint;
  1103. is_global:=boolean(ppufile.getbyte);
  1104. end;
  1105. procedure tai_datablock.ppuwrite(ppufile:tcompilerppufile);
  1106. begin
  1107. inherited ppuwrite(ppufile);
  1108. ppufile.putasmsymbol(sym);
  1109. ppufile.putaint(size);
  1110. ppufile.putbyte(byte(is_global));
  1111. end;
  1112. procedure tai_datablock.derefimpl;
  1113. begin
  1114. end;
  1115. {****************************************************************************
  1116. TAI_SYMBOL
  1117. ****************************************************************************}
  1118. constructor tai_symbol.Create(_sym:tasmsymbol;siz:longint);
  1119. begin
  1120. inherited Create;
  1121. typ:=ait_symbol;
  1122. sym:=_sym;
  1123. size:=siz;
  1124. { don't redefine global/external symbols as local, as code to access
  1125. such symbols is different on some platforms }
  1126. if not(sym.bind in [AB_NONE,AB_LOCAL]) then
  1127. internalerror(2013081601);
  1128. sym.bind:=AB_LOCAL;
  1129. is_global:=false;
  1130. end;
  1131. constructor tai_symbol.Create_global(_sym:tasmsymbol;siz:longint);
  1132. begin
  1133. inherited Create;
  1134. typ:=ait_symbol;
  1135. sym:=_sym;
  1136. size:=siz;
  1137. { don't override PRIVATE_EXTERN with GLOBAL }
  1138. if not(sym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN]) then
  1139. sym.bind:=AB_GLOBAL;
  1140. is_global:=true;
  1141. end;
  1142. constructor tai_symbol.Createname(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  1143. begin
  1144. inherited Create;
  1145. typ:=ait_symbol;
  1146. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,_symtyp);
  1147. size:=siz;
  1148. is_global:=false;
  1149. end;
  1150. constructor tai_symbol.Createname_global(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  1151. begin
  1152. inherited Create;
  1153. typ:=ait_symbol;
  1154. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,_symtyp);
  1155. size:=siz;
  1156. is_global:=true;
  1157. end;
  1158. constructor tai_symbol.createname_global_value(const _name: string;_symtyp: tasmsymtype; siz: longint; val: ptruint);
  1159. begin
  1160. Createname_global(_name,_symtyp,siz);
  1161. value:=val;
  1162. has_value:=true;
  1163. end;
  1164. constructor tai_symbol.ppuload(t:taitype;ppufile:tcompilerppufile);
  1165. begin
  1166. inherited ppuload(t,ppufile);
  1167. sym:=ppufile.getasmsymbol;
  1168. size:=ppufile.getlongint;
  1169. is_global:=boolean(ppufile.getbyte);
  1170. end;
  1171. procedure tai_symbol.ppuwrite(ppufile:tcompilerppufile);
  1172. begin
  1173. inherited ppuwrite(ppufile);
  1174. ppufile.putasmsymbol(sym);
  1175. ppufile.putlongint(size);
  1176. ppufile.putbyte(byte(is_global));
  1177. end;
  1178. procedure tai_symbol.derefimpl;
  1179. begin
  1180. end;
  1181. {****************************************************************************
  1182. TAI_SYMBOL_END
  1183. ****************************************************************************}
  1184. constructor tai_symbol_end.Create(_sym:tasmsymbol);
  1185. begin
  1186. inherited Create;
  1187. typ:=ait_symbol_end;
  1188. sym:=_sym;
  1189. end;
  1190. constructor tai_symbol_end.Createname(const _name : string);
  1191. begin
  1192. inherited Create;
  1193. typ:=ait_symbol_end;
  1194. sym:=current_asmdata.GetAsmSymbol(_name);
  1195. if not assigned(sym) then
  1196. internalerror(2013080301);
  1197. end;
  1198. constructor tai_symbol_end.ppuload(t:taitype;ppufile:tcompilerppufile);
  1199. begin
  1200. inherited ppuload(t,ppufile);
  1201. sym:=ppufile.getasmsymbol;
  1202. end;
  1203. procedure tai_symbol_end.ppuwrite(ppufile:tcompilerppufile);
  1204. begin
  1205. inherited ppuwrite(ppufile);
  1206. ppufile.putasmsymbol(sym);
  1207. end;
  1208. procedure tai_symbol_end.derefimpl;
  1209. begin
  1210. end;
  1211. {****************************************************************************
  1212. TAI_SYMBOL_END
  1213. ****************************************************************************}
  1214. constructor tai_directive.Create(_directive:TAsmDirective;const _name:ansistring);
  1215. begin
  1216. inherited Create;
  1217. typ:=ait_directive;
  1218. name:=_name;
  1219. directive:=_directive;
  1220. end;
  1221. constructor tai_directive.ppuload(t:taitype;ppufile:tcompilerppufile);
  1222. begin
  1223. inherited ppuload(t,ppufile);
  1224. name:=ppufile.getansistring;
  1225. directive:=TAsmDirective(ppufile.getbyte);
  1226. end;
  1227. procedure tai_directive.ppuwrite(ppufile:tcompilerppufile);
  1228. begin
  1229. inherited ppuwrite(ppufile);
  1230. ppufile.putansistring(name);
  1231. ppufile.putbyte(byte(directive));
  1232. end;
  1233. {****************************************************************************
  1234. TAI_CONST
  1235. ****************************************************************************}
  1236. constructor tai_const.Create(_typ:taiconst_type;_value : int64);
  1237. begin
  1238. inherited Create;
  1239. typ:=ait_const;
  1240. consttype:=_typ;
  1241. value:=_value;
  1242. sym:=nil;
  1243. endsym:=nil;
  1244. end;
  1245. constructor tai_const.Create_128bit(_value : int64);
  1246. begin
  1247. inherited Create;
  1248. typ:=ait_const;
  1249. consttype:=aitconst_128bit;
  1250. value:=_value;
  1251. sym:=nil;
  1252. endsym:=nil;
  1253. end;
  1254. constructor tai_const.Create_64bit(_value : int64);
  1255. begin
  1256. inherited Create;
  1257. typ:=ait_const;
  1258. consttype:=aitconst_64bit;
  1259. value:=_value;
  1260. sym:=nil;
  1261. endsym:=nil;
  1262. end;
  1263. constructor tai_const.Create_32bit(_value : longint);
  1264. begin
  1265. inherited Create;
  1266. typ:=ait_const;
  1267. consttype:=aitconst_32bit;
  1268. value:=_value;
  1269. sym:=nil;
  1270. endsym:=nil;
  1271. end;
  1272. constructor tai_const.Create_16bit(_value : word);
  1273. begin
  1274. inherited Create;
  1275. typ:=ait_const;
  1276. consttype:=aitconst_16bit;
  1277. value:=_value;
  1278. sym:=nil;
  1279. endsym:=nil;
  1280. end;
  1281. constructor tai_const.Create_64bit_unaligned(_value : int64);
  1282. begin
  1283. inherited Create;
  1284. typ:=ait_const;
  1285. consttype:=aitconst_64bit_unaligned;
  1286. value:=_value;
  1287. sym:=nil;
  1288. endsym:=nil;
  1289. end;
  1290. constructor tai_const.Create_32bit_unaligned(_value : longint);
  1291. begin
  1292. inherited Create;
  1293. typ:=ait_const;
  1294. consttype:=aitconst_32bit_unaligned;
  1295. value:=_value;
  1296. sym:=nil;
  1297. endsym:=nil;
  1298. end;
  1299. constructor tai_const.Create_16bit_unaligned(_value : word);
  1300. begin
  1301. inherited Create;
  1302. typ:=ait_const;
  1303. consttype:=aitconst_16bit_unaligned;
  1304. value:=_value;
  1305. sym:=nil;
  1306. endsym:=nil;
  1307. end;
  1308. constructor tai_const.Create_8bit(_value : byte);
  1309. begin
  1310. inherited Create;
  1311. typ:=ait_const;
  1312. consttype:=aitconst_8bit;
  1313. value:=_value;
  1314. sym:=nil;
  1315. endsym:=nil;
  1316. end;
  1317. constructor tai_const.Create_char(size: integer; _value: dword);
  1318. begin
  1319. inherited Create;
  1320. typ:=ait_const;
  1321. case size of
  1322. 1:
  1323. begin
  1324. consttype:=aitconst_8bit;
  1325. value:=byte(_value)
  1326. end;
  1327. 2:
  1328. begin
  1329. consttype:=aitconst_16bit;
  1330. value:=word(_value)
  1331. end
  1332. else
  1333. InternalError(2010030701)
  1334. end
  1335. end;
  1336. constructor tai_const.Create_sleb128bit(_value : int64);
  1337. begin
  1338. inherited Create;
  1339. typ:=ait_const;
  1340. consttype:=aitconst_sleb128bit;
  1341. value:=_value;
  1342. sym:=nil;
  1343. endsym:=nil;
  1344. end;
  1345. constructor tai_const.Create_uleb128bit(_value : qword);
  1346. begin
  1347. inherited Create;
  1348. typ:=ait_const;
  1349. consttype:=aitconst_uleb128bit;
  1350. value:=int64(_value);
  1351. sym:=nil;
  1352. endsym:=nil;
  1353. end;
  1354. constructor tai_const.Create_aint(_value : aint);
  1355. begin
  1356. inherited Create;
  1357. typ:=ait_const;
  1358. consttype:=aitconst_aint;
  1359. value:=_value;
  1360. sym:=nil;
  1361. endsym:=nil;
  1362. end;
  1363. constructor tai_const.Create_pint(_value : pint);
  1364. begin
  1365. inherited Create;
  1366. typ:=ait_const;
  1367. consttype:=aitconst_ptr;
  1368. value:=_value;
  1369. sym:=nil;
  1370. endsym:=nil;
  1371. end;
  1372. constructor tai_const.Create_pint_unaligned(_value: pint);
  1373. begin
  1374. inherited Create;
  1375. typ:=ait_const;
  1376. consttype:=aitconst_ptr_unaligned;
  1377. value:=_value;
  1378. sym:=nil;
  1379. endsym:=nil;
  1380. end;
  1381. constructor tai_const.Create_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  1382. begin
  1383. inherited Create;
  1384. typ:=ait_const;
  1385. consttype:=_typ;
  1386. sym:=_sym;
  1387. endsym:=nil;
  1388. value:=0;
  1389. { update sym info }
  1390. if assigned(sym) then
  1391. sym.increfs;
  1392. end;
  1393. constructor tai_const.Create_sym(_sym:tasmsymbol);
  1394. begin
  1395. self.create_sym_offset(_sym,0);
  1396. end;
  1397. {$ifdef i8086}
  1398. constructor tai_const.Create_sym_near(_sym: tasmsymbol);
  1399. begin
  1400. self.create_sym(_sym);
  1401. consttype:=aitconst_ptr;
  1402. end;
  1403. constructor tai_const.Create_sym_far(_sym: tasmsymbol);
  1404. begin
  1405. self.create_sym(_sym);
  1406. consttype:=aitconst_farptr;
  1407. end;
  1408. {$endif i8086}
  1409. constructor tai_const.Create_sym_offset(_sym:tasmsymbol;ofs:aint);
  1410. begin
  1411. inherited Create;
  1412. typ:=ait_const;
  1413. {$ifdef i8086}
  1414. if assigned(_sym) and (_sym.typ=AT_DATA) then
  1415. begin
  1416. if current_settings.x86memorymodel in x86_far_data_models then
  1417. consttype:=aitconst_farptr
  1418. else
  1419. consttype:=aitconst_ptr;
  1420. end
  1421. else
  1422. begin
  1423. if current_settings.x86memorymodel in x86_far_code_models then
  1424. consttype:=aitconst_farptr
  1425. else
  1426. consttype:=aitconst_ptr;
  1427. end;
  1428. {$else i8086}
  1429. consttype:=aitconst_ptr;
  1430. {$endif i8086}
  1431. { sym is allowed to be nil, this is used to write nil pointers }
  1432. sym:=_sym;
  1433. endsym:=nil;
  1434. { store the original offset in symofs so that we can recalculate the
  1435. value field in the assembler }
  1436. symofs:=ofs;
  1437. value:=ofs;
  1438. { update sym info }
  1439. if assigned(sym) then
  1440. sym.increfs;
  1441. end;
  1442. constructor tai_const.Create_type_sym_offset(_typ : taiconst_type;_sym : tasmsymbol; ofs : aint);
  1443. begin
  1444. inherited Create;
  1445. typ:=ait_const;
  1446. consttype:=_typ;
  1447. { sym is allowed to be nil, this is used to write nil pointers }
  1448. sym:=_sym;
  1449. endsym:=nil;
  1450. { store the original offset in symofs so that we can recalculate the
  1451. value field in the assembler }
  1452. symofs:=ofs;
  1453. value:=ofs;
  1454. { update sym info }
  1455. if assigned(sym) then
  1456. sym.increfs;
  1457. end;
  1458. constructor tai_const.Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  1459. begin
  1460. self.create_sym_offset(_sym,0);
  1461. consttype:=_typ;
  1462. endsym:=_endsym;
  1463. endsym.increfs;
  1464. end;
  1465. constructor tai_const.Create_rel_sym_offset(_typ: taiconst_type; _sym,_endsym: tasmsymbol; _ofs: int64);
  1466. begin
  1467. self.create_sym_offset(_sym,_ofs);
  1468. consttype:=_typ;
  1469. endsym:=_endsym;
  1470. endsym.increfs;
  1471. end;
  1472. constructor tai_const.Create_rva_sym(_sym:tasmsymbol);
  1473. begin
  1474. self.create_sym_offset(_sym,0);
  1475. consttype:=aitconst_rva_symbol;
  1476. end;
  1477. constructor tai_const.Createname(const name:string;ofs:aint);
  1478. begin
  1479. self.Createname(name,AT_NONE,ofs);
  1480. end;
  1481. constructor tai_const.Createname(const name:string;_symtyp:Tasmsymtype;ofs:aint);
  1482. begin
  1483. self.create_sym_offset(current_asmdata.RefAsmSymbol(name,_symtyp),ofs);
  1484. end;
  1485. constructor tai_const.Create_type_name(_typ:taiconst_type;const name:string;ofs:aint);
  1486. begin
  1487. self.Create_type_name(_typ,name,AT_NONE,ofs);
  1488. end;
  1489. constructor tai_const.Create_type_name(_typ:taiconst_type;const name:string;_symtyp:Tasmsymtype;ofs:aint);
  1490. begin
  1491. self.create_sym_offset(current_asmdata.RefAsmSymbol(name,_symtyp),ofs);
  1492. consttype:=_typ;
  1493. end;
  1494. constructor tai_const.Create_nil_codeptr;
  1495. begin
  1496. self.Create_int_codeptr(0);
  1497. end;
  1498. constructor tai_const.Create_nil_dataptr;
  1499. begin
  1500. self.Create_int_dataptr(0);
  1501. end;
  1502. constructor tai_const.Create_int_codeptr(_value: int64);
  1503. begin
  1504. inherited Create;
  1505. typ:=ait_const;
  1506. {$ifdef i8086}
  1507. if current_settings.x86memorymodel in x86_far_code_models then
  1508. consttype:=aitconst_farptr
  1509. else
  1510. {$endif i8086}
  1511. consttype:=aitconst_ptr;
  1512. sym:=nil;
  1513. endsym:=nil;
  1514. symofs:=0;
  1515. value:=_value;
  1516. end;
  1517. constructor tai_const.Create_int_dataptr(_value: int64);
  1518. begin
  1519. inherited Create;
  1520. typ:=ait_const;
  1521. {$ifdef i8086}
  1522. if current_settings.x86memorymodel in x86_far_data_models then
  1523. consttype:=aitconst_farptr
  1524. else
  1525. {$endif i8086}
  1526. consttype:=aitconst_ptr;
  1527. sym:=nil;
  1528. endsym:=nil;
  1529. symofs:=0;
  1530. value:=_value;
  1531. end;
  1532. constructor tai_const.ppuload(t:taitype;ppufile:tcompilerppufile);
  1533. begin
  1534. inherited ppuload(t,ppufile);
  1535. consttype:=taiconst_type(ppufile.getbyte);
  1536. sym:=ppufile.getasmsymbol;
  1537. endsym:=ppufile.getasmsymbol;
  1538. value:=ppufile.getint64;
  1539. end;
  1540. procedure tai_const.ppuwrite(ppufile:tcompilerppufile);
  1541. begin
  1542. inherited ppuwrite(ppufile);
  1543. ppufile.putbyte(byte(consttype));
  1544. ppufile.putasmsymbol(sym);
  1545. ppufile.putasmsymbol(endsym);
  1546. ppufile.putint64(value);
  1547. end;
  1548. procedure tai_const.derefimpl;
  1549. begin
  1550. end;
  1551. function tai_const.getcopy:tlinkedlistitem;
  1552. begin
  1553. getcopy:=inherited getcopy;
  1554. { we need to increase the reference number }
  1555. if assigned(sym) then
  1556. sym.increfs;
  1557. if assigned(endsym) then
  1558. endsym.increfs;
  1559. end;
  1560. function tai_const.size:longint;
  1561. begin
  1562. case consttype of
  1563. aitconst_8bit :
  1564. result:=1;
  1565. aitconst_16bit,aitconst_16bit_unaligned :
  1566. result:=2;
  1567. aitconst_32bit,aitconst_darwin_dwarf_delta32,
  1568. aitconst_32bit_unaligned:
  1569. result:=4;
  1570. aitconst_64bit,aitconst_darwin_dwarf_delta64,
  1571. aitconst_64bit_unaligned:
  1572. result:=8;
  1573. aitconst_secrel32_symbol,
  1574. aitconst_rva_symbol :
  1575. if target_info.system=system_x86_64_win64 then
  1576. result:=sizeof(longint)
  1577. else
  1578. result:=sizeof(pint);
  1579. aitconst_uleb128bit :
  1580. result:=LengthUleb128(qword(value));
  1581. aitconst_sleb128bit :
  1582. result:=LengthSleb128(value);
  1583. aitconst_half16bit:
  1584. result:=2;
  1585. aitconst_gotoff_symbol:
  1586. result:=4;
  1587. else
  1588. internalerror(200603253);
  1589. end;
  1590. end;
  1591. {****************************************************************************
  1592. TAI_real_32bit
  1593. ****************************************************************************}
  1594. constructor tai_real_32bit.Create(_value : ts32real);
  1595. begin
  1596. inherited Create;
  1597. typ:=ait_real_32bit;
  1598. value:=_value;
  1599. end;
  1600. constructor tai_real_32bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1601. begin
  1602. inherited ppuload(t,ppufile);
  1603. value:=ppufile.getreal;
  1604. end;
  1605. procedure tai_real_32bit.ppuwrite(ppufile:tcompilerppufile);
  1606. begin
  1607. inherited ppuwrite(ppufile);
  1608. ppufile.putreal(value);
  1609. end;
  1610. {****************************************************************************
  1611. TAI_real_64bit
  1612. ****************************************************************************}
  1613. constructor tai_real_64bit.Create(_value : ts64real);
  1614. begin
  1615. inherited Create;
  1616. typ:=ait_real_64bit;
  1617. value:=_value;
  1618. end;
  1619. {$ifdef ARM}
  1620. constructor tai_real_64bit.Create_hiloswapped(_value : ts64real);
  1621. begin
  1622. inherited Create;
  1623. typ:=ait_real_64bit;
  1624. value:=_value;
  1625. formatoptions:=fo_hiloswapped;
  1626. end;
  1627. {$endif ARM}
  1628. constructor tai_real_64bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1629. begin
  1630. inherited ppuload(t,ppufile);
  1631. value:=ppufile.getreal;
  1632. {$ifdef ARM}
  1633. formatoptions:=tformatoptions(ppufile.getbyte);
  1634. {$endif ARM}
  1635. end;
  1636. procedure tai_real_64bit.ppuwrite(ppufile:tcompilerppufile);
  1637. begin
  1638. inherited ppuwrite(ppufile);
  1639. ppufile.putreal(value);
  1640. {$ifdef ARM}
  1641. ppufile.putbyte(byte(formatoptions));
  1642. {$endif ARM}
  1643. end;
  1644. {****************************************************************************
  1645. TAI_real_80bit
  1646. ****************************************************************************}
  1647. constructor tai_real_80bit.Create(_value : ts80real; _savesize: byte);
  1648. begin
  1649. inherited Create;
  1650. typ:=ait_real_80bit;
  1651. value:=_value;
  1652. savesize:=_savesize;
  1653. end;
  1654. constructor tai_real_80bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1655. begin
  1656. inherited ppuload(t,ppufile);
  1657. value:=ppufile.getreal;
  1658. savesize:=ppufile.getbyte;
  1659. end;
  1660. procedure tai_real_80bit.ppuwrite(ppufile:tcompilerppufile);
  1661. begin
  1662. inherited ppuwrite(ppufile);
  1663. ppufile.putreal(value);
  1664. ppufile.putbyte(savesize);
  1665. end;
  1666. {****************************************************************************
  1667. TAI_real_80bit
  1668. ****************************************************************************}
  1669. constructor tai_real_128bit.Create(_value : ts128real);
  1670. begin
  1671. inherited Create;
  1672. typ:=ait_real_128bit;
  1673. value:=_value;
  1674. end;
  1675. constructor tai_real_128bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1676. begin
  1677. inherited ppuload(t,ppufile);
  1678. value:=ppufile.getreal;
  1679. end;
  1680. procedure tai_real_128bit.ppuwrite(ppufile:tcompilerppufile);
  1681. begin
  1682. inherited ppuwrite(ppufile);
  1683. ppufile.putreal(value);
  1684. end;
  1685. {****************************************************************************
  1686. Tai_comp_64bit
  1687. ****************************************************************************}
  1688. constructor tai_comp_64bit.Create(_value : ts64comp);
  1689. begin
  1690. inherited Create;
  1691. typ:=ait_comp_64bit;
  1692. value:=_value;
  1693. end;
  1694. constructor tai_comp_64bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1695. begin
  1696. inherited ppuload(t,ppufile);
  1697. ppufile.putdata(value,sizeof(value));
  1698. end;
  1699. procedure tai_comp_64bit.ppuwrite(ppufile:tcompilerppufile);
  1700. begin
  1701. inherited ppuwrite(ppufile);
  1702. ppufile.getdata(value,sizeof(value));
  1703. end;
  1704. {****************************************************************************
  1705. TAI_STRING
  1706. ****************************************************************************}
  1707. constructor tai_string.Create(const _str : string);
  1708. begin
  1709. inherited Create;
  1710. typ:=ait_string;
  1711. len:=length(_str);
  1712. getmem(str,len+1);
  1713. move(_str[1],str^,len);
  1714. str[len]:=#0;
  1715. end;
  1716. constructor tai_string.Create_pchar(_str : pchar;length : longint);
  1717. begin
  1718. inherited Create;
  1719. typ:=ait_string;
  1720. str:=_str;
  1721. len:=length;
  1722. end;
  1723. destructor tai_string.destroy;
  1724. begin
  1725. if str<>nil then
  1726. freemem(str);
  1727. inherited Destroy;
  1728. end;
  1729. constructor tai_string.ppuload(t:taitype;ppufile:tcompilerppufile);
  1730. begin
  1731. inherited ppuload(t,ppufile);
  1732. len:=ppufile.getlongint;
  1733. getmem(str,len);
  1734. ppufile.getdata(str^,len);
  1735. end;
  1736. procedure tai_string.ppuwrite(ppufile:tcompilerppufile);
  1737. begin
  1738. inherited ppuwrite(ppufile);
  1739. ppufile.putlongint(len);
  1740. ppufile.putdata(str^,len);
  1741. end;
  1742. function tai_string.getcopy : tlinkedlistitem;
  1743. var
  1744. p : tlinkedlistitem;
  1745. begin
  1746. p:=inherited getcopy;
  1747. getmem(tai_string(p).str,len);
  1748. move(str^,tai_string(p).str^,len);
  1749. getcopy:=p;
  1750. end;
  1751. {****************************************************************************
  1752. TAI_LABEL
  1753. ****************************************************************************}
  1754. constructor tai_label.Create(_labsym : tasmlabel);
  1755. begin
  1756. inherited Create;
  1757. typ:=ait_label;
  1758. labsym:=_labsym;
  1759. labsym.is_set:=true;
  1760. end;
  1761. constructor tai_label.ppuload(t:taitype;ppufile:tcompilerppufile);
  1762. begin
  1763. inherited ppuload(t,ppufile);
  1764. labsym:=tasmlabel(ppufile.getasmsymbol);
  1765. ppufile.getbyte; { was is_global flag, now unused }
  1766. end;
  1767. procedure tai_label.ppuwrite(ppufile:tcompilerppufile);
  1768. begin
  1769. inherited ppuwrite(ppufile);
  1770. ppufile.putasmsymbol(labsym);
  1771. ppufile.putbyte(0); { was is_global flag, now unused }
  1772. end;
  1773. procedure tai_label.derefimpl;
  1774. begin
  1775. labsym.is_set:=true;
  1776. end;
  1777. {****************************************************************************
  1778. tai_comment comment to be inserted in the assembler file
  1779. ****************************************************************************}
  1780. constructor tai_comment.Create(_str : pchar);
  1781. begin
  1782. inherited Create;
  1783. typ:=ait_comment;
  1784. str:=_str;
  1785. end;
  1786. destructor tai_comment.destroy;
  1787. begin
  1788. freemem(str);
  1789. inherited Destroy;
  1790. end;
  1791. constructor tai_comment.ppuload(t:taitype;ppufile:tcompilerppufile);
  1792. var
  1793. len : longint;
  1794. begin
  1795. inherited ppuload(t,ppufile);
  1796. len:=ppufile.getlongint;
  1797. getmem(str,len+1);
  1798. ppufile.getdata(str^,len);
  1799. str[len]:=#0;
  1800. end;
  1801. procedure tai_comment.ppuwrite(ppufile:tcompilerppufile);
  1802. var
  1803. len : longint;
  1804. begin
  1805. inherited ppuwrite(ppufile);
  1806. len:=strlen(str);
  1807. ppufile.putlongint(len);
  1808. ppufile.putdata(str^,len);
  1809. end;
  1810. function tai_comment.getcopy : tlinkedlistitem;
  1811. var
  1812. p : tlinkedlistitem;
  1813. begin
  1814. p:=inherited getcopy;
  1815. getmem(tai_comment(p).str,strlen(str)+1);
  1816. move(str^,tai_comment(p).str^,strlen(str)+1);
  1817. getcopy:=p;
  1818. end;
  1819. {****************************************************************************
  1820. TAI_STABS
  1821. ****************************************************************************}
  1822. constructor tai_stab.create(_stabtype:TStabType;_str : pchar);
  1823. begin
  1824. inherited create;
  1825. typ:=ait_stab;
  1826. str:=_str;
  1827. stabtype:=_stabtype;
  1828. end;
  1829. constructor tai_stab.create_str(_stabtype:TStabType;const s:string);
  1830. begin
  1831. self.create(_stabtype,strpnew(s));
  1832. end;
  1833. constructor tai_stab.create_ansistr(_stabtype:TStabType;const s:ansistring);
  1834. begin
  1835. inherited create;
  1836. typ:=ait_stab;
  1837. stabtype:=_stabtype;
  1838. getmem(str,length(s)+1);
  1839. if length(s)>0 then
  1840. move(s[1],str^,length(s)+1)
  1841. else
  1842. str^:=#0;
  1843. end;
  1844. destructor tai_stab.destroy;
  1845. begin
  1846. freemem(str);
  1847. inherited destroy;
  1848. end;
  1849. {****************************************************************************
  1850. TAI_FORCE_LINE
  1851. ****************************************************************************}
  1852. constructor tai_force_line.create;
  1853. begin
  1854. inherited create;
  1855. typ:=ait_force_line;
  1856. end;
  1857. {****************************************************************************
  1858. TAI_FUNCTION_NAME
  1859. ****************************************************************************}
  1860. constructor tai_function_name.create(const s:string);
  1861. begin
  1862. inherited create;
  1863. typ:=ait_function_name;
  1864. funcname:=stringdup(s);
  1865. end;
  1866. destructor tai_function_name.destroy;
  1867. begin
  1868. stringdispose(funcname);
  1869. inherited destroy;
  1870. end;
  1871. {****************************************************************************
  1872. TAI_CUTOBJECT
  1873. ****************************************************************************}
  1874. constructor tai_cutobject.Create;
  1875. begin
  1876. inherited Create;
  1877. typ:=ait_cutobject;
  1878. place:=cut_normal;
  1879. end;
  1880. constructor tai_cutobject.Create_begin;
  1881. begin
  1882. inherited Create;
  1883. typ:=ait_cutobject;
  1884. place:=cut_begin;
  1885. end;
  1886. constructor tai_cutobject.Create_end;
  1887. begin
  1888. inherited Create;
  1889. typ:=ait_cutobject;
  1890. place:=cut_end;
  1891. end;
  1892. constructor tai_cutobject.ppuload(t:taitype;ppufile:tcompilerppufile);
  1893. begin
  1894. inherited ppuload(t,ppufile);
  1895. place:=TCutPlace(ppufile.getbyte);
  1896. end;
  1897. procedure tai_cutobject.ppuwrite(ppufile:tcompilerppufile);
  1898. begin
  1899. inherited ppuwrite(ppufile);
  1900. ppufile.putbyte(byte(place));
  1901. end;
  1902. {****************************************************************************
  1903. Tai_Marker
  1904. ****************************************************************************}
  1905. constructor Tai_Marker.Create(_Kind: TAsmMarker);
  1906. begin
  1907. Inherited Create;
  1908. typ := ait_marker;
  1909. Kind := _Kind;
  1910. end;
  1911. constructor Tai_Marker.ppuload(t:taitype;ppufile:tcompilerppufile);
  1912. begin
  1913. inherited ppuload(t,ppufile);
  1914. kind:=TAsmMarker(ppufile.getbyte);
  1915. end;
  1916. procedure Tai_Marker.ppuwrite(ppufile:tcompilerppufile);
  1917. begin
  1918. inherited ppuwrite(ppufile);
  1919. ppufile.putbyte(byte(kind));
  1920. end;
  1921. {*****************************************************************************
  1922. tai_tempalloc
  1923. *****************************************************************************}
  1924. constructor tai_tempalloc.alloc(pos,size:longint);
  1925. begin
  1926. inherited Create;
  1927. typ:=ait_tempalloc;
  1928. allocation:=true;
  1929. temppos:=pos;
  1930. tempsize:=size;
  1931. {$ifdef EXTDEBUG}
  1932. problem:=nil;
  1933. {$endif EXTDEBUG}
  1934. end;
  1935. destructor tai_tempalloc.destroy;
  1936. begin
  1937. {$ifdef EXTDEBUG}
  1938. stringdispose(problem);
  1939. {$endif EXTDEBUG}
  1940. inherited destroy;
  1941. end;
  1942. constructor tai_tempalloc.dealloc(pos,size:longint);
  1943. begin
  1944. inherited Create;
  1945. typ:=ait_tempalloc;
  1946. allocation:=false;
  1947. temppos:=pos;
  1948. tempsize:=size;
  1949. {$ifdef EXTDEBUG}
  1950. problem:=nil;
  1951. {$endif EXTDEBUG}
  1952. end;
  1953. {$ifdef EXTDEBUG}
  1954. constructor tai_tempalloc.allocinfo(pos,size:longint;const st:string);
  1955. begin
  1956. inherited Create;
  1957. typ:=ait_tempalloc;
  1958. allocation:=false;
  1959. temppos:=pos;
  1960. tempsize:=size;
  1961. problem:=stringdup(st);
  1962. end;
  1963. {$endif EXTDEBUG}
  1964. constructor tai_tempalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  1965. begin
  1966. inherited ppuload(t,ppufile);
  1967. temppos:=ppufile.getlongint;
  1968. tempsize:=ppufile.getlongint;
  1969. allocation:=boolean(ppufile.getbyte);
  1970. {$ifdef EXTDEBUG}
  1971. problem:=nil;
  1972. {$endif EXTDEBUG}
  1973. end;
  1974. procedure tai_tempalloc.ppuwrite(ppufile:tcompilerppufile);
  1975. begin
  1976. inherited ppuwrite(ppufile);
  1977. ppufile.putlongint(temppos);
  1978. ppufile.putlongint(tempsize);
  1979. ppufile.putbyte(byte(allocation));
  1980. end;
  1981. {*****************************************************************************
  1982. tai_regalloc
  1983. *****************************************************************************}
  1984. constructor tai_regalloc.alloc(r : tregister;ainstr:tai);
  1985. begin
  1986. inherited create;
  1987. typ:=ait_regalloc;
  1988. ratype:=ra_alloc;
  1989. reg:=r;
  1990. { ainstr must be an instruction }
  1991. if assigned(ainstr) and
  1992. (ainstr.typ<>ait_instruction) then
  1993. internalerror(200411011);
  1994. instr:=ainstr;
  1995. end;
  1996. constructor tai_regalloc.dealloc(r : tregister;ainstr:tai);
  1997. begin
  1998. inherited create;
  1999. typ:=ait_regalloc;
  2000. ratype:=ra_dealloc;
  2001. reg:=r;
  2002. { ainstr must be an instruction }
  2003. if assigned(ainstr) and
  2004. (ainstr.typ<>ait_instruction) then
  2005. internalerror(200411012);
  2006. instr:=ainstr;
  2007. end;
  2008. constructor tai_regalloc.sync(r : tregister);
  2009. begin
  2010. inherited create;
  2011. typ:=ait_regalloc;
  2012. ratype:=ra_sync;
  2013. reg:=r;
  2014. end;
  2015. constructor tai_regalloc.resize(r : tregister);
  2016. begin
  2017. inherited create;
  2018. typ:=ait_regalloc;
  2019. ratype:=ra_resize;
  2020. reg:=r;
  2021. end;
  2022. constructor tai_regalloc.markused(r : tregister);
  2023. begin
  2024. inherited create;
  2025. typ:=ait_regalloc;
  2026. ratype:=ra_markused;
  2027. reg:=r;
  2028. end;
  2029. constructor tai_regalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  2030. begin
  2031. inherited ppuload(t,ppufile);
  2032. ppufile.getdata(reg,sizeof(Tregister));
  2033. ratype:=tregalloctype(ppufile.getbyte);
  2034. keep:=boolean(ppufile.getbyte);
  2035. end;
  2036. procedure tai_regalloc.ppuwrite(ppufile:tcompilerppufile);
  2037. begin
  2038. inherited ppuwrite(ppufile);
  2039. ppufile.putdata(reg,sizeof(Tregister));
  2040. ppufile.putbyte(byte(ratype));
  2041. ppufile.putbyte(byte(keep));
  2042. end;
  2043. {*****************************************************************************
  2044. TaiInstruction
  2045. *****************************************************************************}
  2046. constructor tai_cpu_abstract.Create(op : tasmop);
  2047. begin
  2048. inherited create;
  2049. typ:=ait_instruction;
  2050. is_jmp:=false;
  2051. opcode:=op;
  2052. ops:=0;
  2053. end;
  2054. destructor tai_cpu_abstract.Destroy;
  2055. var
  2056. i : integer;
  2057. begin
  2058. for i:=0 to opercnt-1 do
  2059. freeop(i);
  2060. inherited destroy;
  2061. end;
  2062. { ---------------------------------------------------------------------
  2063. Loading of operands.
  2064. ---------------------------------------------------------------------}
  2065. procedure tai_cpu_abstract.allocate_oper(opers:longint);
  2066. begin
  2067. while (opers>opercnt) do
  2068. begin
  2069. new(oper[opercnt]);
  2070. fillchar(oper[opercnt]^,sizeof(toper),0);
  2071. inc(opercnt);
  2072. end;
  2073. end;
  2074. procedure tai_cpu_abstract.loadconst(opidx:longint;l:aint);
  2075. begin
  2076. allocate_oper(opidx+1);
  2077. with oper[opidx]^ do
  2078. begin
  2079. if typ<>top_const then
  2080. clearop(opidx);
  2081. val:=l;
  2082. typ:=top_const;
  2083. end;
  2084. end;
  2085. procedure tai_cpu_abstract.loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  2086. var
  2087. r : treference;
  2088. begin
  2089. reference_reset_symbol(r,s,sofs,1);
  2090. r.refaddr:=addr_full;
  2091. loadref(opidx,r);
  2092. end;
  2093. procedure tai_cpu_abstract.loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset,forceref:boolean);
  2094. begin
  2095. if not assigned(s) then
  2096. internalerror(200204251);
  2097. allocate_oper(opidx+1);
  2098. with oper[opidx]^ do
  2099. begin
  2100. if typ<>top_local then
  2101. begin
  2102. clearop(opidx);
  2103. new(localoper);
  2104. end;
  2105. with oper[opidx]^.localoper^ do
  2106. begin
  2107. localsym:=s;
  2108. localsymofs:=sofs;
  2109. localindexreg:=indexreg;
  2110. localscale:=scale;
  2111. localgetoffset:=getoffset;
  2112. localforceref:=forceref;
  2113. end;
  2114. typ:=top_local;
  2115. end;
  2116. end;
  2117. procedure tai_cpu_abstract.loadref(opidx:longint;const r:treference);
  2118. begin
  2119. allocate_oper(opidx+1);
  2120. with oper[opidx]^ do
  2121. begin
  2122. if typ<>top_ref then
  2123. begin
  2124. clearop(opidx);
  2125. new(ref);
  2126. end;
  2127. ref^:=r;
  2128. {$ifdef x86}
  2129. { We allow this exception for x86, since overloading this would be
  2130. too much of a a speed penalty}
  2131. if (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  2132. segprefix:=ref^.segment;
  2133. {$endif}
  2134. if (cs_create_pic in current_settings.moduleswitches) and
  2135. assigned(r.symbol) and
  2136. not assigned(r.relsymbol) and
  2137. (r.refaddr=addr_no)
  2138. {$ifdef ARM}
  2139. and not(r.base=NR_R15)
  2140. {$endif ARM}
  2141. {$ifdef aarch64}
  2142. and not(r.refaddr in [addr_full,addr_gotpageoffset,addr_gotpage])
  2143. {$endif aarch64}
  2144. then
  2145. internalerror(200502052);
  2146. typ:=top_ref;
  2147. if assigned(add_reg_instruction_hook) then
  2148. begin
  2149. add_reg_instruction_hook(self,ref^.base);
  2150. add_reg_instruction_hook(self,ref^.index);
  2151. end;
  2152. { mark symbol as used }
  2153. if assigned(ref^.symbol) then
  2154. ref^.symbol.increfs;
  2155. if assigned(ref^.relsymbol) then
  2156. ref^.relsymbol.increfs;
  2157. end;
  2158. end;
  2159. procedure tai_cpu_abstract.loadreg(opidx:longint;r:tregister);
  2160. begin
  2161. allocate_oper(opidx+1);
  2162. with oper[opidx]^ do
  2163. begin
  2164. if typ<>top_reg then
  2165. clearop(opidx);
  2166. reg:=r;
  2167. typ:=top_reg;
  2168. end;
  2169. if assigned(add_reg_instruction_hook) then
  2170. add_reg_instruction_hook(self,r);
  2171. {$ifdef ARM}
  2172. { R15 is the PC on the ARM thus moves to R15 are jumps.
  2173. Due to speed considerations we don't use a virtual overridden method here.
  2174. Because the pc/r15 isn't handled by the reg. allocator this should never cause
  2175. problems with iregs getting r15.
  2176. }
  2177. is_jmp:=(opcode=A_MOV) and (opidx=0) and (r=NR_R15);
  2178. {$endif ARM}
  2179. end;
  2180. procedure tai_cpu_abstract.loadoper(opidx:longint;o:toper);
  2181. begin
  2182. allocate_oper(opidx+1);
  2183. clearop(opidx);
  2184. oper[opidx]^:=o;
  2185. { copy also the reference }
  2186. with oper[opidx]^ do
  2187. begin
  2188. case typ of
  2189. top_reg:
  2190. begin
  2191. if assigned(add_reg_instruction_hook) then
  2192. add_reg_instruction_hook(self,reg);
  2193. end;
  2194. top_ref:
  2195. begin
  2196. new(ref);
  2197. ref^:=o.ref^;
  2198. {$ifdef x86}
  2199. if (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  2200. segprefix:=ref^.segment;
  2201. {$endif x86}
  2202. if assigned(add_reg_instruction_hook) then
  2203. begin
  2204. add_reg_instruction_hook(self,ref^.base);
  2205. add_reg_instruction_hook(self,ref^.index);
  2206. end;
  2207. end;
  2208. {$ifdef ARM}
  2209. top_shifterop:
  2210. begin
  2211. new(shifterop);
  2212. shifterop^:=o.shifterop^;
  2213. if assigned(add_reg_instruction_hook) then
  2214. add_reg_instruction_hook(self,shifterop^.rs);
  2215. end;
  2216. {$endif ARM}
  2217. end;
  2218. end;
  2219. end;
  2220. procedure tai_cpu_abstract.clearop(opidx:longint);
  2221. begin
  2222. with oper[opidx]^ do
  2223. begin
  2224. case typ of
  2225. top_ref:
  2226. dispose(ref);
  2227. top_local:
  2228. dispose(localoper);
  2229. {$ifdef ARM}
  2230. top_shifterop:
  2231. dispose(shifterop);
  2232. top_regset:
  2233. dispose(regset);
  2234. {$endif ARM}
  2235. {$ifdef m68k}
  2236. top_regset:
  2237. begin
  2238. dispose(dataregset);
  2239. dispose(addrregset);
  2240. dispose(fpuregset);
  2241. end;
  2242. {$endif m68k}
  2243. {$ifdef jvm}
  2244. top_string:
  2245. freemem(pcval);
  2246. top_wstring:
  2247. donewidestring(pwstrval);
  2248. {$endif jvm}
  2249. end;
  2250. typ:=top_none;
  2251. end;
  2252. end;
  2253. procedure tai_cpu_abstract.freeop(opidx:longint);
  2254. begin
  2255. clearop(opidx);
  2256. dispose(oper[opidx]);
  2257. end;
  2258. { ---------------------------------------------------------------------
  2259. Miscellaneous methods.
  2260. ---------------------------------------------------------------------}
  2261. procedure tai_cpu_abstract.SetCondition(const c:TAsmCond);
  2262. begin
  2263. condition:=c;
  2264. end;
  2265. Function tai_cpu_abstract.getcopy:TLinkedListItem;
  2266. var
  2267. i : longint;
  2268. p : tai_cpu_abstract;
  2269. begin
  2270. p:=tai_cpu_abstract(inherited getcopy);
  2271. { make a copy of the references }
  2272. p.opercnt:=0;
  2273. p.allocate_oper(ops);
  2274. for i:=0 to ops-1 do
  2275. begin
  2276. p.oper[i]^:=oper[i]^;
  2277. case oper[i]^.typ of
  2278. top_local :
  2279. begin
  2280. new(p.oper[i]^.localoper);
  2281. p.oper[i]^.localoper^:=oper[i]^.localoper^;
  2282. end;
  2283. top_ref :
  2284. begin
  2285. new(p.oper[i]^.ref);
  2286. p.oper[i]^.ref^:=oper[i]^.ref^;
  2287. end;
  2288. {$ifdef ARM}
  2289. top_shifterop:
  2290. begin
  2291. new(p.oper[i]^.shifterop);
  2292. p.oper[i]^.shifterop^:=oper[i]^.shifterop^;
  2293. end;
  2294. {$endif ARM}
  2295. end;
  2296. end;
  2297. getcopy:=p;
  2298. end;
  2299. function tai_cpu_abstract.is_same_reg_move(regtype: Tregistertype):boolean;
  2300. begin
  2301. { When the generic RA is used this needs to be overridden, we don't use
  2302. virtual;abstract; to prevent a lot of warnings of unimplemented abstract methods
  2303. when tai_cpu is created (PFV) }
  2304. internalerror(2004040901);
  2305. result:=false;
  2306. end;
  2307. function tai_cpu_abstract.spilling_get_operation_type(opnr: longint): topertype;
  2308. begin
  2309. internalerror(2004040902);
  2310. result:=operand_readwrite;
  2311. end;
  2312. function tai_cpu_abstract.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
  2313. begin
  2314. result := operand_read;
  2315. end;
  2316. constructor tai_cpu_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  2317. var
  2318. i : integer;
  2319. begin
  2320. inherited ppuload(t,ppufile);
  2321. { hopefully, we don't get problems with big/litte endian here when cross compiling :/ }
  2322. ppufile.getdata(condition,sizeof(tasmcond));
  2323. allocate_oper(ppufile.getbyte);
  2324. for i:=0 to ops-1 do
  2325. ppuloadoper(ppufile,oper[i]^);
  2326. opcode:=tasmop(ppufile.getword);
  2327. {$ifdef x86}
  2328. ppufile.getdata(segprefix,sizeof(Tregister));
  2329. {$endif x86}
  2330. is_jmp:=boolean(ppufile.getbyte);
  2331. end;
  2332. procedure tai_cpu_abstract.ppuwrite(ppufile:tcompilerppufile);
  2333. var
  2334. i : integer;
  2335. begin
  2336. inherited ppuwrite(ppufile);
  2337. ppufile.putdata(condition,sizeof(tasmcond));
  2338. ppufile.putbyte(ops);
  2339. for i:=0 to ops-1 do
  2340. ppuwriteoper(ppufile,oper[i]^);
  2341. ppufile.putword(word(opcode));
  2342. {$ifdef x86}
  2343. ppufile.putdata(segprefix,sizeof(Tregister));
  2344. {$endif x86}
  2345. ppufile.putbyte(byte(is_jmp));
  2346. end;
  2347. procedure tai_cpu_abstract.buildderefimpl;
  2348. var
  2349. i : integer;
  2350. begin
  2351. for i:=0 to ops-1 do
  2352. ppubuildderefimploper(oper[i]^);
  2353. end;
  2354. procedure tai_cpu_abstract.derefimpl;
  2355. var
  2356. i : integer;
  2357. begin
  2358. for i:=0 to ops-1 do
  2359. ppuderefoper(oper[i]^);
  2360. end;
  2361. procedure tai_cpu_abstract.resetpass1;
  2362. begin
  2363. end;
  2364. procedure tai_cpu_abstract.resetpass2;
  2365. begin
  2366. end;
  2367. function tai_cpu_abstract.Pass1(objdata:TObjData):longint;
  2368. begin
  2369. result:=0;
  2370. end;
  2371. procedure tai_cpu_abstract.Pass2(objdata:TObjData);
  2372. begin
  2373. end;
  2374. procedure tai_cpu_abstract.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  2375. begin
  2376. o.typ:=toptype(ppufile.getbyte);
  2377. o.ot:=ppufile.getlongint;
  2378. case o.typ of
  2379. top_reg :
  2380. ppufile.getdata(o.reg,sizeof(Tregister));
  2381. top_ref :
  2382. begin
  2383. new(o.ref);
  2384. {$ifdef x86}
  2385. ppufile.getdata(o.ref^.segment,sizeof(Tregister));
  2386. {$endif x86}
  2387. ppufile.getdata(o.ref^.base,sizeof(Tregister));
  2388. ppufile.getdata(o.ref^.index,sizeof(Tregister));
  2389. ppufile.getdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));
  2390. o.ref^.scalefactor:=ppufile.getbyte;
  2391. o.ref^.offset:=ppufile.getaint;
  2392. o.ref^.symbol:=ppufile.getasmsymbol;
  2393. o.ref^.relsymbol:=ppufile.getasmsymbol;
  2394. end;
  2395. top_const :
  2396. o.val:=ppufile.getaint;
  2397. top_local :
  2398. begin
  2399. new(o.localoper);
  2400. with o.localoper^ do
  2401. begin
  2402. ppufile.getderef(localsymderef);
  2403. localsymofs:=ppufile.getaint;
  2404. localindexreg:=tregister(ppufile.getlongint);
  2405. localscale:=ppufile.getbyte;
  2406. localgetoffset:=(ppufile.getbyte<>0);
  2407. end;
  2408. end;
  2409. else
  2410. internalerror(2007010210);
  2411. end;
  2412. end;
  2413. procedure tai_cpu_abstract.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  2414. begin
  2415. ppufile.putbyte(byte(o.typ));
  2416. ppufile.putlongint(o.ot);
  2417. case o.typ of
  2418. top_reg :
  2419. ppufile.putdata(o.reg,sizeof(Tregister));
  2420. top_ref :
  2421. begin
  2422. {$ifdef x86}
  2423. ppufile.putdata(o.ref^.segment,sizeof(Tregister));
  2424. {$endif x86}
  2425. ppufile.putdata(o.ref^.base,sizeof(Tregister));
  2426. ppufile.putdata(o.ref^.index,sizeof(Tregister));
  2427. ppufile.putdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));
  2428. ppufile.putbyte(o.ref^.scalefactor);
  2429. ppufile.putaint(o.ref^.offset);
  2430. ppufile.putasmsymbol(o.ref^.symbol);
  2431. ppufile.putasmsymbol(o.ref^.relsymbol);
  2432. end;
  2433. top_const :
  2434. ppufile.putaint(o.val);
  2435. top_local :
  2436. begin
  2437. with o.localoper^ do
  2438. begin
  2439. ppufile.putderef(localsymderef);
  2440. ppufile.putaint(localsymofs);
  2441. ppufile.putlongint(longint(localindexreg));
  2442. ppufile.putbyte(localscale);
  2443. ppufile.putbyte(byte(localgetoffset));
  2444. end;
  2445. end;
  2446. else
  2447. internalerror(2007010211);
  2448. end;
  2449. end;
  2450. {****************************************************************************
  2451. tai_align_abstract
  2452. ****************************************************************************}
  2453. constructor tai_align_abstract.Create(b: byte);
  2454. begin
  2455. inherited Create;
  2456. typ:=ait_align;
  2457. {$ifdef EXTDEBUG}
  2458. if upper(classname)='TAI_ALIGN_ABSTRACT' then
  2459. internalerror(200709191);
  2460. {$endif EXTDEBUG}
  2461. if b in [1,2,4,8,16,32] then
  2462. aligntype := b
  2463. else
  2464. aligntype := 1;
  2465. fillsize:=0;
  2466. fillop:=0;
  2467. use_op:=false;
  2468. end;
  2469. constructor tai_align_abstract.Create_op(b: byte; _op: byte);
  2470. begin
  2471. inherited Create;
  2472. typ:=ait_align;
  2473. if b in [1,2,4,8,16,32] then
  2474. aligntype := b
  2475. else
  2476. aligntype := 1;
  2477. fillsize:=0;
  2478. fillop:=_op;
  2479. use_op:=true;
  2480. end;
  2481. constructor tai_align_abstract.Create_zeros(b: byte);
  2482. begin
  2483. inherited Create;
  2484. typ:=ait_align;
  2485. if b in [1,2,4,8,16,32] then
  2486. aligntype := b
  2487. else
  2488. aligntype := 1;
  2489. use_op:=true;
  2490. fillsize:=0;
  2491. fillop:=0;
  2492. end;
  2493. function tai_align_abstract.calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;
  2494. begin
  2495. if fillsize>sizeof(buf) then
  2496. internalerror(200404293);
  2497. fillchar(buf,high(buf),fillop);
  2498. calculatefillbuf:=pchar(@buf);
  2499. end;
  2500. constructor tai_align_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  2501. begin
  2502. inherited ppuload(t,ppufile);
  2503. aligntype:=ppufile.getbyte;
  2504. fillsize:=0;
  2505. fillop:=ppufile.getbyte;
  2506. use_op:=boolean(ppufile.getbyte);
  2507. end;
  2508. procedure tai_align_abstract.ppuwrite(ppufile:tcompilerppufile);
  2509. begin
  2510. inherited ppuwrite(ppufile);
  2511. ppufile.putbyte(aligntype);
  2512. ppufile.putbyte(fillop);
  2513. ppufile.putbyte(byte(use_op));
  2514. end;
  2515. {****************************************************************************
  2516. tai_seh_directive
  2517. ****************************************************************************}
  2518. const
  2519. datatypemap: array[TAsmSehDirective] of TSehDirectiveDatatype=(
  2520. sd_string, { proc }
  2521. sd_none, { endproc }
  2522. sd_none, { endprologue }
  2523. sd_string, { handler }
  2524. sd_none, { handlerdata }
  2525. sd_none,sd_none,sd_none, { eh, 32, no32 }
  2526. sd_regoffset, { setframe }
  2527. sd_offset, { stackalloc }
  2528. sd_reg, { pushreg }
  2529. sd_regoffset, { savereg }
  2530. sd_regoffset, { savexmm }
  2531. sd_none { pushframe }
  2532. );
  2533. constructor tai_seh_directive.create(_kind:TAsmSehDirective);
  2534. begin
  2535. inherited Create;
  2536. typ:=ait_seh_directive;
  2537. kind:=_kind;
  2538. data.typ:=datatypemap[_kind];
  2539. end;
  2540. constructor tai_seh_directive.create_name(_kind:TAsmSehDirective;const _name:string);
  2541. begin
  2542. create(_kind);
  2543. data.name:=stringdup(_name);
  2544. end;
  2545. constructor tai_seh_directive.create_reg(_kind:TAsmSehDirective;r:TRegister);
  2546. begin
  2547. create(_kind);
  2548. data.reg:=r;
  2549. end;
  2550. constructor tai_seh_directive.create_offset(_kind:TAsmSehDirective;ofs:dword);
  2551. begin
  2552. create(_kind);
  2553. data.offset:=ofs;
  2554. end;
  2555. constructor tai_seh_directive.create_reg_offset(_kind:TAsmSehDirective;
  2556. r:TRegister;ofs:dword);
  2557. begin
  2558. create(_kind);
  2559. data.offset:=ofs;
  2560. data.reg:=r;
  2561. end;
  2562. constructor tai_seh_directive.ppuload(t:taitype;ppufile:tcompilerppufile);
  2563. begin
  2564. inherited ppuload(t, ppufile);
  2565. kind:=TAsmSehDirective(ppufile.getbyte);
  2566. data.typ:=datatypemap[kind];
  2567. case data.typ of
  2568. sd_none: ;
  2569. sd_string:
  2570. begin
  2571. data.name:=stringdup(ppufile.getstring);
  2572. data.flags:=ppufile.getbyte;
  2573. end;
  2574. sd_reg,sd_offset,sd_regoffset:
  2575. begin
  2576. ppufile.getdata(data.reg,sizeof(TRegister));
  2577. data.offset:=ppufile.getdword;
  2578. end;
  2579. else
  2580. InternalError(2011091201);
  2581. end;
  2582. end;
  2583. destructor tai_seh_directive.destroy;
  2584. begin
  2585. if data.typ=sd_string then
  2586. stringdispose(data.name);
  2587. inherited destroy;
  2588. end;
  2589. procedure tai_seh_directive.ppuwrite(ppufile:tcompilerppufile);
  2590. begin
  2591. inherited ppuwrite(ppufile);
  2592. ppufile.putbyte(ord(kind));
  2593. case data.typ of
  2594. sd_none: ;
  2595. sd_string:
  2596. begin
  2597. ppufile.putstring(data.name^);
  2598. ppufile.putbyte(data.flags);
  2599. end;
  2600. sd_reg,sd_offset,sd_regoffset:
  2601. begin
  2602. ppufile.putdata(data.reg,sizeof(TRegister));
  2603. ppufile.putdword(data.offset);
  2604. end;
  2605. else
  2606. InternalError(2011091202);
  2607. end;
  2608. end;
  2609. procedure tai_seh_directive.generate_code(objdata:TObjData);
  2610. begin
  2611. end;
  2612. {$ifdef JVM}
  2613. {****************************************************************************
  2614. tai_jvar
  2615. ****************************************************************************}
  2616. constructor tai_jvar.Create(_stackslot: longint; const _desc: shortstring; _startlab, _stoplab: TAsmSymbol);
  2617. begin
  2618. Inherited create;
  2619. typ:=ait_jvar;
  2620. stackslot:=_stackslot;
  2621. desc:=stringdup(_desc);
  2622. startlab:=_startlab;
  2623. stoplab:=_stoplab;
  2624. end;
  2625. constructor tai_jvar.ppuload(t: taitype; ppufile: tcompilerppufile);
  2626. begin
  2627. inherited ppuload(t, ppufile);
  2628. stackslot:=ppufile.getlongint;
  2629. desc:=stringdup(ppufile.getstring);
  2630. startlab:=ppufile.getasmsymbol;
  2631. stoplab:=ppufile.getasmsymbol;
  2632. end;
  2633. procedure tai_jvar.ppuwrite(ppufile: tcompilerppufile);
  2634. begin
  2635. inherited ppuwrite(ppufile);
  2636. ppufile.putlongint(stackslot);
  2637. ppufile.putstring(desc^);
  2638. ppufile.putasmsymbol(startlab);
  2639. ppufile.putasmsymbol(stoplab);
  2640. end;
  2641. destructor tai_jvar.destroy;
  2642. begin
  2643. stringdispose(desc);
  2644. inherited destroy;
  2645. end;
  2646. {****************************************************************************
  2647. tai_jcatch
  2648. ****************************************************************************}
  2649. constructor tai_jcatch.Create(const _name: shortstring; _startlab, _stoplab, _handlerlab: TAsmSymbol);
  2650. begin
  2651. Inherited create;
  2652. typ:=ait_jcatch;
  2653. name:=stringdup(_name);
  2654. startlab:=_startlab;
  2655. startlab.increfs;
  2656. stoplab:=_stoplab;
  2657. stoplab.increfs;
  2658. handlerlab:=_handlerlab;
  2659. handlerlab.increfs;
  2660. end;
  2661. destructor tai_jcatch.destroy;
  2662. begin
  2663. stringdispose(name);
  2664. inherited destroy;
  2665. end;
  2666. constructor tai_jcatch.ppuload(t: taitype; ppufile: tcompilerppufile);
  2667. begin
  2668. inherited ppuload(t, ppufile);
  2669. name:=stringdup(ppufile.getstring);
  2670. startlab:=ppufile.getasmsymbol;
  2671. startlab.increfs;
  2672. stoplab:=ppufile.getasmsymbol;
  2673. stoplab.increfs;
  2674. handlerlab:=ppufile.getasmsymbol;
  2675. handlerlab.increfs;
  2676. end;
  2677. procedure tai_jcatch.ppuwrite(ppufile: tcompilerppufile);
  2678. begin
  2679. inherited ppuwrite(ppufile);
  2680. ppufile.putstring(name^);
  2681. ppufile.putasmsymbol(startlab);
  2682. ppufile.putasmsymbol(stoplab);
  2683. ppufile.putasmsymbol(handlerlab);
  2684. end;
  2685. {$endif JVM}
  2686. begin
  2687. {$push}{$warnings off}
  2688. { taitype should fit into a 4 byte set for speed reasons }
  2689. if ord(high(taitype))>31 then
  2690. internalerror(201108181);
  2691. {$pop}
  2692. end.