aasmtai.pas 94 KB

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