aasmtai.pas 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216
  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. newlocation,
  758. newlocationhi : tregister;
  759. varsym : tsym;
  760. constructor create(sym : tsym;loc : tregister);
  761. constructor create64(sym : tsym;loc,lochi : tregister);
  762. {$ifdef cpu64bitalu}
  763. constructor create128(sym : tsym;loc,lochi : tregister);
  764. {$endif cpu64bitalu}
  765. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  766. procedure ppuwrite(ppufile:tcompilerppufile);override;
  767. procedure buildderefimpl;override;
  768. procedure derefimpl;override;
  769. end;
  770. TSehDirectiveDatatype=(sd_none,sd_string,sd_reg,sd_offset,sd_regoffset);
  771. TSehDirectiveData=record
  772. case typ: TSehDirectiveDatatype of
  773. sd_none: ();
  774. sd_string: (name:pshortstring;flags:byte);
  775. sd_reg,sd_offset,sd_regoffset: (reg:TRegister;offset:dword);
  776. end;
  777. tai_seh_directive = class(tai)
  778. kind: TAsmSehDirective;
  779. data: TSehDirectiveData;
  780. constructor create(_kind:TAsmSehDirective);
  781. constructor create_name(_kind:TAsmSehDirective;const _name: string);
  782. constructor create_reg(_kind:TAsmSehDirective;r:TRegister);
  783. constructor create_offset(_kind:TAsmSehDirective;ofs:dword);
  784. constructor create_reg_offset(_kind:TAsmSehDirective;r:TRegister;ofs:dword);
  785. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  786. destructor destroy;override;
  787. procedure ppuwrite(ppufile:tcompilerppufile);override;
  788. procedure generate_code(objdata:TObjData);virtual;
  789. property datatype: TSehDirectiveDatatype read data.typ;
  790. end;
  791. tai_seh_directive_class=class of tai_seh_directive;
  792. {$ifdef JVM}
  793. { JVM variable live range description }
  794. tai_jvar = class(tai)
  795. stackslot: longint;
  796. desc: pshortstring;
  797. startlab,stoplab: tasmsymbol;
  798. constructor Create(_stackslot: longint; const _desc: shortstring; _startlab, _stoplab: TAsmSymbol);
  799. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  800. procedure ppuwrite(ppufile:tcompilerppufile);override;
  801. destructor destroy;override;
  802. end;
  803. tai_jvar_class = class of tai_jvar;
  804. { JVM exception catch description }
  805. tai_jcatch = class(tai)
  806. name: pshortstring;
  807. startlab,stoplab,handlerlab: tasmsymbol;
  808. constructor Create(const _name: shortstring; _startlab, _stoplab, _handlerlab: TAsmSymbol);
  809. destructor destroy;override;
  810. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  811. procedure ppuwrite(ppufile:tcompilerppufile);override;
  812. end;
  813. tai_jcatch_class = class of tai_jcatch;
  814. {$endif JVM}
  815. tai_symbolpair = class(tai)
  816. kind: TSymbolPairKind;
  817. sym,
  818. value: pshortstring;
  819. constructor create(akind: TSymbolPairKind; const asym, avalue: string);
  820. destructor destroy;override;
  821. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  822. procedure ppuwrite(ppufile:tcompilerppufile);override;
  823. end;
  824. var
  825. { array with all class types for tais }
  826. aiclass : taiclassarray;
  827. { target specific tais, possibly overwritten in target specific aasmcpu }
  828. cai_align : tai_align_class = tai_align_abstract;
  829. cai_cpu : tai_cpu_class = tai_cpu_abstract;
  830. cai_seh_directive: tai_seh_directive_class = tai_seh_directive;
  831. { hook to notify uses of registers }
  832. add_reg_instruction_hook : tadd_reg_instruction_proc;
  833. procedure maybe_new_object_file(list:TAsmList);
  834. procedure new_section(list:TAsmList;Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  835. procedure section_symbol_start(list:TAsmList;const Aname:string;Asymtyp:Tasmsymtype;
  836. Aglobal:boolean;Asectype:TAsmSectiontype;Aalign:byte);
  837. procedure section_symbol_end(list:TAsmList;const Aname:string);
  838. function ppuloadai(ppufile:tcompilerppufile):tai;
  839. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  840. implementation
  841. uses
  842. SysUtils,
  843. verbose,
  844. globals;
  845. const
  846. pputaimarker = 254;
  847. {****************************************************************************
  848. Helpers
  849. ****************************************************************************}
  850. procedure maybe_new_object_file(list:TAsmList);
  851. begin
  852. if create_smartlink_library then
  853. list.concat(tai_cutobject.create);
  854. end;
  855. procedure new_section(list:TAsmList;Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  856. begin
  857. list.concat(tai_section.create(Asectype,Aname,Aalign,Asecorder));
  858. list.concat(cai_align.create(Aalign));
  859. end;
  860. procedure section_symbol_start(list:TAsmList;const Aname:string;Asymtyp:Tasmsymtype;
  861. Aglobal:boolean;Asectype:TAsmSectiontype;Aalign:byte);
  862. begin
  863. maybe_new_object_file(list);
  864. new_section(list,Asectype,Aname,Aalign);
  865. if Aglobal or
  866. create_smartlink then
  867. list.concat(tai_symbol.createname_global(Aname,Asymtyp,0))
  868. else
  869. list.concat(tai_symbol.createname(Aname,Asymtyp,0));
  870. end;
  871. procedure section_symbol_end(list:TAsmList;const Aname:string);
  872. begin
  873. list.concat(tai_symbol_end.createname(Aname));
  874. end;
  875. function ppuloadai(ppufile:tcompilerppufile):tai;
  876. var
  877. b : byte;
  878. t : taitype;
  879. begin
  880. { marker }
  881. b:=ppufile.getbyte;
  882. if b<>pputaimarker then
  883. internalerror(200208181);
  884. { load nodetype }
  885. t:=taitype(ppufile.getbyte);
  886. if t<>ait_none then
  887. begin
  888. if t>high(taitype) then
  889. internalerror(200208182);
  890. if not assigned(aiclass[t]) then
  891. internalerror(200208183);
  892. {writeln('taiload: ',taitypestr[t]);}
  893. { generate tai of the correct class }
  894. ppuloadai:=aiclass[t].ppuload(t,ppufile);
  895. end
  896. else
  897. ppuloadai:=nil;
  898. end;
  899. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  900. begin
  901. { marker, read by ppuloadnode }
  902. ppufile.putbyte(pputaimarker);
  903. if assigned(n) then
  904. begin
  905. { type, read by ppuloadnode }
  906. ppufile.putbyte(byte(n.typ));
  907. {writeln('taiwrite: ',taitypestr[n.typ]);}
  908. n.ppuwrite(ppufile);
  909. end
  910. else
  911. ppufile.putbyte(byte(ait_none));
  912. end;
  913. constructor tai_symbolpair.create(akind: TSymbolPairKind; const asym, avalue: string);
  914. begin
  915. inherited create;
  916. kind:=akind;
  917. typ:=ait_symbolpair;
  918. sym:=stringdup(asym);
  919. value:=stringdup(avalue);
  920. end;
  921. destructor tai_symbolpair.destroy;
  922. begin
  923. stringdispose(sym);
  924. stringdispose(value);
  925. inherited destroy;
  926. end;
  927. constructor tai_symbolpair.ppuload(t: taitype; ppufile: tcompilerppufile);
  928. begin
  929. inherited ppuload(t,ppufile);
  930. kind:=TSymbolPairKind(ppufile.getbyte);;
  931. sym:=stringdup(ppufile.getstring);
  932. value:=stringdup(ppufile.getstring);
  933. end;
  934. procedure tai_symbolpair.ppuwrite(ppufile: tcompilerppufile);
  935. begin
  936. inherited ppuwrite(ppufile);
  937. ppufile.putbyte(byte(kind));
  938. ppufile.putstring(sym^);
  939. ppufile.putstring(value^);
  940. end;
  941. constructor tai_varloc.create(sym: tsym; loc: tregister);
  942. begin
  943. inherited Create;
  944. typ:=ait_varloc;
  945. newlocation:=loc;
  946. newlocationhi:=NR_NO;
  947. varsym:=sym;
  948. end;
  949. constructor tai_varloc.create64(sym: tsym; loc, lochi: tregister);
  950. begin
  951. inherited Create;
  952. typ:=ait_varloc;
  953. newlocation:=loc;
  954. newlocationhi:=lochi;
  955. varsym:=sym;
  956. end;
  957. {$ifdef cpu64bitalu}
  958. constructor tai_varloc.create128(sym: tsym; loc, lochi: tregister);
  959. begin
  960. inherited Create;
  961. typ:=ait_varloc;
  962. newlocation:=loc;
  963. newlocationhi:=lochi;
  964. varsym:=sym;
  965. end;
  966. {$endif cpu64bitalu}
  967. constructor tai_varloc.ppuload(t: taitype; ppufile: tcompilerppufile);
  968. begin
  969. inherited ppuload(t, ppufile);
  970. end;
  971. procedure tai_varloc.ppuwrite(ppufile: tcompilerppufile);
  972. begin
  973. inherited ppuwrite(ppufile);
  974. end;
  975. procedure tai_varloc.buildderefimpl;
  976. begin
  977. inherited buildderefimpl;
  978. end;
  979. procedure tai_varloc.derefimpl;
  980. begin
  981. inherited derefimpl;
  982. end;
  983. {****************************************************************************
  984. TAI
  985. ****************************************************************************}
  986. constructor tai.Create;
  987. begin
  988. {$ifndef NOOPT}
  989. optinfo:=nil;
  990. {$endif NOOPT}
  991. end;
  992. constructor tai.ppuload(t:taitype;ppufile:tcompilerppufile);
  993. begin
  994. typ:=t;
  995. {$ifndef NOOPT}
  996. optinfo:=nil;
  997. {$endif}
  998. end;
  999. procedure tai.ppuwrite(ppufile:tcompilerppufile);
  1000. begin
  1001. end;
  1002. procedure tai.buildderefimpl;
  1003. begin
  1004. end;
  1005. procedure tai.derefimpl;
  1006. begin
  1007. end;
  1008. {****************************************************************************
  1009. TAILINEINFO
  1010. ****************************************************************************}
  1011. constructor tailineinfo.create;
  1012. begin
  1013. inherited create;
  1014. fileinfo:=current_filepos;
  1015. end;
  1016. constructor tailineinfo.ppuload(t:taitype;ppufile:tcompilerppufile);
  1017. begin
  1018. inherited ppuload(t,ppufile);
  1019. ppufile.getposinfo(fileinfo);
  1020. end;
  1021. procedure tailineinfo.ppuwrite(ppufile:tcompilerppufile);
  1022. begin
  1023. inherited ppuwrite(ppufile);
  1024. ppufile.putposinfo(fileinfo);
  1025. end;
  1026. {****************************************************************************
  1027. TAI_SIMPLE
  1028. ****************************************************************************}
  1029. constructor tai_simple.create(_typ : taitype);
  1030. begin
  1031. inherited create;
  1032. typ:=_typ;
  1033. end;
  1034. {****************************************************************************
  1035. TAI_SECTION
  1036. ****************************************************************************}
  1037. constructor tai_section.Create(Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  1038. begin
  1039. inherited Create;
  1040. typ:=ait_section;
  1041. sectype:=asectype;
  1042. secalign:=Aalign;
  1043. secorder:=Asecorder;
  1044. name:=stringdup(Aname);
  1045. sec:=nil;
  1046. end;
  1047. constructor tai_section.ppuload(t:taitype;ppufile:tcompilerppufile);
  1048. begin
  1049. inherited ppuload(t,ppufile);
  1050. sectype:=TAsmSectiontype(ppufile.getbyte);
  1051. secalign:=ppufile.getbyte;
  1052. name:=stringdup(ppufile.getstring);
  1053. sec:=nil;
  1054. end;
  1055. destructor tai_section.Destroy;
  1056. begin
  1057. stringdispose(name);
  1058. end;
  1059. procedure tai_section.ppuwrite(ppufile:tcompilerppufile);
  1060. begin
  1061. inherited ppuwrite(ppufile);
  1062. ppufile.putbyte(byte(sectype));
  1063. ppufile.putbyte(secalign);
  1064. ppufile.putstring(name^);
  1065. end;
  1066. {****************************************************************************
  1067. TAI_DATABLOCK
  1068. ****************************************************************************}
  1069. constructor tai_datablock.Create(const _name : string;_size : asizeint);
  1070. begin
  1071. inherited Create;
  1072. typ:=ait_datablock;
  1073. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,AT_DATA);
  1074. { keep things aligned }
  1075. if _size<=0 then
  1076. _size:=sizeof(aint);
  1077. size:=_size;
  1078. is_global:=false;
  1079. end;
  1080. constructor tai_datablock.Create_global(const _name : string;_size : asizeint);
  1081. begin
  1082. inherited Create;
  1083. typ:=ait_datablock;
  1084. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,AT_DATA);
  1085. { keep things aligned }
  1086. if _size<=0 then
  1087. _size:=sizeof(aint);
  1088. size:=_size;
  1089. is_global:=true;
  1090. end;
  1091. constructor tai_datablock.ppuload(t:taitype;ppufile:tcompilerppufile);
  1092. begin
  1093. inherited Create;
  1094. sym:=ppufile.getasmsymbol;
  1095. size:=ppufile.getaint;
  1096. is_global:=boolean(ppufile.getbyte);
  1097. end;
  1098. procedure tai_datablock.ppuwrite(ppufile:tcompilerppufile);
  1099. begin
  1100. inherited ppuwrite(ppufile);
  1101. ppufile.putasmsymbol(sym);
  1102. ppufile.putaint(size);
  1103. ppufile.putbyte(byte(is_global));
  1104. end;
  1105. procedure tai_datablock.derefimpl;
  1106. begin
  1107. end;
  1108. {****************************************************************************
  1109. TAI_SYMBOL
  1110. ****************************************************************************}
  1111. constructor tai_symbol.Create(_sym:tasmsymbol;siz:longint);
  1112. begin
  1113. inherited Create;
  1114. typ:=ait_symbol;
  1115. sym:=_sym;
  1116. size:=siz;
  1117. { don't redefine global/external symbols as local, as code to access
  1118. such symbols is different on some platforms }
  1119. if not(sym.bind in [AB_NONE,AB_LOCAL]) then
  1120. internalerror(2013081601);
  1121. sym.bind:=AB_LOCAL;
  1122. is_global:=false;
  1123. end;
  1124. constructor tai_symbol.Create_global(_sym:tasmsymbol;siz:longint);
  1125. begin
  1126. inherited Create;
  1127. typ:=ait_symbol;
  1128. sym:=_sym;
  1129. size:=siz;
  1130. { don't override PRIVATE_EXTERN with GLOBAL }
  1131. if not(sym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN]) then
  1132. sym.bind:=AB_GLOBAL;
  1133. is_global:=true;
  1134. end;
  1135. constructor tai_symbol.Createname(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  1136. begin
  1137. inherited Create;
  1138. typ:=ait_symbol;
  1139. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,_symtyp);
  1140. size:=siz;
  1141. is_global:=false;
  1142. end;
  1143. constructor tai_symbol.Createname_global(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  1144. begin
  1145. inherited Create;
  1146. typ:=ait_symbol;
  1147. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,_symtyp);
  1148. size:=siz;
  1149. is_global:=true;
  1150. end;
  1151. constructor tai_symbol.createname_global_value(const _name: string;_symtyp: tasmsymtype; siz: longint; val: ptruint);
  1152. begin
  1153. Createname_global(_name,_symtyp,siz);
  1154. value:=val;
  1155. has_value:=true;
  1156. end;
  1157. constructor tai_symbol.ppuload(t:taitype;ppufile:tcompilerppufile);
  1158. begin
  1159. inherited ppuload(t,ppufile);
  1160. sym:=ppufile.getasmsymbol;
  1161. size:=ppufile.getlongint;
  1162. is_global:=boolean(ppufile.getbyte);
  1163. end;
  1164. procedure tai_symbol.ppuwrite(ppufile:tcompilerppufile);
  1165. begin
  1166. inherited ppuwrite(ppufile);
  1167. ppufile.putasmsymbol(sym);
  1168. ppufile.putlongint(size);
  1169. ppufile.putbyte(byte(is_global));
  1170. end;
  1171. procedure tai_symbol.derefimpl;
  1172. begin
  1173. end;
  1174. {****************************************************************************
  1175. TAI_SYMBOL_END
  1176. ****************************************************************************}
  1177. constructor tai_symbol_end.Create(_sym:tasmsymbol);
  1178. begin
  1179. inherited Create;
  1180. typ:=ait_symbol_end;
  1181. sym:=_sym;
  1182. end;
  1183. constructor tai_symbol_end.Createname(const _name : string);
  1184. begin
  1185. inherited Create;
  1186. typ:=ait_symbol_end;
  1187. sym:=current_asmdata.GetAsmSymbol(_name);
  1188. if not assigned(sym) then
  1189. internalerror(2013080301);
  1190. end;
  1191. constructor tai_symbol_end.ppuload(t:taitype;ppufile:tcompilerppufile);
  1192. begin
  1193. inherited ppuload(t,ppufile);
  1194. sym:=ppufile.getasmsymbol;
  1195. end;
  1196. procedure tai_symbol_end.ppuwrite(ppufile:tcompilerppufile);
  1197. begin
  1198. inherited ppuwrite(ppufile);
  1199. ppufile.putasmsymbol(sym);
  1200. end;
  1201. procedure tai_symbol_end.derefimpl;
  1202. begin
  1203. end;
  1204. {****************************************************************************
  1205. TAI_SYMBOL_END
  1206. ****************************************************************************}
  1207. constructor tai_directive.Create(_directive:TAsmDirective;const _name:ansistring);
  1208. begin
  1209. inherited Create;
  1210. typ:=ait_directive;
  1211. name:=_name;
  1212. directive:=_directive;
  1213. end;
  1214. constructor tai_directive.ppuload(t:taitype;ppufile:tcompilerppufile);
  1215. begin
  1216. inherited ppuload(t,ppufile);
  1217. name:=ppufile.getansistring;
  1218. directive:=TAsmDirective(ppufile.getbyte);
  1219. end;
  1220. procedure tai_directive.ppuwrite(ppufile:tcompilerppufile);
  1221. begin
  1222. inherited ppuwrite(ppufile);
  1223. ppufile.putansistring(name);
  1224. ppufile.putbyte(byte(directive));
  1225. end;
  1226. {****************************************************************************
  1227. TAI_CONST
  1228. ****************************************************************************}
  1229. constructor tai_const.Create(_typ:taiconst_type;_value : int64);
  1230. begin
  1231. inherited Create;
  1232. typ:=ait_const;
  1233. consttype:=_typ;
  1234. value:=_value;
  1235. sym:=nil;
  1236. endsym:=nil;
  1237. end;
  1238. constructor tai_const.Create_128bit(_value : int64);
  1239. begin
  1240. inherited Create;
  1241. typ:=ait_const;
  1242. consttype:=aitconst_128bit;
  1243. value:=_value;
  1244. sym:=nil;
  1245. endsym:=nil;
  1246. end;
  1247. constructor tai_const.Create_64bit(_value : int64);
  1248. begin
  1249. inherited Create;
  1250. typ:=ait_const;
  1251. consttype:=aitconst_64bit;
  1252. value:=_value;
  1253. sym:=nil;
  1254. endsym:=nil;
  1255. end;
  1256. constructor tai_const.Create_32bit(_value : longint);
  1257. begin
  1258. inherited Create;
  1259. typ:=ait_const;
  1260. consttype:=aitconst_32bit;
  1261. value:=_value;
  1262. sym:=nil;
  1263. endsym:=nil;
  1264. end;
  1265. constructor tai_const.Create_16bit(_value : word);
  1266. begin
  1267. inherited Create;
  1268. typ:=ait_const;
  1269. consttype:=aitconst_16bit;
  1270. value:=_value;
  1271. sym:=nil;
  1272. endsym:=nil;
  1273. end;
  1274. constructor tai_const.Create_64bit_unaligned(_value : int64);
  1275. begin
  1276. inherited Create;
  1277. typ:=ait_const;
  1278. consttype:=aitconst_64bit_unaligned;
  1279. value:=_value;
  1280. sym:=nil;
  1281. endsym:=nil;
  1282. end;
  1283. constructor tai_const.Create_32bit_unaligned(_value : longint);
  1284. begin
  1285. inherited Create;
  1286. typ:=ait_const;
  1287. consttype:=aitconst_32bit_unaligned;
  1288. value:=_value;
  1289. sym:=nil;
  1290. endsym:=nil;
  1291. end;
  1292. constructor tai_const.Create_16bit_unaligned(_value : word);
  1293. begin
  1294. inherited Create;
  1295. typ:=ait_const;
  1296. consttype:=aitconst_16bit_unaligned;
  1297. value:=_value;
  1298. sym:=nil;
  1299. endsym:=nil;
  1300. end;
  1301. constructor tai_const.Create_8bit(_value : byte);
  1302. begin
  1303. inherited Create;
  1304. typ:=ait_const;
  1305. consttype:=aitconst_8bit;
  1306. value:=_value;
  1307. sym:=nil;
  1308. endsym:=nil;
  1309. end;
  1310. constructor tai_const.Create_char(size: integer; _value: dword);
  1311. begin
  1312. inherited Create;
  1313. typ:=ait_const;
  1314. case size of
  1315. 1:
  1316. begin
  1317. consttype:=aitconst_8bit;
  1318. value:=byte(_value)
  1319. end;
  1320. 2:
  1321. begin
  1322. consttype:=aitconst_16bit;
  1323. value:=word(_value)
  1324. end
  1325. else
  1326. InternalError(2010030701)
  1327. end
  1328. end;
  1329. constructor tai_const.Create_sleb128bit(_value : int64);
  1330. begin
  1331. inherited Create;
  1332. typ:=ait_const;
  1333. consttype:=aitconst_sleb128bit;
  1334. value:=_value;
  1335. sym:=nil;
  1336. endsym:=nil;
  1337. end;
  1338. constructor tai_const.Create_uleb128bit(_value : qword);
  1339. begin
  1340. inherited Create;
  1341. typ:=ait_const;
  1342. consttype:=aitconst_uleb128bit;
  1343. value:=int64(_value);
  1344. sym:=nil;
  1345. endsym:=nil;
  1346. end;
  1347. constructor tai_const.Create_aint(_value : aint);
  1348. begin
  1349. inherited Create;
  1350. typ:=ait_const;
  1351. consttype:=aitconst_aint;
  1352. value:=_value;
  1353. sym:=nil;
  1354. endsym:=nil;
  1355. end;
  1356. constructor tai_const.Create_pint(_value : pint);
  1357. begin
  1358. inherited Create;
  1359. typ:=ait_const;
  1360. consttype:=aitconst_ptr;
  1361. value:=_value;
  1362. sym:=nil;
  1363. endsym:=nil;
  1364. end;
  1365. constructor tai_const.Create_pint_unaligned(_value: pint);
  1366. begin
  1367. inherited Create;
  1368. typ:=ait_const;
  1369. consttype:=aitconst_ptr_unaligned;
  1370. value:=_value;
  1371. sym:=nil;
  1372. endsym:=nil;
  1373. end;
  1374. constructor tai_const.Create_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  1375. begin
  1376. inherited Create;
  1377. typ:=ait_const;
  1378. consttype:=_typ;
  1379. sym:=_sym;
  1380. endsym:=nil;
  1381. value:=0;
  1382. { update sym info }
  1383. if assigned(sym) then
  1384. sym.increfs;
  1385. end;
  1386. constructor tai_const.Create_sym(_sym:tasmsymbol);
  1387. begin
  1388. self.create_sym_offset(_sym,0);
  1389. end;
  1390. {$ifdef i8086}
  1391. constructor tai_const.Create_sym_near(_sym: tasmsymbol);
  1392. begin
  1393. self.create_sym(_sym);
  1394. consttype:=aitconst_ptr;
  1395. end;
  1396. constructor tai_const.Create_sym_far(_sym: tasmsymbol);
  1397. begin
  1398. self.create_sym(_sym);
  1399. consttype:=aitconst_farptr;
  1400. end;
  1401. {$endif i8086}
  1402. constructor tai_const.Create_sym_offset(_sym:tasmsymbol;ofs:asizeint);
  1403. begin
  1404. inherited Create;
  1405. typ:=ait_const;
  1406. {$ifdef i8086}
  1407. if assigned(_sym) and (_sym.typ=AT_DATA) then
  1408. begin
  1409. if current_settings.x86memorymodel in x86_far_data_models then
  1410. consttype:=aitconst_farptr
  1411. else
  1412. consttype:=aitconst_ptr;
  1413. end
  1414. else
  1415. begin
  1416. if current_settings.x86memorymodel in x86_far_code_models then
  1417. consttype:=aitconst_farptr
  1418. else
  1419. consttype:=aitconst_ptr;
  1420. end;
  1421. {$else i8086}
  1422. {$ifdef avr}
  1423. if assigned(_sym) and (_sym.typ=AT_FUNCTION) then
  1424. consttype:=aitconst_gs
  1425. else
  1426. {$endif avr}
  1427. consttype:=aitconst_ptr;
  1428. {$endif i8086}
  1429. { sym is allowed to be nil, this is used to write nil pointers }
  1430. sym:=_sym;
  1431. endsym:=nil;
  1432. { store the original offset in symofs so that we can recalculate the
  1433. value field in the assembler }
  1434. symofs:=ofs;
  1435. value:=ofs;
  1436. { update sym info }
  1437. if assigned(sym) then
  1438. sym.increfs;
  1439. end;
  1440. constructor tai_const.Create_type_sym_offset(_typ : taiconst_type;_sym : tasmsymbol; ofs : asizeint);
  1441. begin
  1442. inherited Create;
  1443. typ:=ait_const;
  1444. consttype:=_typ;
  1445. { sym is allowed to be nil, this is used to write nil pointers }
  1446. sym:=_sym;
  1447. endsym:=nil;
  1448. { store the original offset in symofs so that we can recalculate the
  1449. value field in the assembler }
  1450. symofs:=ofs;
  1451. value:=ofs;
  1452. { update sym info }
  1453. if assigned(sym) then
  1454. sym.increfs;
  1455. end;
  1456. constructor tai_const.Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  1457. begin
  1458. self.create_sym_offset(_sym,0);
  1459. consttype:=_typ;
  1460. endsym:=_endsym;
  1461. endsym.increfs;
  1462. end;
  1463. constructor tai_const.Create_rel_sym_offset(_typ: taiconst_type; _sym,_endsym: tasmsymbol; _ofs: int64);
  1464. begin
  1465. self.create_sym_offset(_sym,_ofs);
  1466. consttype:=_typ;
  1467. endsym:=_endsym;
  1468. endsym.increfs;
  1469. end;
  1470. constructor tai_const.Create_rva_sym(_sym:tasmsymbol);
  1471. begin
  1472. self.create_sym_offset(_sym,0);
  1473. consttype:=aitconst_rva_symbol;
  1474. end;
  1475. constructor tai_const.Createname(const name:string;ofs:asizeint);
  1476. begin
  1477. self.Createname(name,AT_NONE,ofs);
  1478. end;
  1479. constructor tai_const.Createname(const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  1480. begin
  1481. self.create_sym_offset(current_asmdata.RefAsmSymbol(name,_symtyp),ofs);
  1482. end;
  1483. constructor tai_const.Create_type_name(_typ:taiconst_type;const name:string;ofs:asizeint);
  1484. begin
  1485. self.Create_type_name(_typ,name,AT_NONE,ofs);
  1486. end;
  1487. constructor tai_const.Create_type_name(_typ:taiconst_type;const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  1488. begin
  1489. self.create_sym_offset(current_asmdata.RefAsmSymbol(name,_symtyp),ofs);
  1490. consttype:=_typ;
  1491. end;
  1492. constructor tai_const.Create_nil_codeptr;
  1493. begin
  1494. self.Create_int_codeptr(0);
  1495. end;
  1496. constructor tai_const.Create_nil_dataptr;
  1497. begin
  1498. self.Create_int_dataptr(0);
  1499. end;
  1500. constructor tai_const.Create_int_codeptr(_value: int64);
  1501. begin
  1502. inherited Create;
  1503. typ:=ait_const;
  1504. {$ifdef i8086}
  1505. if current_settings.x86memorymodel in x86_far_code_models then
  1506. consttype:=aitconst_farptr
  1507. else
  1508. {$endif i8086}
  1509. {$ifdef avr}
  1510. consttype:=aitconst_gs;
  1511. {$else avr}
  1512. consttype:=aitconst_ptr;
  1513. {$endif avr}
  1514. sym:=nil;
  1515. endsym:=nil;
  1516. symofs:=0;
  1517. value:=_value;
  1518. end;
  1519. constructor tai_const.Create_int_dataptr(_value: int64);
  1520. begin
  1521. inherited Create;
  1522. typ:=ait_const;
  1523. {$ifdef i8086}
  1524. if current_settings.x86memorymodel in x86_far_data_models then
  1525. consttype:=aitconst_farptr
  1526. else
  1527. {$endif i8086}
  1528. consttype:=aitconst_ptr;
  1529. sym:=nil;
  1530. endsym:=nil;
  1531. symofs:=0;
  1532. value:=_value;
  1533. end;
  1534. {$ifdef i8086}
  1535. constructor tai_const.Create_seg_name(const name:string);
  1536. begin
  1537. self.Createname(name,0);
  1538. self.consttype:=aitconst_seg;
  1539. end;
  1540. constructor tai_const.Create_dgroup;
  1541. begin
  1542. self.Create_16bit(0);
  1543. self.consttype:=aitconst_dgroup;
  1544. end;
  1545. constructor tai_const.Create_fardataseg;
  1546. begin
  1547. self.Create_16bit(0);
  1548. self.consttype:=aitconst_fardataseg;
  1549. end;
  1550. {$endif i8086}
  1551. constructor tai_const.ppuload(t:taitype;ppufile:tcompilerppufile);
  1552. begin
  1553. inherited ppuload(t,ppufile);
  1554. consttype:=taiconst_type(ppufile.getbyte);
  1555. sym:=ppufile.getasmsymbol;
  1556. endsym:=ppufile.getasmsymbol;
  1557. value:=ppufile.getint64;
  1558. end;
  1559. procedure tai_const.ppuwrite(ppufile:tcompilerppufile);
  1560. begin
  1561. inherited ppuwrite(ppufile);
  1562. ppufile.putbyte(byte(consttype));
  1563. ppufile.putasmsymbol(sym);
  1564. ppufile.putasmsymbol(endsym);
  1565. ppufile.putint64(value);
  1566. end;
  1567. procedure tai_const.derefimpl;
  1568. begin
  1569. end;
  1570. function tai_const.getcopy:tlinkedlistitem;
  1571. begin
  1572. getcopy:=inherited getcopy;
  1573. { we need to increase the reference number }
  1574. if assigned(sym) then
  1575. sym.increfs;
  1576. if assigned(endsym) then
  1577. endsym.increfs;
  1578. end;
  1579. function tai_const.size:longint;
  1580. begin
  1581. case consttype of
  1582. aitconst_8bit :
  1583. result:=1;
  1584. aitconst_16bit,aitconst_16bit_unaligned :
  1585. result:=2;
  1586. aitconst_32bit,aitconst_darwin_dwarf_delta32,
  1587. aitconst_32bit_unaligned:
  1588. result:=4;
  1589. aitconst_64bit,aitconst_darwin_dwarf_delta64,
  1590. aitconst_64bit_unaligned:
  1591. result:=8;
  1592. aitconst_secrel32_symbol,
  1593. aitconst_rva_symbol :
  1594. if target_info.system=system_x86_64_win64 then
  1595. result:=sizeof(longint)
  1596. else
  1597. result:=sizeof(pint);
  1598. aitconst_uleb128bit :
  1599. result:=LengthUleb128(qword(value));
  1600. aitconst_sleb128bit :
  1601. result:=LengthSleb128(value);
  1602. aitconst_half16bit,
  1603. aitconst_gs:
  1604. result:=2;
  1605. aitconst_farptr:
  1606. result:=4;
  1607. aitconst_dgroup,
  1608. aitconst_fardataseg,
  1609. aitconst_seg:
  1610. result:=2;
  1611. aitconst_got:
  1612. result:=sizeof(pint);
  1613. aitconst_gotoff_symbol:
  1614. result:=4;
  1615. else
  1616. internalerror(200603253);
  1617. end;
  1618. end;
  1619. {****************************************************************************
  1620. TAI_realconst
  1621. ****************************************************************************}
  1622. constructor tai_realconst.create_s32real(val: ts32real);
  1623. begin
  1624. inherited create;
  1625. typ:=ait_realconst;
  1626. realtyp:=aitrealconst_s32bit;
  1627. savesize:=4;
  1628. value.s32val:=val;
  1629. end;
  1630. constructor tai_realconst.create_s64real(val: ts64real);
  1631. begin
  1632. inherited create;
  1633. typ:=ait_realconst;
  1634. realtyp:=aitrealconst_s64bit;
  1635. savesize:=8;
  1636. value.s64val:=val;
  1637. end;
  1638. {$ifdef ARM}
  1639. constructor tai_realconst.create_s64real_hiloswapped(val : ts64real);
  1640. begin
  1641. inherited create;
  1642. typ:=ait_realconst;
  1643. realtyp:=aitrealconst_s64bit;
  1644. value.s64val:=val;
  1645. savesize:=8;
  1646. formatoptions:=fo_hiloswapped;
  1647. end;
  1648. {$endif ARM}
  1649. constructor tai_realconst.create_s80real(val: ts80real; _savesize: byte);
  1650. begin
  1651. inherited create;
  1652. typ:=ait_realconst;
  1653. realtyp:=aitrealconst_s80bit;
  1654. savesize:=_savesize;
  1655. value.s80val:=val;
  1656. end;
  1657. constructor tai_realconst.create_s128real(val: ts128real);
  1658. begin
  1659. inherited create;
  1660. typ:=ait_realconst;
  1661. realtyp:=aitrealconst_s128bit;
  1662. savesize:=16;
  1663. value.s128val:=val;
  1664. end;
  1665. constructor tai_realconst.create_s64compreal(val: ts64comp);
  1666. begin
  1667. inherited create;
  1668. typ:=ait_realconst;
  1669. realtyp:=aitrealconst_s64comp;
  1670. savesize:=8;
  1671. value.s64compval:=val;
  1672. end;
  1673. constructor tai_realconst.ppuload(t: taitype; ppufile: tcompilerppufile);
  1674. begin
  1675. inherited;
  1676. realtyp:=tairealconsttype(ppufile.getbyte);
  1677. {$ifdef ARM}
  1678. formatoptions:=tformatoptions(ppufile.getbyte);
  1679. {$endif ARM}
  1680. case realtyp of
  1681. aitrealconst_s32bit:
  1682. value.s32val:=ppufile.getreal;
  1683. aitrealconst_s64bit:
  1684. value.s64val:=ppufile.getreal;
  1685. aitrealconst_s80bit:
  1686. value.s80val:=ppufile.getreal;
  1687. aitrealconst_s128bit:
  1688. value.s128val:=ppufile.getreal;
  1689. aitrealconst_s64comp:
  1690. value.s64compval:=comp(ppufile.getint64);
  1691. else
  1692. internalerror(2014050602);
  1693. end;
  1694. end;
  1695. procedure tai_realconst.ppuwrite(ppufile: tcompilerppufile);
  1696. var
  1697. c: comp;
  1698. begin
  1699. inherited ppuwrite(ppufile);
  1700. ppufile.putbyte(byte(realtyp));
  1701. {$ifdef ARM}
  1702. ppufile.putbyte(byte(formatoptions));
  1703. {$endif ARM}
  1704. case realtyp of
  1705. aitrealconst_s32bit:
  1706. ppufile.putreal(value.s32val);
  1707. aitrealconst_s64bit:
  1708. ppufile.putreal(value.s64val);
  1709. aitrealconst_s80bit:
  1710. ppufile.putreal(value.s80val);
  1711. aitrealconst_s128bit:
  1712. ppufile.putreal(value.s128val);
  1713. aitrealconst_s64comp:
  1714. begin
  1715. c:=comp(value.s64compval);
  1716. ppufile.putint64(int64(c));
  1717. end
  1718. else
  1719. internalerror(2014050601);
  1720. end;
  1721. end;
  1722. function tai_realconst.getcopy: tlinkedlistitem;
  1723. begin
  1724. result:=inherited getcopy;
  1725. tai_realconst(result).value:=value;
  1726. tai_realconst(result).realtyp:=realtyp;
  1727. tai_realconst(result).savesize:=savesize;
  1728. {$ifdef ARM}
  1729. tai_realconst(result).formatoptions:=formatoptions;
  1730. {$endif ARM}
  1731. end;
  1732. function tai_realconst.datasize: word;
  1733. begin
  1734. case realtyp of
  1735. aitrealconst_s32bit:
  1736. result:=4;
  1737. aitrealconst_s64bit,
  1738. aitrealconst_s64comp:
  1739. result:=8;
  1740. aitrealconst_s80bit:
  1741. result:=10;
  1742. aitrealconst_s128bit:
  1743. result:=16;
  1744. else
  1745. internalerror(2014050603);
  1746. end;
  1747. end;
  1748. {****************************************************************************
  1749. TAI_STRING
  1750. ****************************************************************************}
  1751. constructor tai_string.Create(const _str : string);
  1752. begin
  1753. inherited Create;
  1754. typ:=ait_string;
  1755. len:=length(_str);
  1756. getmem(str,len+1);
  1757. move(_str[1],str^,len);
  1758. str[len]:=#0;
  1759. end;
  1760. constructor tai_string.Create_pchar(_str : pchar;length : longint);
  1761. begin
  1762. inherited Create;
  1763. typ:=ait_string;
  1764. str:=_str;
  1765. len:=length;
  1766. end;
  1767. destructor tai_string.destroy;
  1768. begin
  1769. if str<>nil then
  1770. freemem(str);
  1771. inherited Destroy;
  1772. end;
  1773. constructor tai_string.ppuload(t:taitype;ppufile:tcompilerppufile);
  1774. begin
  1775. inherited ppuload(t,ppufile);
  1776. len:=ppufile.getlongint;
  1777. getmem(str,len+1);
  1778. ppufile.getdata(str^,len);
  1779. str[len]:=#0
  1780. end;
  1781. procedure tai_string.ppuwrite(ppufile:tcompilerppufile);
  1782. begin
  1783. inherited ppuwrite(ppufile);
  1784. ppufile.putlongint(len);
  1785. ppufile.putdata(str^,len);
  1786. end;
  1787. function tai_string.getcopy : tlinkedlistitem;
  1788. var
  1789. p : tlinkedlistitem;
  1790. begin
  1791. p:=inherited getcopy;
  1792. getmem(tai_string(p).str,len);
  1793. move(str^,tai_string(p).str^,len);
  1794. getcopy:=p;
  1795. end;
  1796. {****************************************************************************
  1797. TAI_LABEL
  1798. ****************************************************************************}
  1799. constructor tai_label.Create(_labsym : tasmlabel);
  1800. begin
  1801. inherited Create;
  1802. typ:=ait_label;
  1803. labsym:=_labsym;
  1804. labsym.is_set:=true;
  1805. end;
  1806. constructor tai_label.ppuload(t:taitype;ppufile:tcompilerppufile);
  1807. begin
  1808. inherited ppuload(t,ppufile);
  1809. labsym:=tasmlabel(ppufile.getasmsymbol);
  1810. ppufile.getbyte; { was is_global flag, now unused }
  1811. end;
  1812. procedure tai_label.ppuwrite(ppufile:tcompilerppufile);
  1813. begin
  1814. inherited ppuwrite(ppufile);
  1815. ppufile.putasmsymbol(labsym);
  1816. ppufile.putbyte(0); { was is_global flag, now unused }
  1817. end;
  1818. procedure tai_label.derefimpl;
  1819. begin
  1820. labsym.is_set:=true;
  1821. end;
  1822. {****************************************************************************
  1823. tai_comment comment to be inserted in the assembler file
  1824. ****************************************************************************}
  1825. constructor tai_comment.Create(_str : pchar);
  1826. begin
  1827. inherited Create;
  1828. typ:=ait_comment;
  1829. str:=_str;
  1830. end;
  1831. destructor tai_comment.destroy;
  1832. begin
  1833. freemem(str);
  1834. inherited Destroy;
  1835. end;
  1836. constructor tai_comment.ppuload(t:taitype;ppufile:tcompilerppufile);
  1837. var
  1838. len : longint;
  1839. begin
  1840. inherited ppuload(t,ppufile);
  1841. len:=ppufile.getlongint;
  1842. getmem(str,len+1);
  1843. ppufile.getdata(str^,len);
  1844. str[len]:=#0;
  1845. end;
  1846. procedure tai_comment.ppuwrite(ppufile:tcompilerppufile);
  1847. var
  1848. len : longint;
  1849. begin
  1850. inherited ppuwrite(ppufile);
  1851. len:=strlen(str);
  1852. ppufile.putlongint(len);
  1853. ppufile.putdata(str^,len);
  1854. end;
  1855. function tai_comment.getcopy : tlinkedlistitem;
  1856. var
  1857. p : tlinkedlistitem;
  1858. begin
  1859. p:=inherited getcopy;
  1860. getmem(tai_comment(p).str,strlen(str)+1);
  1861. move(str^,tai_comment(p).str^,strlen(str)+1);
  1862. getcopy:=p;
  1863. end;
  1864. {****************************************************************************
  1865. TAI_STABS
  1866. ****************************************************************************}
  1867. constructor tai_stab.create(_stabtype:TStabType;_str : pchar);
  1868. begin
  1869. inherited create;
  1870. typ:=ait_stab;
  1871. str:=_str;
  1872. stabtype:=_stabtype;
  1873. end;
  1874. constructor tai_stab.create_str(_stabtype:TStabType;const s:string);
  1875. begin
  1876. self.create(_stabtype,strpnew(s));
  1877. end;
  1878. constructor tai_stab.create_ansistr(_stabtype:TStabType;const s:ansistring);
  1879. begin
  1880. inherited create;
  1881. typ:=ait_stab;
  1882. stabtype:=_stabtype;
  1883. getmem(str,length(s)+1);
  1884. if length(s)>0 then
  1885. move(s[1],str^,length(s)+1)
  1886. else
  1887. str^:=#0;
  1888. end;
  1889. destructor tai_stab.destroy;
  1890. begin
  1891. freemem(str);
  1892. inherited destroy;
  1893. end;
  1894. {****************************************************************************
  1895. TAI_FORCE_LINE
  1896. ****************************************************************************}
  1897. constructor tai_force_line.create;
  1898. begin
  1899. inherited create;
  1900. typ:=ait_force_line;
  1901. end;
  1902. {****************************************************************************
  1903. TAI_FUNCTION_NAME
  1904. ****************************************************************************}
  1905. constructor tai_function_name.create(const s:string);
  1906. begin
  1907. inherited create;
  1908. typ:=ait_function_name;
  1909. funcname:=stringdup(s);
  1910. end;
  1911. destructor tai_function_name.destroy;
  1912. begin
  1913. stringdispose(funcname);
  1914. inherited destroy;
  1915. end;
  1916. {****************************************************************************
  1917. TAI_CUTOBJECT
  1918. ****************************************************************************}
  1919. constructor tai_cutobject.Create;
  1920. begin
  1921. inherited Create;
  1922. typ:=ait_cutobject;
  1923. place:=cut_normal;
  1924. end;
  1925. constructor tai_cutobject.Create_begin;
  1926. begin
  1927. inherited Create;
  1928. typ:=ait_cutobject;
  1929. place:=cut_begin;
  1930. end;
  1931. constructor tai_cutobject.Create_end;
  1932. begin
  1933. inherited Create;
  1934. typ:=ait_cutobject;
  1935. place:=cut_end;
  1936. end;
  1937. constructor tai_cutobject.ppuload(t:taitype;ppufile:tcompilerppufile);
  1938. begin
  1939. inherited ppuload(t,ppufile);
  1940. place:=TCutPlace(ppufile.getbyte);
  1941. end;
  1942. procedure tai_cutobject.ppuwrite(ppufile:tcompilerppufile);
  1943. begin
  1944. inherited ppuwrite(ppufile);
  1945. ppufile.putbyte(byte(place));
  1946. end;
  1947. {****************************************************************************
  1948. Tai_Marker
  1949. ****************************************************************************}
  1950. constructor Tai_Marker.Create(_Kind: TAsmMarker);
  1951. begin
  1952. Inherited Create;
  1953. typ := ait_marker;
  1954. Kind := _Kind;
  1955. end;
  1956. constructor Tai_Marker.ppuload(t:taitype;ppufile:tcompilerppufile);
  1957. begin
  1958. inherited ppuload(t,ppufile);
  1959. kind:=TAsmMarker(ppufile.getbyte);
  1960. end;
  1961. procedure Tai_Marker.ppuwrite(ppufile:tcompilerppufile);
  1962. begin
  1963. inherited ppuwrite(ppufile);
  1964. ppufile.putbyte(byte(kind));
  1965. end;
  1966. {*****************************************************************************
  1967. tai_tempalloc
  1968. *****************************************************************************}
  1969. constructor tai_tempalloc.alloc(pos,size:longint);
  1970. begin
  1971. inherited Create;
  1972. typ:=ait_tempalloc;
  1973. allocation:=true;
  1974. temppos:=pos;
  1975. tempsize:=size;
  1976. {$ifdef EXTDEBUG}
  1977. problem:=nil;
  1978. {$endif EXTDEBUG}
  1979. end;
  1980. destructor tai_tempalloc.destroy;
  1981. begin
  1982. {$ifdef EXTDEBUG}
  1983. stringdispose(problem);
  1984. {$endif EXTDEBUG}
  1985. inherited destroy;
  1986. end;
  1987. constructor tai_tempalloc.dealloc(pos,size:longint);
  1988. begin
  1989. inherited Create;
  1990. typ:=ait_tempalloc;
  1991. allocation:=false;
  1992. temppos:=pos;
  1993. tempsize:=size;
  1994. {$ifdef EXTDEBUG}
  1995. problem:=nil;
  1996. {$endif EXTDEBUG}
  1997. end;
  1998. {$ifdef EXTDEBUG}
  1999. constructor tai_tempalloc.allocinfo(pos,size:longint;const st:string);
  2000. begin
  2001. inherited Create;
  2002. typ:=ait_tempalloc;
  2003. allocation:=false;
  2004. temppos:=pos;
  2005. tempsize:=size;
  2006. problem:=stringdup(st);
  2007. end;
  2008. {$endif EXTDEBUG}
  2009. constructor tai_tempalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  2010. begin
  2011. inherited ppuload(t,ppufile);
  2012. temppos:=ppufile.getlongint;
  2013. tempsize:=ppufile.getlongint;
  2014. allocation:=boolean(ppufile.getbyte);
  2015. {$ifdef EXTDEBUG}
  2016. problem:=nil;
  2017. {$endif EXTDEBUG}
  2018. end;
  2019. procedure tai_tempalloc.ppuwrite(ppufile:tcompilerppufile);
  2020. begin
  2021. inherited ppuwrite(ppufile);
  2022. ppufile.putlongint(temppos);
  2023. ppufile.putlongint(tempsize);
  2024. ppufile.putbyte(byte(allocation));
  2025. end;
  2026. {*****************************************************************************
  2027. tai_regalloc
  2028. *****************************************************************************}
  2029. constructor tai_regalloc.alloc(r : tregister;ainstr:tai);
  2030. begin
  2031. inherited create;
  2032. typ:=ait_regalloc;
  2033. ratype:=ra_alloc;
  2034. reg:=r;
  2035. { ainstr must be an instruction }
  2036. if assigned(ainstr) and
  2037. (ainstr.typ<>ait_instruction) then
  2038. internalerror(200411011);
  2039. instr:=ainstr;
  2040. end;
  2041. constructor tai_regalloc.dealloc(r : tregister;ainstr:tai);
  2042. begin
  2043. inherited create;
  2044. typ:=ait_regalloc;
  2045. ratype:=ra_dealloc;
  2046. reg:=r;
  2047. { ainstr must be an instruction }
  2048. if assigned(ainstr) and
  2049. (ainstr.typ<>ait_instruction) then
  2050. internalerror(200411012);
  2051. instr:=ainstr;
  2052. end;
  2053. constructor tai_regalloc.sync(r : tregister);
  2054. begin
  2055. inherited create;
  2056. typ:=ait_regalloc;
  2057. ratype:=ra_sync;
  2058. reg:=r;
  2059. end;
  2060. constructor tai_regalloc.resize(r : tregister);
  2061. begin
  2062. inherited create;
  2063. typ:=ait_regalloc;
  2064. ratype:=ra_resize;
  2065. reg:=r;
  2066. end;
  2067. constructor tai_regalloc.markused(r : tregister);
  2068. begin
  2069. inherited create;
  2070. typ:=ait_regalloc;
  2071. ratype:=ra_markused;
  2072. reg:=r;
  2073. end;
  2074. constructor tai_regalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  2075. begin
  2076. inherited ppuload(t,ppufile);
  2077. ppufile.getdata(reg,sizeof(Tregister));
  2078. ratype:=tregalloctype(ppufile.getbyte);
  2079. keep:=boolean(ppufile.getbyte);
  2080. end;
  2081. procedure tai_regalloc.ppuwrite(ppufile:tcompilerppufile);
  2082. begin
  2083. inherited ppuwrite(ppufile);
  2084. ppufile.putdata(reg,sizeof(Tregister));
  2085. ppufile.putbyte(byte(ratype));
  2086. ppufile.putbyte(byte(keep));
  2087. end;
  2088. {*****************************************************************************
  2089. TaiInstruction
  2090. *****************************************************************************}
  2091. constructor tai_cpu_abstract.Create(op : tasmop);
  2092. begin
  2093. inherited create;
  2094. typ:=ait_instruction;
  2095. is_jmp:=false;
  2096. opcode:=op;
  2097. ops:=0;
  2098. end;
  2099. destructor tai_cpu_abstract.Destroy;
  2100. var
  2101. i : integer;
  2102. begin
  2103. for i:=0 to opercnt-1 do
  2104. freeop(i);
  2105. inherited destroy;
  2106. end;
  2107. { ---------------------------------------------------------------------
  2108. Loading of operands.
  2109. ---------------------------------------------------------------------}
  2110. procedure tai_cpu_abstract.allocate_oper(opers:longint);
  2111. begin
  2112. while (opers>opercnt) do
  2113. begin
  2114. new(oper[opercnt]);
  2115. fillchar(oper[opercnt]^,sizeof(toper),0);
  2116. inc(opercnt);
  2117. end;
  2118. end;
  2119. procedure tai_cpu_abstract.loadconst(opidx:longint;l:aint);
  2120. begin
  2121. allocate_oper(opidx+1);
  2122. with oper[opidx]^ do
  2123. begin
  2124. if typ<>top_const then
  2125. clearop(opidx);
  2126. val:=l;
  2127. typ:=top_const;
  2128. end;
  2129. end;
  2130. procedure tai_cpu_abstract.loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  2131. var
  2132. r : treference;
  2133. begin
  2134. reference_reset_symbol(r,s,sofs,1);
  2135. r.refaddr:=addr_full;
  2136. loadref(opidx,r);
  2137. end;
  2138. procedure tai_cpu_abstract.loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset,forceref:boolean);
  2139. begin
  2140. if not assigned(s) then
  2141. internalerror(200204251);
  2142. allocate_oper(opidx+1);
  2143. with oper[opidx]^ do
  2144. begin
  2145. if typ<>top_local then
  2146. begin
  2147. clearop(opidx);
  2148. new(localoper);
  2149. end;
  2150. with oper[opidx]^.localoper^ do
  2151. begin
  2152. localsym:=s;
  2153. localsymofs:=sofs;
  2154. localindexreg:=indexreg;
  2155. localscale:=scale;
  2156. localgetoffset:=getoffset;
  2157. localforceref:=forceref;
  2158. end;
  2159. typ:=top_local;
  2160. end;
  2161. end;
  2162. procedure tai_cpu_abstract.loadref(opidx:longint;const r:treference);
  2163. begin
  2164. allocate_oper(opidx+1);
  2165. with oper[opidx]^ do
  2166. begin
  2167. if typ<>top_ref then
  2168. begin
  2169. clearop(opidx);
  2170. new(ref);
  2171. end;
  2172. ref^:=r;
  2173. {$ifdef x86}
  2174. { We allow this exception for x86, since overloading this would be
  2175. too much of a a speed penalty}
  2176. if (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  2177. segprefix:=ref^.segment;
  2178. {$endif}
  2179. {$ifndef llvm}
  2180. if (cs_create_pic in current_settings.moduleswitches) and
  2181. assigned(r.symbol) and
  2182. not assigned(r.relsymbol) and
  2183. (r.refaddr=addr_no)
  2184. {$ifdef ARM}
  2185. and not(r.base=NR_R15)
  2186. {$endif ARM}
  2187. {$ifdef aarch64}
  2188. and not(r.refaddr in [addr_full,addr_gotpageoffset,addr_gotpage])
  2189. {$endif aarch64}
  2190. then
  2191. internalerror(200502052);
  2192. {$endif not llvm}
  2193. typ:=top_ref;
  2194. if assigned(add_reg_instruction_hook) then
  2195. begin
  2196. add_reg_instruction_hook(self,ref^.base);
  2197. add_reg_instruction_hook(self,ref^.index);
  2198. end;
  2199. { mark symbol as used }
  2200. if assigned(ref^.symbol) then
  2201. ref^.symbol.increfs;
  2202. if assigned(ref^.relsymbol) then
  2203. ref^.relsymbol.increfs;
  2204. end;
  2205. end;
  2206. procedure tai_cpu_abstract.loadreg(opidx:longint;r:tregister);
  2207. begin
  2208. allocate_oper(opidx+1);
  2209. with oper[opidx]^ do
  2210. begin
  2211. if typ<>top_reg then
  2212. clearop(opidx);
  2213. reg:=r;
  2214. typ:=top_reg;
  2215. end;
  2216. if assigned(add_reg_instruction_hook) then
  2217. add_reg_instruction_hook(self,r);
  2218. {$ifdef ARM}
  2219. { R15 is the PC on the ARM thus moves to R15 are jumps.
  2220. Due to speed considerations we don't use a virtual overridden method here.
  2221. Because the pc/r15 isn't handled by the reg. allocator this should never cause
  2222. problems with iregs getting r15.
  2223. }
  2224. is_jmp:=(opcode=A_MOV) and (opidx=0) and (r=NR_R15);
  2225. {$endif ARM}
  2226. end;
  2227. procedure tai_cpu_abstract.loadoper(opidx:longint;o:toper);
  2228. begin
  2229. allocate_oper(opidx+1);
  2230. clearop(opidx);
  2231. oper[opidx]^:=o;
  2232. { copy also the reference }
  2233. with oper[opidx]^ do
  2234. begin
  2235. case typ of
  2236. top_reg:
  2237. begin
  2238. if assigned(add_reg_instruction_hook) then
  2239. add_reg_instruction_hook(self,reg);
  2240. end;
  2241. top_ref:
  2242. begin
  2243. new(ref);
  2244. ref^:=o.ref^;
  2245. {$ifdef x86}
  2246. if (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  2247. segprefix:=ref^.segment;
  2248. {$endif x86}
  2249. if assigned(add_reg_instruction_hook) then
  2250. begin
  2251. add_reg_instruction_hook(self,ref^.base);
  2252. add_reg_instruction_hook(self,ref^.index);
  2253. end;
  2254. end;
  2255. {$ifdef ARM}
  2256. top_shifterop:
  2257. begin
  2258. new(shifterop);
  2259. shifterop^:=o.shifterop^;
  2260. if assigned(add_reg_instruction_hook) then
  2261. add_reg_instruction_hook(self,shifterop^.rs);
  2262. end;
  2263. {$endif ARM}
  2264. end;
  2265. end;
  2266. end;
  2267. procedure tai_cpu_abstract.clearop(opidx:longint);
  2268. begin
  2269. with oper[opidx]^ do
  2270. begin
  2271. case typ of
  2272. top_ref:
  2273. dispose(ref);
  2274. top_local:
  2275. dispose(localoper);
  2276. {$ifdef ARM}
  2277. top_shifterop:
  2278. dispose(shifterop);
  2279. top_regset:
  2280. dispose(regset);
  2281. {$endif ARM}
  2282. {$ifdef jvm}
  2283. top_string:
  2284. freemem(pcval);
  2285. top_wstring:
  2286. donewidestring(pwstrval);
  2287. {$endif jvm}
  2288. end;
  2289. typ:=top_none;
  2290. end;
  2291. end;
  2292. procedure tai_cpu_abstract.freeop(opidx:longint);
  2293. begin
  2294. clearop(opidx);
  2295. dispose(oper[opidx]);
  2296. end;
  2297. { ---------------------------------------------------------------------
  2298. Miscellaneous methods.
  2299. ---------------------------------------------------------------------}
  2300. procedure tai_cpu_abstract.SetCondition(const c:TAsmCond);
  2301. begin
  2302. condition:=c;
  2303. end;
  2304. Function tai_cpu_abstract.getcopy:TLinkedListItem;
  2305. var
  2306. i : longint;
  2307. p : tai_cpu_abstract;
  2308. begin
  2309. p:=tai_cpu_abstract(inherited getcopy);
  2310. { make a copy of the references }
  2311. p.opercnt:=0;
  2312. p.allocate_oper(ops);
  2313. for i:=0 to ops-1 do
  2314. begin
  2315. p.oper[i]^:=oper[i]^;
  2316. case oper[i]^.typ of
  2317. top_local :
  2318. begin
  2319. new(p.oper[i]^.localoper);
  2320. p.oper[i]^.localoper^:=oper[i]^.localoper^;
  2321. end;
  2322. top_ref :
  2323. begin
  2324. new(p.oper[i]^.ref);
  2325. p.oper[i]^.ref^:=oper[i]^.ref^;
  2326. end;
  2327. {$ifdef ARM}
  2328. top_shifterop:
  2329. begin
  2330. new(p.oper[i]^.shifterop);
  2331. p.oper[i]^.shifterop^:=oper[i]^.shifterop^;
  2332. end;
  2333. {$endif ARM}
  2334. end;
  2335. end;
  2336. getcopy:=p;
  2337. end;
  2338. function tai_cpu_abstract.is_same_reg_move(regtype: Tregistertype):boolean;
  2339. begin
  2340. { When the generic RA is used this needs to be overridden, we don't use
  2341. virtual;abstract; to prevent a lot of warnings of unimplemented abstract methods
  2342. when tai_cpu is created (PFV) }
  2343. internalerror(2004040901);
  2344. result:=false;
  2345. end;
  2346. function tai_cpu_abstract.spilling_get_operation_type(opnr: longint): topertype;
  2347. begin
  2348. internalerror(2004040902);
  2349. result:=operand_readwrite;
  2350. end;
  2351. function tai_cpu_abstract.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
  2352. begin
  2353. result := operand_read;
  2354. end;
  2355. constructor tai_cpu_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  2356. var
  2357. i : integer;
  2358. begin
  2359. inherited ppuload(t,ppufile);
  2360. { hopefully, we don't get problems with big/litte endian here when cross compiling :/ }
  2361. ppufile.getdata(condition,sizeof(tasmcond));
  2362. allocate_oper(ppufile.getbyte);
  2363. for i:=0 to ops-1 do
  2364. ppuloadoper(ppufile,oper[i]^);
  2365. opcode:=tasmop(ppufile.getword);
  2366. {$ifdef x86}
  2367. ppufile.getdata(segprefix,sizeof(Tregister));
  2368. {$endif x86}
  2369. is_jmp:=boolean(ppufile.getbyte);
  2370. end;
  2371. procedure tai_cpu_abstract.ppuwrite(ppufile:tcompilerppufile);
  2372. var
  2373. i : integer;
  2374. begin
  2375. inherited ppuwrite(ppufile);
  2376. ppufile.putdata(condition,sizeof(tasmcond));
  2377. ppufile.putbyte(ops);
  2378. for i:=0 to ops-1 do
  2379. ppuwriteoper(ppufile,oper[i]^);
  2380. ppufile.putword(word(opcode));
  2381. {$ifdef x86}
  2382. ppufile.putdata(segprefix,sizeof(Tregister));
  2383. {$endif x86}
  2384. ppufile.putbyte(byte(is_jmp));
  2385. end;
  2386. procedure tai_cpu_abstract.buildderefimpl;
  2387. var
  2388. i : integer;
  2389. begin
  2390. for i:=0 to ops-1 do
  2391. ppubuildderefimploper(oper[i]^);
  2392. end;
  2393. procedure tai_cpu_abstract.derefimpl;
  2394. var
  2395. i : integer;
  2396. begin
  2397. for i:=0 to ops-1 do
  2398. ppuderefoper(oper[i]^);
  2399. end;
  2400. procedure tai_cpu_abstract.resetpass1;
  2401. begin
  2402. end;
  2403. procedure tai_cpu_abstract.resetpass2;
  2404. begin
  2405. end;
  2406. function tai_cpu_abstract.Pass1(objdata:TObjData):longint;
  2407. begin
  2408. result:=0;
  2409. end;
  2410. procedure tai_cpu_abstract.Pass2(objdata:TObjData);
  2411. begin
  2412. end;
  2413. procedure tai_cpu_abstract.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  2414. begin
  2415. o.typ:=toptype(ppufile.getbyte);
  2416. o.ot:=ppufile.getlongint;
  2417. case o.typ of
  2418. top_reg :
  2419. ppufile.getdata(o.reg,sizeof(Tregister));
  2420. top_ref :
  2421. begin
  2422. new(o.ref);
  2423. {$ifdef x86}
  2424. ppufile.getdata(o.ref^.segment,sizeof(Tregister));
  2425. {$endif x86}
  2426. ppufile.getdata(o.ref^.base,sizeof(Tregister));
  2427. ppufile.getdata(o.ref^.index,sizeof(Tregister));
  2428. ppufile.getdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));
  2429. o.ref^.scalefactor:=ppufile.getbyte;
  2430. o.ref^.offset:=ppufile.getaint;
  2431. o.ref^.symbol:=ppufile.getasmsymbol;
  2432. o.ref^.relsymbol:=ppufile.getasmsymbol;
  2433. end;
  2434. top_const :
  2435. o.val:=ppufile.getaint;
  2436. top_local :
  2437. begin
  2438. new(o.localoper);
  2439. with o.localoper^ do
  2440. begin
  2441. ppufile.getderef(localsymderef);
  2442. localsymofs:=ppufile.getaint;
  2443. localindexreg:=tregister(ppufile.getlongint);
  2444. localscale:=ppufile.getbyte;
  2445. localgetoffset:=(ppufile.getbyte<>0);
  2446. end;
  2447. end;
  2448. else
  2449. internalerror(2007010210);
  2450. end;
  2451. end;
  2452. procedure tai_cpu_abstract.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  2453. begin
  2454. ppufile.putbyte(byte(o.typ));
  2455. ppufile.putlongint(o.ot);
  2456. case o.typ of
  2457. top_reg :
  2458. ppufile.putdata(o.reg,sizeof(Tregister));
  2459. top_ref :
  2460. begin
  2461. {$ifdef x86}
  2462. ppufile.putdata(o.ref^.segment,sizeof(Tregister));
  2463. {$endif x86}
  2464. ppufile.putdata(o.ref^.base,sizeof(Tregister));
  2465. ppufile.putdata(o.ref^.index,sizeof(Tregister));
  2466. ppufile.putdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));
  2467. ppufile.putbyte(o.ref^.scalefactor);
  2468. ppufile.putaint(o.ref^.offset);
  2469. ppufile.putasmsymbol(o.ref^.symbol);
  2470. ppufile.putasmsymbol(o.ref^.relsymbol);
  2471. end;
  2472. top_const :
  2473. ppufile.putaint(o.val);
  2474. top_local :
  2475. begin
  2476. with o.localoper^ do
  2477. begin
  2478. ppufile.putderef(localsymderef);
  2479. ppufile.putaint(localsymofs);
  2480. ppufile.putlongint(longint(localindexreg));
  2481. ppufile.putbyte(localscale);
  2482. ppufile.putbyte(byte(localgetoffset));
  2483. end;
  2484. end;
  2485. else
  2486. internalerror(2007010211);
  2487. end;
  2488. end;
  2489. {****************************************************************************
  2490. tai_align_abstract
  2491. ****************************************************************************}
  2492. constructor tai_align_abstract.Create(b: byte);
  2493. begin
  2494. inherited Create;
  2495. typ:=ait_align;
  2496. {$ifdef EXTDEBUG}
  2497. if upper(classname)='TAI_ALIGN_ABSTRACT' then
  2498. internalerror(200709191);
  2499. {$endif EXTDEBUG}
  2500. if b in [1,2,4,8,16,32] then
  2501. aligntype := b
  2502. else
  2503. aligntype := 1;
  2504. fillsize:=0;
  2505. fillop:=0;
  2506. use_op:=false;
  2507. end;
  2508. constructor tai_align_abstract.Create_op(b: byte; _op: byte);
  2509. begin
  2510. inherited Create;
  2511. typ:=ait_align;
  2512. if b in [1,2,4,8,16,32] then
  2513. aligntype := b
  2514. else
  2515. aligntype := 1;
  2516. fillsize:=0;
  2517. fillop:=_op;
  2518. use_op:=true;
  2519. end;
  2520. constructor tai_align_abstract.Create_zeros(b: byte);
  2521. begin
  2522. inherited Create;
  2523. typ:=ait_align;
  2524. if b in [1,2,4,8,16,32] then
  2525. aligntype := b
  2526. else
  2527. aligntype := 1;
  2528. use_op:=true;
  2529. fillsize:=0;
  2530. fillop:=0;
  2531. end;
  2532. function tai_align_abstract.calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;
  2533. begin
  2534. if fillsize>sizeof(buf) then
  2535. internalerror(200404293);
  2536. fillchar(buf,high(buf),fillop);
  2537. calculatefillbuf:=pchar(@buf);
  2538. end;
  2539. constructor tai_align_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  2540. begin
  2541. inherited ppuload(t,ppufile);
  2542. aligntype:=ppufile.getbyte;
  2543. fillsize:=0;
  2544. fillop:=ppufile.getbyte;
  2545. use_op:=boolean(ppufile.getbyte);
  2546. end;
  2547. procedure tai_align_abstract.ppuwrite(ppufile:tcompilerppufile);
  2548. begin
  2549. inherited ppuwrite(ppufile);
  2550. ppufile.putbyte(aligntype);
  2551. ppufile.putbyte(fillop);
  2552. ppufile.putbyte(byte(use_op));
  2553. end;
  2554. {****************************************************************************
  2555. tai_seh_directive
  2556. ****************************************************************************}
  2557. const
  2558. datatypemap: array[TAsmSehDirective] of TSehDirectiveDatatype=(
  2559. sd_string, { proc }
  2560. sd_none, { endproc }
  2561. sd_none, { endprologue }
  2562. sd_string, { handler }
  2563. sd_none, { handlerdata }
  2564. sd_none,sd_none,sd_none, { eh, 32, no32 }
  2565. sd_regoffset, { setframe }
  2566. sd_offset, { stackalloc }
  2567. sd_reg, { pushreg }
  2568. sd_regoffset, { savereg }
  2569. sd_regoffset, { savexmm }
  2570. sd_none { pushframe }
  2571. );
  2572. constructor tai_seh_directive.create(_kind:TAsmSehDirective);
  2573. begin
  2574. inherited Create;
  2575. typ:=ait_seh_directive;
  2576. kind:=_kind;
  2577. data.typ:=datatypemap[_kind];
  2578. end;
  2579. constructor tai_seh_directive.create_name(_kind:TAsmSehDirective;const _name:string);
  2580. begin
  2581. create(_kind);
  2582. data.name:=stringdup(_name);
  2583. end;
  2584. constructor tai_seh_directive.create_reg(_kind:TAsmSehDirective;r:TRegister);
  2585. begin
  2586. create(_kind);
  2587. data.reg:=r;
  2588. end;
  2589. constructor tai_seh_directive.create_offset(_kind:TAsmSehDirective;ofs:dword);
  2590. begin
  2591. create(_kind);
  2592. data.offset:=ofs;
  2593. end;
  2594. constructor tai_seh_directive.create_reg_offset(_kind:TAsmSehDirective;
  2595. r:TRegister;ofs:dword);
  2596. begin
  2597. create(_kind);
  2598. data.offset:=ofs;
  2599. data.reg:=r;
  2600. end;
  2601. constructor tai_seh_directive.ppuload(t:taitype;ppufile:tcompilerppufile);
  2602. begin
  2603. inherited ppuload(t, ppufile);
  2604. kind:=TAsmSehDirective(ppufile.getbyte);
  2605. data.typ:=datatypemap[kind];
  2606. case data.typ of
  2607. sd_none: ;
  2608. sd_string:
  2609. begin
  2610. data.name:=stringdup(ppufile.getstring);
  2611. data.flags:=ppufile.getbyte;
  2612. end;
  2613. sd_reg,sd_offset,sd_regoffset:
  2614. begin
  2615. ppufile.getdata(data.reg,sizeof(TRegister));
  2616. data.offset:=ppufile.getdword;
  2617. end;
  2618. else
  2619. InternalError(2011091201);
  2620. end;
  2621. end;
  2622. destructor tai_seh_directive.destroy;
  2623. begin
  2624. if data.typ=sd_string then
  2625. stringdispose(data.name);
  2626. inherited destroy;
  2627. end;
  2628. procedure tai_seh_directive.ppuwrite(ppufile:tcompilerppufile);
  2629. begin
  2630. inherited ppuwrite(ppufile);
  2631. ppufile.putbyte(ord(kind));
  2632. case data.typ of
  2633. sd_none: ;
  2634. sd_string:
  2635. begin
  2636. ppufile.putstring(data.name^);
  2637. ppufile.putbyte(data.flags);
  2638. end;
  2639. sd_reg,sd_offset,sd_regoffset:
  2640. begin
  2641. ppufile.putdata(data.reg,sizeof(TRegister));
  2642. ppufile.putdword(data.offset);
  2643. end;
  2644. else
  2645. InternalError(2011091202);
  2646. end;
  2647. end;
  2648. procedure tai_seh_directive.generate_code(objdata:TObjData);
  2649. begin
  2650. end;
  2651. {$ifdef JVM}
  2652. {****************************************************************************
  2653. tai_jvar
  2654. ****************************************************************************}
  2655. constructor tai_jvar.Create(_stackslot: longint; const _desc: shortstring; _startlab, _stoplab: TAsmSymbol);
  2656. begin
  2657. Inherited create;
  2658. typ:=ait_jvar;
  2659. stackslot:=_stackslot;
  2660. desc:=stringdup(_desc);
  2661. startlab:=_startlab;
  2662. stoplab:=_stoplab;
  2663. end;
  2664. constructor tai_jvar.ppuload(t: taitype; ppufile: tcompilerppufile);
  2665. begin
  2666. inherited ppuload(t, ppufile);
  2667. stackslot:=ppufile.getlongint;
  2668. desc:=stringdup(ppufile.getstring);
  2669. startlab:=ppufile.getasmsymbol;
  2670. stoplab:=ppufile.getasmsymbol;
  2671. end;
  2672. procedure tai_jvar.ppuwrite(ppufile: tcompilerppufile);
  2673. begin
  2674. inherited ppuwrite(ppufile);
  2675. ppufile.putlongint(stackslot);
  2676. ppufile.putstring(desc^);
  2677. ppufile.putasmsymbol(startlab);
  2678. ppufile.putasmsymbol(stoplab);
  2679. end;
  2680. destructor tai_jvar.destroy;
  2681. begin
  2682. stringdispose(desc);
  2683. inherited destroy;
  2684. end;
  2685. {****************************************************************************
  2686. tai_jcatch
  2687. ****************************************************************************}
  2688. constructor tai_jcatch.Create(const _name: shortstring; _startlab, _stoplab, _handlerlab: TAsmSymbol);
  2689. begin
  2690. Inherited create;
  2691. typ:=ait_jcatch;
  2692. name:=stringdup(_name);
  2693. startlab:=_startlab;
  2694. startlab.increfs;
  2695. stoplab:=_stoplab;
  2696. stoplab.increfs;
  2697. handlerlab:=_handlerlab;
  2698. handlerlab.increfs;
  2699. end;
  2700. destructor tai_jcatch.destroy;
  2701. begin
  2702. stringdispose(name);
  2703. inherited destroy;
  2704. end;
  2705. constructor tai_jcatch.ppuload(t: taitype; ppufile: tcompilerppufile);
  2706. begin
  2707. inherited ppuload(t, ppufile);
  2708. name:=stringdup(ppufile.getstring);
  2709. startlab:=ppufile.getasmsymbol;
  2710. startlab.increfs;
  2711. stoplab:=ppufile.getasmsymbol;
  2712. stoplab.increfs;
  2713. handlerlab:=ppufile.getasmsymbol;
  2714. handlerlab.increfs;
  2715. end;
  2716. procedure tai_jcatch.ppuwrite(ppufile: tcompilerppufile);
  2717. begin
  2718. inherited ppuwrite(ppufile);
  2719. ppufile.putstring(name^);
  2720. ppufile.putasmsymbol(startlab);
  2721. ppufile.putasmsymbol(stoplab);
  2722. ppufile.putasmsymbol(handlerlab);
  2723. end;
  2724. {$endif JVM}
  2725. begin
  2726. {$push}{$warnings off}
  2727. { taitype should fit into a 4 byte set for speed reasons }
  2728. if ord(high(taitype))>31 then
  2729. internalerror(201108181);
  2730. {$pop}
  2731. end.