aasmtai.pas 94 KB

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