aasmtai.pas 110 KB

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