aasmtai.pas 94 KB

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