aasmtai.pas 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678
  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. {$pop}
  641. end;
  642. { Generates an uninitializised data block }
  643. tai_datablock = class(tailineinfo)
  644. is_global : boolean;
  645. sym : tasmsymbol;
  646. size : asizeint;
  647. constructor Create(const _name: string; _size: asizeint; def: tdef; _typ: Tasmsymtype);
  648. constructor Create_hidden(const _name: string; _size: asizeint; def: tdef; _typ: Tasmsymtype);
  649. constructor Create_global(const _name: string; _size: asizeint; def: tdef; _typ: Tasmsymtype);
  650. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  651. procedure ppuwrite(ppufile:tcompilerppufile);override;
  652. procedure derefimpl;override;
  653. end;
  654. { Generates an integer const }
  655. { tai_const }
  656. tai_const = class(tai)
  657. sym,
  658. endsym : tasmsymbol;
  659. { if symbols and offset are provided the symofs is used,
  660. the value is calculated during assembling }
  661. symofs,
  662. value : int64;
  663. consttype : taiconst_type;
  664. { sleb128 and uleb128 values have a varying length, by calling FixSize their size can be fixed
  665. to avoid that other offsets need to be changed. The value to write is stored in fixed_size }
  666. fixed_size : byte;
  667. { we use for the 128bit int64/qword for now because I can't imagine a
  668. case where we need 128 bit now (FK) }
  669. constructor Create(_typ:taiconst_type;_value : int64);
  670. constructor Create_128bit(_value : int64);
  671. constructor Create_64bit(_value : int64);
  672. constructor Create_32bit(_value : longint);
  673. constructor Create_16bit(_value : word);
  674. constructor Create_64bit_unaligned(_value : int64);
  675. constructor Create_32bit_unaligned(_value : longint);
  676. constructor Create_16bit_unaligned(_value : word);
  677. constructor Create_8bit(_value : byte);
  678. constructor Create_char(size: integer; _value: dword);
  679. constructor Create_sleb128bit(_value : int64);
  680. constructor Create_uleb128bit(_value : qword);
  681. constructor Create_aint(_value : aint);
  682. constructor Create_sizeint(_value : asizeint);
  683. constructor Create_sizeint_unaligned(_value : asizeint);
  684. constructor Create_sym(_sym:tasmsymbol);
  685. {$ifdef i8086}
  686. constructor Create_sym_near(_sym:tasmsymbol);
  687. constructor Create_sym_far(_sym:tasmsymbol);
  688. constructor Createname_near(const name:string;ofs:asizeint);
  689. constructor Createname_far(const name:string;ofs:asizeint);
  690. constructor Createname_near(const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  691. constructor Createname_far(const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  692. {$endif i8086}
  693. constructor Create_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  694. constructor Create_sym_offset(_sym:tasmsymbol;ofs:asizeint);
  695. constructor Create_type_sym_offset(_typ:taiconst_type;_sym:tasmsymbol;ofs:asizeint);
  696. constructor Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  697. constructor Create_rel_sym_offset(_typ : taiconst_type; _sym,_endsym : tasmsymbol; _ofs : int64);
  698. constructor Create_rva_sym(_sym:tasmsymbol);
  699. constructor Createname(const name:string;ofs:asizeint);
  700. constructor Createname_rel(const name, endname: string);
  701. constructor Createname(const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  702. constructor Create_type_name(_typ:taiconst_type;const name:string;ofs:asizeint);
  703. constructor Create_type_name(_typ:taiconst_type;const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  704. constructor Create_nil_codeptr;
  705. constructor Create_nil_codeptr_unaligned;
  706. constructor Create_nil_dataptr;
  707. constructor Create_nil_dataptr_unaligned;
  708. constructor Create_int_codeptr(_value: int64);
  709. constructor Create_int_codeptr_unaligned(_value: int64);
  710. constructor Create_int_dataptr(_value: int64);
  711. constructor Create_int_dataptr_unaligned(_value: int64);
  712. {$ifdef avr}
  713. constructor Create_int_dataptr_unaligned(_value: int64; size: taiconst_type);
  714. {$endif}
  715. {$ifdef i8086}
  716. constructor Create_seg_name(const name:string);
  717. constructor Create_dgroup;
  718. constructor Create_fardataseg;
  719. {$endif i8086}
  720. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  721. procedure ppuwrite(ppufile:tcompilerppufile);override;
  722. procedure derefimpl;override;
  723. function getcopy:tlinkedlistitem;override;
  724. function size:longint;
  725. { sleb128 and uleb128 values have a varying length, by calling FixSize their size can be fixed
  726. to avoid that other offsets need to be changed. The value to write is stored in fixed_size }
  727. Procedure FixSize;
  728. end;
  729. { floating point const }
  730. tformatoptions = (fo_none,fo_hiloswapped);
  731. tai_realconst = class(tai)
  732. realtyp: tairealconsttype;
  733. savesize: byte;
  734. value: record
  735. case tairealconsttype of
  736. aitrealconst_s32bit: (s32val: ts32real);
  737. aitrealconst_s64bit: (s64val: ts64real);
  738. aitrealconst_s80bit: (s80val: ts80real);
  739. aitrealconst_s128bit: (s128val: ts128real);
  740. aitrealconst_s64comp: (s64compval: ts64comp);
  741. end;
  742. {$ifdef ARM}
  743. formatoptions : tformatoptions;
  744. {$endif ARM}
  745. constructor create_s32real(val: ts32real);
  746. constructor create_s64real(val: ts64real);
  747. {$ifdef ARM}
  748. constructor create_s64real_hiloswapped(val : ts64real);
  749. {$endif ARM}
  750. constructor create_s80real(val: ts80real; _savesize: byte);
  751. constructor create_s128real(val: ts128real);
  752. constructor create_s64compreal(val: ts64comp);
  753. constructor ppuload(t: taitype;ppufile: tcompilerppufile); override;
  754. procedure ppuwrite(ppufile: tcompilerppufile); override;
  755. function getcopy:tlinkedlistitem;override;
  756. function datasize: word;
  757. end;
  758. { tai_stab }
  759. tai_stab = class(tai)
  760. str : pchar;
  761. stabtype : TStabType;
  762. constructor Create(_stabtype:TStabType;_str : pchar);
  763. constructor Create_str(_stabtype:TStabType;const s:string);
  764. constructor create_ansistr(_stabtype: TStabType; const s: ansistring);
  765. destructor Destroy;override;
  766. end;
  767. tai_force_line = class(tailineinfo)
  768. constructor Create;
  769. end;
  770. tai_function_name = class(tai)
  771. funcname : pshortstring;
  772. constructor create(const s:string);
  773. destructor destroy;override;
  774. end;
  775. { Insert a cut to split assembler into several smaller files }
  776. tai_cutobject = class(tai)
  777. place : tcutplace;
  778. constructor Create;
  779. constructor Create_begin;
  780. constructor Create_end;
  781. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  782. procedure ppuwrite(ppufile:tcompilerppufile);override;
  783. end;
  784. { Insert a marker for assembler and inline blocks }
  785. tai_marker = class(tai)
  786. Kind: TAsmMarker;
  787. Constructor Create(_Kind: TAsmMarker);
  788. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  789. procedure ppuwrite(ppufile:tcompilerppufile);override;
  790. end;
  791. tai_tempalloc = class(tai)
  792. allocation : boolean;
  793. {$ifdef EXTDEBUG}
  794. problem : pshortstring;
  795. {$endif EXTDEBUG}
  796. temppos,
  797. tempsize : longint;
  798. constructor alloc(pos,size:longint);
  799. constructor dealloc(pos,size:longint);
  800. {$ifdef EXTDEBUG}
  801. constructor allocinfo(pos,size:longint;const st:string);
  802. {$endif EXTDEBUG}
  803. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  804. destructor destroy;override;
  805. procedure ppuwrite(ppufile:tcompilerppufile);override;
  806. end;
  807. tai_regalloc = class(tai)
  808. reg : tregister;
  809. ratype : TRegAllocType;
  810. { tells BuildLabelTableAndFixRegAlloc that the deallocation should be kept }
  811. keep : boolean;
  812. { reg(de)alloc belongs to this instruction, this
  813. is only used for automatic inserted (de)alloc for
  814. imaginary register and required for spilling code }
  815. instr : tai;
  816. constructor alloc(r : tregister;ainstr:tai);
  817. constructor dealloc(r : tregister;ainstr:tai);
  818. constructor sync(r : tregister);
  819. constructor resize(r : tregister);
  820. constructor markused(r : tregister);
  821. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  822. procedure ppuwrite(ppufile:tcompilerppufile);override;
  823. end;
  824. tadd_reg_instruction_proc=procedure(instr:Tai;r:tregister) of object;
  825. { Class template for assembler instructions
  826. }
  827. tai_cpu_abstract = class(tailineinfo)
  828. protected
  829. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);virtual;
  830. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);virtual;
  831. procedure ppubuildderefimploper(var o:toper);virtual;abstract;
  832. procedure ppuderefoper(var o:toper);virtual;abstract;
  833. public
  834. { Condition flags for instruction }
  835. condition : TAsmCond;
  836. { Number of operands to instruction }
  837. ops : byte;
  838. { Number of allocate oper structures }
  839. opercnt : byte;
  840. { Operands of instruction }
  841. oper : array[0..max_operands-1] of poper;
  842. { Actual opcode of instruction }
  843. opcode : tasmop;
  844. {$ifdef x86}
  845. segprefix : tregister;
  846. {$endif x86}
  847. { true if instruction is a jmp }
  848. is_jmp : boolean; { is this instruction a jump? (needed for optimizer) }
  849. Constructor Create(op : tasmop);virtual;
  850. Destructor Destroy;override;
  851. function getcopy:TLinkedListItem;override;
  852. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  853. procedure ppuwrite(ppufile:tcompilerppufile);override;
  854. procedure buildderefimpl;override;
  855. procedure derefimpl;override;
  856. procedure SetCondition(const c:TAsmCond);
  857. procedure allocate_oper(opers:longint);
  858. procedure loadconst(opidx:longint;l:tcgint);
  859. procedure loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  860. procedure loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset,forceref:boolean);
  861. procedure loadref(opidx:longint;const r:treference);
  862. procedure loadreg(opidx:longint;r:tregister);
  863. procedure loadoper(opidx:longint;o:toper); virtual;
  864. procedure clearop(opidx:longint); virtual;
  865. procedure freeop(opidx:longint);
  866. { register allocator }
  867. function is_same_reg_move(regtype: Tregistertype):boolean;virtual;
  868. function spilling_get_operation_type(opnr: longint): topertype;virtual;
  869. function spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;virtual;
  870. function Pass1(objdata:TObjData):longint;virtual;
  871. procedure Pass2(objdata:TObjData);virtual;
  872. procedure resetpass1; virtual;
  873. procedure resetpass2; virtual;
  874. end;
  875. tai_cpu_class = class of tai_cpu_abstract;
  876. { Buffer type used for alignment }
  877. tfillbuffer = array[0..63] of char;
  878. { alignment for operator }
  879. tai_align_abstract = class(tai)
  880. aligntype : byte; { 1 = no align, 2 = word align, 4 = dword align }
  881. maxbytes : byte; { if needed bytes would be larger than maxbyes, alignment is ignored }
  882. fillsize : byte; { real size to fill }
  883. fillop : byte; { value to fill with - optional }
  884. use_op : boolean;
  885. constructor Create(b:byte);virtual;
  886. constructor Create_op(b: byte; _op: byte);virtual;
  887. constructor create_max(b: byte; max: byte);virtual;
  888. constructor create_op_max(b: byte; _op: byte; max: byte);virtual;
  889. constructor Create_zeros(b:byte);
  890. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  891. procedure ppuwrite(ppufile:tcompilerppufile);override;
  892. function calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;virtual;
  893. end;
  894. tai_align_class = class of tai_align_abstract;
  895. tai_varloc = class(tai)
  896. newlocation,
  897. newlocationhi : tregister;
  898. varsym : tsym;
  899. constructor create(sym : tsym;loc : tregister);
  900. constructor create64(sym : tsym;loc,lochi : tregister);
  901. {$ifdef cpu64bitalu}
  902. constructor create128(sym : tsym;loc,lochi : tregister);
  903. {$endif cpu64bitalu}
  904. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  905. procedure ppuwrite(ppufile:tcompilerppufile);override;
  906. procedure buildderefimpl;override;
  907. procedure derefimpl;override;
  908. end;
  909. TSehDirectiveDatatype=(sd_none,sd_string,sd_reg,sd_offset,sd_regoffset);
  910. TSehDirectiveData=record
  911. case typ: TSehDirectiveDatatype of
  912. sd_none: ();
  913. sd_string: (name:pshortstring;flags:byte);
  914. sd_reg,sd_offset,sd_regoffset: (reg:TRegister;offset:dword);
  915. end;
  916. tai_seh_directive = class(tai)
  917. kind: TAsmSehDirective;
  918. data: TSehDirectiveData;
  919. constructor create(_kind:TAsmSehDirective);
  920. constructor create_name(_kind:TAsmSehDirective;const _name: string);
  921. constructor create_reg(_kind:TAsmSehDirective;r:TRegister);
  922. constructor create_offset(_kind:TAsmSehDirective;ofs:dword);
  923. constructor create_reg_offset(_kind:TAsmSehDirective;r:TRegister;ofs:dword);
  924. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  925. destructor destroy;override;
  926. procedure ppuwrite(ppufile:tcompilerppufile);override;
  927. procedure generate_code(objdata:TObjData);virtual;
  928. property datatype: TSehDirectiveDatatype read data.typ;
  929. end;
  930. tai_seh_directive_class=class of tai_seh_directive;
  931. {$ifdef JVM}
  932. { JVM variable live range description }
  933. tai_jvar = class(tai)
  934. stackslot: longint;
  935. desc: pshortstring;
  936. startlab,stoplab: tasmsymbol;
  937. constructor Create(_stackslot: longint; const _desc: shortstring; _startlab, _stoplab: TAsmSymbol);
  938. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  939. procedure ppuwrite(ppufile:tcompilerppufile);override;
  940. destructor destroy;override;
  941. end;
  942. tai_jvar_class = class of tai_jvar;
  943. { JVM exception catch description }
  944. tai_jcatch = class(tai)
  945. name: pshortstring;
  946. startlab,stoplab,handlerlab: tasmsymbol;
  947. constructor Create(const _name: shortstring; _startlab, _stoplab, _handlerlab: TAsmSymbol);
  948. destructor destroy;override;
  949. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  950. procedure ppuwrite(ppufile:tcompilerppufile);override;
  951. end;
  952. tai_jcatch_class = class of tai_jcatch;
  953. {$endif JVM}
  954. tai_symbolpair = class(tai)
  955. kind: TSymbolPairKind;
  956. sym,
  957. value: pshortstring;
  958. constructor create(akind: TSymbolPairKind; const asym, avalue: string);
  959. destructor destroy;override;
  960. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  961. procedure ppuwrite(ppufile:tcompilerppufile);override;
  962. end;
  963. teattrtyp = (eattrtype_none,eattrtype_dword,eattrtype_ntbs);
  964. tai_eabi_attribute = class(tai)
  965. eattr_typ : teattrtyp;
  966. tag,value : dword;
  967. valuestr : pstring;
  968. constructor create(atag,avalue : dword);
  969. constructor create(atag : dword;const avalue : string);
  970. destructor destroy;override;
  971. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  972. procedure ppuwrite(ppufile:tcompilerppufile);override;
  973. end;
  974. var
  975. { array with all class types for tais }
  976. aiclass : taiclassarray;
  977. { target specific tais, possibly overwritten in target specific aasmcpu }
  978. cai_align : tai_align_class = tai_align_abstract;
  979. cai_cpu : tai_cpu_class = tai_cpu_abstract;
  980. cai_seh_directive: tai_seh_directive_class = tai_seh_directive;
  981. { hook to notify uses of registers }
  982. add_reg_instruction_hook : tadd_reg_instruction_proc;
  983. procedure maybe_new_object_file(list:TAsmList);
  984. function new_section(list:TAsmList;Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default) : tai_section;
  985. function ppuloadai(ppufile:tcompilerppufile):tai;
  986. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  987. implementation
  988. uses
  989. {$ifdef x86}
  990. aasmcpu,
  991. {$endif x86}
  992. SysUtils,
  993. verbose,
  994. globals,
  995. ppu;
  996. const
  997. pputaimarker = 254;
  998. {****************************************************************************
  999. Helpers
  1000. ****************************************************************************}
  1001. procedure maybe_new_object_file(list:TAsmList);
  1002. begin
  1003. if create_smartlink_library then
  1004. list.concat(tai_cutobject.create);
  1005. end;
  1006. function new_section(list:TAsmList;Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default) : tai_section;
  1007. begin
  1008. Result:=tai_section.create(Asectype,Aname,Aalign,Asecorder);
  1009. list.concat(Result);
  1010. inc(list.section_count);
  1011. list.concat(cai_align.create(Aalign));
  1012. end;
  1013. function ppuloadai(ppufile:tcompilerppufile):tai;
  1014. var
  1015. b : byte;
  1016. t : taitype;
  1017. begin
  1018. { marker }
  1019. b:=ppufile.getbyte;
  1020. if b<>pputaimarker then
  1021. internalerror(200208181);
  1022. { load nodetype }
  1023. t:=taitype(ppufile.getbyte);
  1024. if t<>ait_none then
  1025. begin
  1026. if t>high(taitype) then
  1027. internalerror(200208182);
  1028. if not assigned(aiclass[t]) then
  1029. internalerror(200208183);
  1030. {writeln('taiload: ',taitypestr[t]);}
  1031. { generate tai of the correct class }
  1032. ppuloadai:=aiclass[t].ppuload(t,ppufile);
  1033. end
  1034. else
  1035. ppuloadai:=nil;
  1036. end;
  1037. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  1038. begin
  1039. { marker, read by ppuloadnode }
  1040. ppufile.putbyte(pputaimarker);
  1041. if assigned(n) then
  1042. begin
  1043. { type, read by ppuloadnode }
  1044. ppufile.putbyte(byte(n.typ));
  1045. {writeln('taiwrite: ',taitypestr[n.typ]);}
  1046. n.ppuwrite(ppufile);
  1047. end
  1048. else
  1049. ppufile.putbyte(byte(ait_none));
  1050. end;
  1051. constructor tai_symbolpair.create(akind: TSymbolPairKind; const asym, avalue: string);
  1052. begin
  1053. inherited create;
  1054. kind:=akind;
  1055. typ:=ait_symbolpair;
  1056. sym:=stringdup(asym);
  1057. value:=stringdup(avalue);
  1058. end;
  1059. destructor tai_symbolpair.destroy;
  1060. begin
  1061. stringdispose(sym);
  1062. stringdispose(value);
  1063. inherited destroy;
  1064. end;
  1065. constructor tai_symbolpair.ppuload(t: taitype; ppufile: tcompilerppufile);
  1066. begin
  1067. inherited ppuload(t,ppufile);
  1068. kind:=TSymbolPairKind(ppufile.getbyte);
  1069. sym:=ppufile.getpshortstring;
  1070. value:=ppufile.getpshortstring;
  1071. end;
  1072. procedure tai_symbolpair.ppuwrite(ppufile: tcompilerppufile);
  1073. begin
  1074. inherited ppuwrite(ppufile);
  1075. ppufile.putbyte(byte(kind));
  1076. ppufile.putstring(sym^);
  1077. ppufile.putstring(value^);
  1078. end;
  1079. constructor tai_varloc.create(sym: tsym; loc: tregister);
  1080. begin
  1081. inherited Create;
  1082. typ:=ait_varloc;
  1083. newlocation:=loc;
  1084. newlocationhi:=NR_NO;
  1085. varsym:=sym;
  1086. end;
  1087. constructor tai_varloc.create64(sym: tsym; loc, lochi: tregister);
  1088. begin
  1089. inherited Create;
  1090. typ:=ait_varloc;
  1091. newlocation:=loc;
  1092. newlocationhi:=lochi;
  1093. varsym:=sym;
  1094. end;
  1095. {$ifdef cpu64bitalu}
  1096. constructor tai_varloc.create128(sym: tsym; loc, lochi: tregister);
  1097. begin
  1098. inherited Create;
  1099. typ:=ait_varloc;
  1100. newlocation:=loc;
  1101. newlocationhi:=lochi;
  1102. varsym:=sym;
  1103. end;
  1104. {$endif cpu64bitalu}
  1105. constructor tai_varloc.ppuload(t: taitype; ppufile: tcompilerppufile);
  1106. begin
  1107. inherited ppuload(t, ppufile);
  1108. end;
  1109. procedure tai_varloc.ppuwrite(ppufile: tcompilerppufile);
  1110. begin
  1111. inherited ppuwrite(ppufile);
  1112. end;
  1113. procedure tai_varloc.buildderefimpl;
  1114. begin
  1115. inherited buildderefimpl;
  1116. end;
  1117. procedure tai_varloc.derefimpl;
  1118. begin
  1119. inherited derefimpl;
  1120. end;
  1121. {****************************************************************************
  1122. TAI
  1123. ****************************************************************************}
  1124. constructor tai.Create;
  1125. begin
  1126. {$ifndef NOOPT}
  1127. optinfo:=nil;
  1128. {$endif NOOPT}
  1129. end;
  1130. constructor tai.ppuload(t:taitype;ppufile:tcompilerppufile);
  1131. begin
  1132. typ:=t;
  1133. {$ifndef NOOPT}
  1134. optinfo:=nil;
  1135. {$endif}
  1136. end;
  1137. procedure tai.ppuwrite(ppufile:tcompilerppufile);
  1138. begin
  1139. end;
  1140. procedure tai.buildderefimpl;
  1141. begin
  1142. end;
  1143. procedure tai.derefimpl;
  1144. begin
  1145. end;
  1146. {****************************************************************************
  1147. TAILINEINFO
  1148. ****************************************************************************}
  1149. constructor tailineinfo.create;
  1150. begin
  1151. inherited create;
  1152. fileinfo:=current_filepos;
  1153. end;
  1154. constructor tailineinfo.ppuload(t:taitype;ppufile:tcompilerppufile);
  1155. begin
  1156. inherited ppuload(t,ppufile);
  1157. ppufile.getposinfo(fileinfo);
  1158. end;
  1159. procedure tailineinfo.ppuwrite(ppufile:tcompilerppufile);
  1160. begin
  1161. inherited ppuwrite(ppufile);
  1162. ppufile.putposinfo(fileinfo);
  1163. end;
  1164. {****************************************************************************
  1165. TAI_SIMPLE
  1166. ****************************************************************************}
  1167. constructor tai_simple.create(_typ : taitype);
  1168. begin
  1169. inherited create;
  1170. typ:=_typ;
  1171. end;
  1172. {****************************************************************************
  1173. TAI_SECTION
  1174. ****************************************************************************}
  1175. constructor tai_section.Create(Asectype:TAsmSectiontype;const Aname:string;Aalign:longint;Asecorder:TasmSectionorder=secorder_default);
  1176. begin
  1177. inherited Create;
  1178. typ:=ait_section;
  1179. sectype:=asectype;
  1180. secalign:=Aalign;
  1181. secorder:=Asecorder;
  1182. TObjData.sectiontype2progbitsandflags(sectype,secprogbits,secflags);
  1183. name:=stringdup(Aname);
  1184. sec:=nil;
  1185. end;
  1186. constructor tai_section.ppuload(t:taitype;ppufile:tcompilerppufile);
  1187. begin
  1188. inherited ppuload(t,ppufile);
  1189. sectype:=TAsmSectiontype(ppufile.getbyte);
  1190. secalign:=ppufile.getlongint;
  1191. name:=ppufile.getpshortstring;
  1192. ppufile.getset(tppuset1(secflags));
  1193. secprogbits:=TSectionProgbits(ppufile.getbyte);
  1194. sec:=nil;
  1195. end;
  1196. destructor tai_section.Destroy;
  1197. begin
  1198. stringdispose(name);
  1199. end;
  1200. procedure tai_section.ppuwrite(ppufile:tcompilerppufile);
  1201. begin
  1202. inherited ppuwrite(ppufile);
  1203. ppufile.putbyte(byte(sectype));
  1204. ppufile.putlongint(secalign);
  1205. ppufile.putstring(name^);
  1206. ppufile.putset(tppuset1(secflags));
  1207. ppufile.putbyte(byte(secprogbits));
  1208. end;
  1209. {****************************************************************************
  1210. TAI_DATABLOCK
  1211. ****************************************************************************}
  1212. constructor tai_datablock.Create(const _name : string;_size : asizeint; def: tdef; _typ:Tasmsymtype);
  1213. begin
  1214. inherited Create;
  1215. typ:=ait_datablock;
  1216. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,_typ,def);
  1217. { keep things aligned }
  1218. if _size<=0 then
  1219. _size:=sizeof(aint);
  1220. size:=_size;
  1221. is_global:=false;
  1222. end;
  1223. constructor tai_datablock.Create_hidden(const _name: string; _size: asizeint; def: tdef; _typ:Tasmsymtype);
  1224. begin
  1225. if tf_supports_hidden_symbols in target_info.flags then
  1226. begin
  1227. inherited Create;
  1228. typ:=ait_datablock;
  1229. sym:=current_asmdata.DefineAsmSymbol(_name,AB_PRIVATE_EXTERN,_typ,def);
  1230. { keep things aligned }
  1231. if _size<=0 then
  1232. _size:=sizeof(aint);
  1233. size:=_size;
  1234. is_global:=true;
  1235. end
  1236. else
  1237. Create(_name,_size,def,_typ);
  1238. end;
  1239. constructor tai_datablock.Create_global(const _name : string;_size : asizeint; def: tdef; _typ:Tasmsymtype);
  1240. begin
  1241. inherited Create;
  1242. typ:=ait_datablock;
  1243. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,_typ,def);
  1244. { keep things aligned }
  1245. if _size<=0 then
  1246. _size:=sizeof(aint);
  1247. size:=_size;
  1248. is_global:=true;
  1249. end;
  1250. constructor tai_datablock.ppuload(t:taitype;ppufile:tcompilerppufile);
  1251. begin
  1252. inherited Create;
  1253. sym:=ppufile.getasmsymbol;
  1254. size:=ppufile.getaint;
  1255. is_global:=ppufile.getboolean;
  1256. end;
  1257. procedure tai_datablock.ppuwrite(ppufile:tcompilerppufile);
  1258. begin
  1259. inherited ppuwrite(ppufile);
  1260. ppufile.putasmsymbol(sym);
  1261. ppufile.putaint(size);
  1262. ppufile.putboolean(is_global);
  1263. end;
  1264. procedure tai_datablock.derefimpl;
  1265. begin
  1266. end;
  1267. {****************************************************************************
  1268. TAI_SYMBOL
  1269. ****************************************************************************}
  1270. constructor tai_symbol.Create(_sym:tasmsymbol;siz:longint);
  1271. begin
  1272. inherited Create;
  1273. typ:=ait_symbol;
  1274. sym:=_sym;
  1275. size:=siz;
  1276. { don't redefine global/external symbols as local, as code to access
  1277. such symbols is different on some platforms }
  1278. if not(sym.bind in [AB_NONE,AB_LOCAL]) then
  1279. internalerror(2013081601);
  1280. sym.bind:=AB_LOCAL;
  1281. is_global:=false;
  1282. end;
  1283. constructor tai_symbol.Create_global(_sym:tasmsymbol;siz:longint);
  1284. begin
  1285. inherited Create;
  1286. typ:=ait_symbol;
  1287. sym:=_sym;
  1288. size:=siz;
  1289. { don't override PRIVATE_EXTERN with GLOBAL }
  1290. if not(sym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN]) then
  1291. sym.bind:=AB_GLOBAL;
  1292. is_global:=true;
  1293. end;
  1294. constructor tai_symbol.Create_Weak(_sym:tasmsymbol;siz:longint);
  1295. begin
  1296. inherited Create;
  1297. typ:=ait_symbol;
  1298. sym:=_sym;
  1299. size:=siz;
  1300. if not(sym.bind in [AB_NONE,AB_WEAK_EXTERNAL]) then
  1301. internalerror(2021092801);
  1302. sym.bind:=AB_WEAK;
  1303. is_global:=false;
  1304. end;
  1305. constructor tai_symbol.Createname(const _name : string;_symtyp:Tasmsymtype;siz:longint;def:tdef);
  1306. begin
  1307. inherited Create;
  1308. typ:=ait_symbol;
  1309. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,_symtyp,def);
  1310. size:=siz;
  1311. is_global:=false;
  1312. end;
  1313. constructor tai_symbol.Createname_global(const _name : string;_symtyp:Tasmsymtype;siz:longint;def:tdef);
  1314. begin
  1315. inherited Create;
  1316. typ:=ait_symbol;
  1317. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,_symtyp,def);
  1318. size:=siz;
  1319. is_global:=true;
  1320. end;
  1321. constructor tai_symbol.Createname_hidden(const _name: string; _symtyp: Tasmsymtype; siz: longint; def: tdef);
  1322. begin
  1323. if tf_supports_hidden_symbols in target_info.flags then
  1324. begin
  1325. inherited Create;
  1326. typ:=ait_symbol;
  1327. sym:=current_asmdata.DefineAsmSymbol(_name,AB_PRIVATE_EXTERN,_symtyp,def);
  1328. size:=siz;
  1329. is_global:=true;
  1330. end
  1331. else
  1332. Createname(_name, _symtyp, siz, def);
  1333. end;
  1334. constructor tai_symbol.createname_global_value(const _name: string;_symtyp: tasmsymtype; siz: longint; val: ptruint;def:tdef);
  1335. begin
  1336. Createname_global(_name,_symtyp,siz,def);
  1337. value:=val;
  1338. has_value:=true;
  1339. end;
  1340. constructor tai_symbol.ppuload(t:taitype;ppufile:tcompilerppufile);
  1341. begin
  1342. inherited ppuload(t,ppufile);
  1343. sym:=ppufile.getasmsymbol;
  1344. size:=ppufile.getlongint;
  1345. is_global:=ppufile.getboolean;
  1346. end;
  1347. procedure tai_symbol.ppuwrite(ppufile:tcompilerppufile);
  1348. begin
  1349. inherited ppuwrite(ppufile);
  1350. ppufile.putasmsymbol(sym);
  1351. ppufile.putlongint(size);
  1352. ppufile.putboolean(is_global);
  1353. end;
  1354. procedure tai_symbol.derefimpl;
  1355. begin
  1356. end;
  1357. {****************************************************************************
  1358. TAI_SYMBOL_END
  1359. ****************************************************************************}
  1360. constructor tai_symbol_end.Create(_sym:tasmsymbol);
  1361. begin
  1362. inherited Create;
  1363. typ:=ait_symbol_end;
  1364. sym:=_sym;
  1365. end;
  1366. constructor tai_symbol_end.Createname(const _name : string);
  1367. begin
  1368. inherited Create;
  1369. typ:=ait_symbol_end;
  1370. sym:=current_asmdata.GetAsmSymbol(_name);
  1371. if not assigned(sym) then
  1372. internalerror(2013080301);
  1373. end;
  1374. constructor tai_symbol_end.ppuload(t:taitype;ppufile:tcompilerppufile);
  1375. begin
  1376. inherited ppuload(t,ppufile);
  1377. sym:=ppufile.getasmsymbol;
  1378. end;
  1379. procedure tai_symbol_end.ppuwrite(ppufile:tcompilerppufile);
  1380. begin
  1381. inherited ppuwrite(ppufile);
  1382. ppufile.putasmsymbol(sym);
  1383. end;
  1384. procedure tai_symbol_end.derefimpl;
  1385. begin
  1386. end;
  1387. {****************************************************************************
  1388. TAI_SYMBOL_END
  1389. ****************************************************************************}
  1390. constructor tai_directive.Create(_directive:TAsmDirective;const _name:ansistring);
  1391. begin
  1392. inherited Create;
  1393. typ:=ait_directive;
  1394. name:=_name;
  1395. directive:=_directive;
  1396. end;
  1397. constructor tai_directive.ppuload(t:taitype;ppufile:tcompilerppufile);
  1398. begin
  1399. inherited ppuload(t,ppufile);
  1400. name:=ppufile.getansistring;
  1401. directive:=TAsmDirective(ppufile.getbyte);
  1402. end;
  1403. procedure tai_directive.ppuwrite(ppufile:tcompilerppufile);
  1404. begin
  1405. inherited ppuwrite(ppufile);
  1406. ppufile.putansistring(name);
  1407. ppufile.putbyte(byte(directive));
  1408. end;
  1409. {****************************************************************************
  1410. TAI_CONST
  1411. ****************************************************************************}
  1412. constructor tai_const.Create(_typ:taiconst_type;_value : int64);
  1413. begin
  1414. inherited Create;
  1415. typ:=ait_const;
  1416. consttype:=_typ;
  1417. value:=_value;
  1418. sym:=nil;
  1419. endsym:=nil;
  1420. end;
  1421. constructor tai_const.Create_128bit(_value : int64);
  1422. begin
  1423. inherited Create;
  1424. typ:=ait_const;
  1425. consttype:=aitconst_128bit;
  1426. value:=_value;
  1427. sym:=nil;
  1428. endsym:=nil;
  1429. end;
  1430. constructor tai_const.Create_64bit(_value : int64);
  1431. begin
  1432. inherited Create;
  1433. typ:=ait_const;
  1434. consttype:=aitconst_64bit;
  1435. value:=_value;
  1436. sym:=nil;
  1437. endsym:=nil;
  1438. end;
  1439. constructor tai_const.Create_32bit(_value : longint);
  1440. begin
  1441. inherited Create;
  1442. typ:=ait_const;
  1443. consttype:=aitconst_32bit;
  1444. value:=_value;
  1445. sym:=nil;
  1446. endsym:=nil;
  1447. end;
  1448. constructor tai_const.Create_16bit(_value : word);
  1449. begin
  1450. inherited Create;
  1451. typ:=ait_const;
  1452. consttype:=aitconst_16bit;
  1453. value:=_value;
  1454. sym:=nil;
  1455. endsym:=nil;
  1456. end;
  1457. constructor tai_const.Create_64bit_unaligned(_value : int64);
  1458. begin
  1459. inherited Create;
  1460. typ:=ait_const;
  1461. consttype:=aitconst_64bit_unaligned;
  1462. value:=_value;
  1463. sym:=nil;
  1464. endsym:=nil;
  1465. end;
  1466. constructor tai_const.Create_32bit_unaligned(_value : longint);
  1467. begin
  1468. inherited Create;
  1469. typ:=ait_const;
  1470. consttype:=aitconst_32bit_unaligned;
  1471. value:=_value;
  1472. sym:=nil;
  1473. endsym:=nil;
  1474. end;
  1475. constructor tai_const.Create_16bit_unaligned(_value : word);
  1476. begin
  1477. inherited Create;
  1478. typ:=ait_const;
  1479. consttype:=aitconst_16bit_unaligned;
  1480. value:=_value;
  1481. sym:=nil;
  1482. endsym:=nil;
  1483. end;
  1484. constructor tai_const.Create_8bit(_value : byte);
  1485. begin
  1486. inherited Create;
  1487. typ:=ait_const;
  1488. consttype:=aitconst_8bit;
  1489. value:=_value;
  1490. sym:=nil;
  1491. endsym:=nil;
  1492. end;
  1493. constructor tai_const.Create_char(size: integer; _value: dword);
  1494. begin
  1495. inherited Create;
  1496. typ:=ait_const;
  1497. case size of
  1498. 1:
  1499. begin
  1500. consttype:=aitconst_8bit;
  1501. value:=byte(_value)
  1502. end;
  1503. 2:
  1504. begin
  1505. consttype:=aitconst_16bit;
  1506. value:=word(_value)
  1507. end
  1508. else
  1509. InternalError(2010030701)
  1510. end
  1511. end;
  1512. constructor tai_const.Create_sleb128bit(_value : int64);
  1513. begin
  1514. inherited Create;
  1515. typ:=ait_const;
  1516. consttype:=aitconst_sleb128bit;
  1517. value:=_value;
  1518. sym:=nil;
  1519. endsym:=nil;
  1520. end;
  1521. constructor tai_const.Create_uleb128bit(_value : qword);
  1522. begin
  1523. inherited Create;
  1524. typ:=ait_const;
  1525. consttype:=aitconst_uleb128bit;
  1526. value:=int64(_value);
  1527. sym:=nil;
  1528. endsym:=nil;
  1529. end;
  1530. constructor tai_const.Create_aint(_value : aint);
  1531. begin
  1532. inherited Create;
  1533. typ:=ait_const;
  1534. consttype:=aitconst_aint;
  1535. value:=_value;
  1536. sym:=nil;
  1537. endsym:=nil;
  1538. end;
  1539. constructor tai_const.Create_sizeint(_value : asizeint);
  1540. begin
  1541. inherited Create;
  1542. typ:=ait_const;
  1543. consttype:=aitconst_sizeint;
  1544. value:=_value;
  1545. sym:=nil;
  1546. endsym:=nil;
  1547. end;
  1548. constructor tai_const.Create_sizeint_unaligned(_value : asizeint);
  1549. begin
  1550. inherited Create;
  1551. typ:=ait_const;
  1552. consttype:=aitconst_sizeint_unaligned;
  1553. value:=_value;
  1554. sym:=nil;
  1555. endsym:=nil;
  1556. end;
  1557. constructor tai_const.Create_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  1558. begin
  1559. inherited Create;
  1560. typ:=ait_const;
  1561. consttype:=_typ;
  1562. sym:=_sym;
  1563. endsym:=nil;
  1564. value:=0;
  1565. { update sym info }
  1566. if assigned(sym) then
  1567. sym.increfs;
  1568. end;
  1569. constructor tai_const.Create_sym(_sym:tasmsymbol);
  1570. begin
  1571. self.create_sym_offset(_sym,0);
  1572. end;
  1573. {$ifdef i8086}
  1574. constructor tai_const.Create_sym_near(_sym: tasmsymbol);
  1575. begin
  1576. self.create_sym(_sym);
  1577. consttype:=aitconst_ptr;
  1578. end;
  1579. constructor tai_const.Create_sym_far(_sym: tasmsymbol);
  1580. begin
  1581. self.create_sym(_sym);
  1582. consttype:=aitconst_farptr;
  1583. end;
  1584. constructor tai_const.Createname_near(const name:string;ofs:asizeint);
  1585. begin
  1586. self.Createname(name,ofs);
  1587. consttype:=aitconst_ptr;
  1588. end;
  1589. constructor tai_const.Createname_far(const name:string;ofs:asizeint);
  1590. begin
  1591. self.Createname(name,ofs);
  1592. consttype:=aitconst_farptr;
  1593. end;
  1594. constructor tai_const.Createname_near(const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  1595. begin
  1596. self.Createname(name,_symtyp,ofs);
  1597. consttype:=aitconst_ptr;
  1598. end;
  1599. constructor tai_const.Createname_far(const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  1600. begin
  1601. self.Createname(name,_symtyp,ofs);
  1602. consttype:=aitconst_farptr;
  1603. end;
  1604. {$endif i8086}
  1605. constructor tai_const.Create_sym_offset(_sym:tasmsymbol;ofs:asizeint);
  1606. begin
  1607. inherited Create;
  1608. typ:=ait_const;
  1609. {$ifdef i8086}
  1610. if assigned(_sym) and (_sym.typ=AT_DATA) then
  1611. begin
  1612. if current_settings.x86memorymodel in x86_far_data_models then
  1613. consttype:=aitconst_farptr
  1614. else
  1615. consttype:=aitconst_ptr;
  1616. end
  1617. else
  1618. begin
  1619. if current_settings.x86memorymodel in x86_far_code_models then
  1620. consttype:=aitconst_farptr
  1621. else
  1622. consttype:=aitconst_ptr;
  1623. end;
  1624. {$else i8086}
  1625. {$ifdef avr}
  1626. if assigned(_sym) and (_sym.typ=AT_FUNCTION) then
  1627. consttype:=aitconst_gs
  1628. else
  1629. {$endif avr}
  1630. consttype:=aitconst_ptr;
  1631. {$endif i8086}
  1632. { sym is allowed to be nil, this is used to write nil pointers }
  1633. sym:=_sym;
  1634. endsym:=nil;
  1635. { store the original offset in symofs so that we can recalculate the
  1636. value field in the assembler }
  1637. symofs:=ofs;
  1638. value:=ofs;
  1639. { update sym info }
  1640. if assigned(sym) then
  1641. sym.increfs;
  1642. end;
  1643. constructor tai_const.Create_type_sym_offset(_typ : taiconst_type;_sym : tasmsymbol; ofs : asizeint);
  1644. begin
  1645. inherited Create;
  1646. typ:=ait_const;
  1647. consttype:=_typ;
  1648. { sym is allowed to be nil, this is used to write nil pointers }
  1649. sym:=_sym;
  1650. endsym:=nil;
  1651. { store the original offset in symofs so that we can recalculate the
  1652. value field in the assembler }
  1653. symofs:=ofs;
  1654. value:=ofs;
  1655. { update sym info }
  1656. if assigned(sym) then
  1657. sym.increfs;
  1658. end;
  1659. constructor tai_const.Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  1660. begin
  1661. self.create_sym_offset(_sym,0);
  1662. consttype:=_typ;
  1663. endsym:=_endsym;
  1664. endsym.increfs;
  1665. end;
  1666. constructor tai_const.Create_rel_sym_offset(_typ: taiconst_type; _sym, _endsym: tasmsymbol; _ofs: int64);
  1667. begin
  1668. self.create_sym_offset(_sym,_ofs);
  1669. consttype:=_typ;
  1670. endsym:=_endsym;
  1671. endsym.increfs;
  1672. end;
  1673. constructor tai_const.Create_rva_sym(_sym:tasmsymbol);
  1674. begin
  1675. self.create_sym_offset(_sym,0);
  1676. consttype:=aitconst_rva_symbol;
  1677. end;
  1678. constructor tai_const.Createname(const name:string;ofs:asizeint);
  1679. begin
  1680. self.Createname(name,AT_NONE,ofs);
  1681. end;
  1682. constructor tai_const.Createname(const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  1683. begin
  1684. self.create_sym_offset(current_asmdata.RefAsmSymbol(name,_symtyp),ofs);
  1685. end;
  1686. constructor tai_const.Createname_rel(const name,endname:string);
  1687. begin
  1688. self.create_sym_offset(current_asmdata.RefAsmSymbol(name,AT_NONE),0);
  1689. endsym:=current_asmdata.RefAsmSymbol(endname,AT_NONE)
  1690. end;
  1691. constructor tai_const.Create_type_name(_typ:taiconst_type;const name:string;ofs:asizeint);
  1692. begin
  1693. self.Create_type_name(_typ,name,AT_NONE,ofs);
  1694. end;
  1695. constructor tai_const.Create_type_name(_typ:taiconst_type;const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  1696. begin
  1697. self.create_sym_offset(current_asmdata.RefAsmSymbol(name,_symtyp),ofs);
  1698. consttype:=_typ;
  1699. end;
  1700. constructor tai_const.Create_nil_codeptr;
  1701. begin
  1702. self.Create_int_codeptr(0);
  1703. end;
  1704. constructor tai_const.Create_nil_codeptr_unaligned;
  1705. begin
  1706. self.Create_int_codeptr_unaligned(0);
  1707. end;
  1708. constructor tai_const.Create_nil_dataptr;
  1709. begin
  1710. self.Create_int_dataptr(0);
  1711. end;
  1712. constructor tai_const.Create_nil_dataptr_unaligned;
  1713. begin
  1714. self.Create_int_dataptr_unaligned(0);
  1715. end;
  1716. constructor tai_const.Create_int_codeptr(_value: int64);
  1717. begin
  1718. inherited Create;
  1719. typ:=ait_const;
  1720. {$ifdef i8086}
  1721. if current_settings.x86memorymodel in x86_far_code_models then
  1722. consttype:=aitconst_farptr
  1723. else
  1724. {$endif i8086}
  1725. {$ifdef avr}
  1726. consttype:=aitconst_gs;
  1727. {$else avr}
  1728. consttype:=aitconst_ptr;
  1729. {$endif avr}
  1730. sym:=nil;
  1731. endsym:=nil;
  1732. symofs:=0;
  1733. value:=_value;
  1734. end;
  1735. constructor tai_const.Create_int_codeptr_unaligned(_value: int64);
  1736. begin
  1737. inherited Create;
  1738. typ:=ait_const;
  1739. {$ifdef i8086}
  1740. if current_settings.x86memorymodel in x86_far_code_models then
  1741. consttype:=aitconst_farptr
  1742. else
  1743. {$endif i8086}
  1744. {$ifdef avr}
  1745. consttype:=aitconst_gs;
  1746. {$else avr}
  1747. consttype:=aitconst_ptr_unaligned;
  1748. {$endif avr}
  1749. sym:=nil;
  1750. endsym:=nil;
  1751. symofs:=0;
  1752. value:=_value;
  1753. end;
  1754. constructor tai_const.Create_int_dataptr(_value: int64);
  1755. begin
  1756. inherited Create;
  1757. typ:=ait_const;
  1758. {$ifdef i8086}
  1759. if current_settings.x86memorymodel in x86_far_data_models then
  1760. consttype:=aitconst_farptr
  1761. else
  1762. {$endif i8086}
  1763. consttype:=aitconst_ptr;
  1764. sym:=nil;
  1765. endsym:=nil;
  1766. symofs:=0;
  1767. value:=_value;
  1768. end;
  1769. constructor tai_const.Create_int_dataptr_unaligned(_value: int64);
  1770. begin
  1771. inherited Create;
  1772. typ:=ait_const;
  1773. {$ifdef i8086}
  1774. if current_settings.x86memorymodel in x86_far_data_models then
  1775. consttype:=aitconst_farptr
  1776. else
  1777. {$endif i8086}
  1778. consttype:=aitconst_ptr_unaligned;
  1779. sym:=nil;
  1780. endsym:=nil;
  1781. symofs:=0;
  1782. value:=_value;
  1783. end;
  1784. {$ifdef avr}
  1785. constructor tai_const.Create_int_dataptr_unaligned(_value: int64;
  1786. size: taiconst_type);
  1787. begin
  1788. inherited Create;
  1789. typ:=ait_const;
  1790. consttype:=size;
  1791. sym:=nil;
  1792. endsym:=nil;
  1793. symofs:=0;
  1794. value:=_value;
  1795. end;
  1796. {$endif avr}
  1797. {$ifdef i8086}
  1798. constructor tai_const.Create_seg_name(const name:string);
  1799. begin
  1800. self.Createname(name,0);
  1801. self.consttype:=aitconst_seg;
  1802. end;
  1803. constructor tai_const.Create_dgroup;
  1804. begin
  1805. self.Create_16bit(0);
  1806. self.consttype:=aitconst_dgroup;
  1807. end;
  1808. constructor tai_const.Create_fardataseg;
  1809. begin
  1810. self.Create_16bit(0);
  1811. self.consttype:=aitconst_fardataseg;
  1812. end;
  1813. {$endif i8086}
  1814. constructor tai_const.ppuload(t:taitype;ppufile:tcompilerppufile);
  1815. begin
  1816. inherited ppuload(t,ppufile);
  1817. consttype:=taiconst_type(ppufile.getbyte);
  1818. sym:=ppufile.getasmsymbol;
  1819. endsym:=ppufile.getasmsymbol;
  1820. value:=ppufile.getint64;
  1821. end;
  1822. procedure tai_const.ppuwrite(ppufile:tcompilerppufile);
  1823. begin
  1824. inherited ppuwrite(ppufile);
  1825. ppufile.putbyte(byte(consttype));
  1826. ppufile.putasmsymbol(sym);
  1827. ppufile.putasmsymbol(endsym);
  1828. ppufile.putint64(value);
  1829. end;
  1830. procedure tai_const.derefimpl;
  1831. begin
  1832. end;
  1833. function tai_const.getcopy:tlinkedlistitem;
  1834. begin
  1835. getcopy:=inherited getcopy;
  1836. { we need to increase the reference number }
  1837. if assigned(sym) then
  1838. sym.increfs;
  1839. if assigned(endsym) then
  1840. endsym.increfs;
  1841. end;
  1842. function tai_const.size:longint;
  1843. begin
  1844. case consttype of
  1845. aitconst_8bit :
  1846. result:=1;
  1847. aitconst_16bit,aitconst_16bit_unaligned :
  1848. result:=2;
  1849. aitconst_32bit,aitconst_darwin_dwarf_delta32,
  1850. aitconst_32bit_unaligned:
  1851. result:=4;
  1852. aitconst_64bit,aitconst_darwin_dwarf_delta64,
  1853. aitconst_64bit_unaligned:
  1854. result:=8;
  1855. aitconst_secrel32_symbol,
  1856. aitconst_rva_symbol :
  1857. if target_info.system in systems_peoptplus then
  1858. result:=sizeof(longint)
  1859. else
  1860. result:=sizeof(pint);
  1861. aitconst_uleb128bit :
  1862. begin
  1863. if fixed_size>0 then
  1864. result:=fixed_size
  1865. else if sym=nil then
  1866. begin
  1867. FixSize;
  1868. result:=fixed_size;
  1869. end
  1870. else
  1871. { worst case }
  1872. result:=sizeof(pint)+2;
  1873. end;
  1874. aitconst_sleb128bit :
  1875. begin
  1876. if fixed_size>0 then
  1877. result:=fixed_size
  1878. else if sym=nil then
  1879. begin
  1880. FixSize;
  1881. result:=fixed_size;
  1882. end
  1883. else
  1884. { worst case }
  1885. result:=sizeof(pint)+2;
  1886. end;
  1887. aitconst_half16bit,
  1888. aitconst_gs:
  1889. result:=2;
  1890. aitconst_farptr:
  1891. result:=4;
  1892. aitconst_dgroup,
  1893. aitconst_fardataseg,
  1894. aitconst_seg:
  1895. result:=2;
  1896. aitconst_got:
  1897. result:=sizeof(pint);
  1898. aitconst_gotoff_symbol:
  1899. result:=4;
  1900. aitconst_gottpoff:
  1901. result:=4;
  1902. aitconst_tlsgd:
  1903. result:=4;
  1904. aitconst_tpoff:
  1905. result:=4;
  1906. aitconst_tlsdesc:
  1907. result:=4;
  1908. aitconst_dtpoff:
  1909. result:=4;
  1910. else
  1911. internalerror(200603253);
  1912. end;
  1913. end;
  1914. procedure tai_const.FixSize;
  1915. begin
  1916. case consttype of
  1917. aitconst_uleb128bit:
  1918. fixed_size:=LengthUleb128(qword(value));
  1919. aitconst_sleb128bit:
  1920. fixed_size:=LengthSleb128(value);
  1921. else
  1922. Internalerror(2019030301);
  1923. end;
  1924. end;
  1925. {****************************************************************************
  1926. TAI_realconst
  1927. ****************************************************************************}
  1928. constructor tai_realconst.create_s32real(val: ts32real);
  1929. begin
  1930. inherited create;
  1931. typ:=ait_realconst;
  1932. realtyp:=aitrealconst_s32bit;
  1933. savesize:=4;
  1934. value.s32val:=val;
  1935. end;
  1936. constructor tai_realconst.create_s64real(val: ts64real);
  1937. begin
  1938. inherited create;
  1939. typ:=ait_realconst;
  1940. realtyp:=aitrealconst_s64bit;
  1941. savesize:=8;
  1942. value.s64val:=val;
  1943. end;
  1944. {$ifdef ARM}
  1945. constructor tai_realconst.create_s64real_hiloswapped(val : ts64real);
  1946. begin
  1947. inherited create;
  1948. typ:=ait_realconst;
  1949. realtyp:=aitrealconst_s64bit;
  1950. value.s64val:=val;
  1951. savesize:=8;
  1952. formatoptions:=fo_hiloswapped;
  1953. end;
  1954. {$endif ARM}
  1955. constructor tai_realconst.create_s80real(val: ts80real; _savesize: byte);
  1956. begin
  1957. inherited create;
  1958. typ:=ait_realconst;
  1959. realtyp:=aitrealconst_s80bit;
  1960. savesize:=_savesize;
  1961. value.s80val:=val;
  1962. end;
  1963. constructor tai_realconst.create_s128real(val: ts128real);
  1964. begin
  1965. inherited create;
  1966. typ:=ait_realconst;
  1967. realtyp:=aitrealconst_s128bit;
  1968. savesize:=16;
  1969. value.s128val:=val;
  1970. end;
  1971. constructor tai_realconst.create_s64compreal(val: ts64comp);
  1972. begin
  1973. inherited create;
  1974. typ:=ait_realconst;
  1975. realtyp:=aitrealconst_s64comp;
  1976. savesize:=8;
  1977. value.s64compval:=val;
  1978. end;
  1979. constructor tai_realconst.ppuload(t: taitype; ppufile: tcompilerppufile);
  1980. begin
  1981. inherited;
  1982. realtyp:=tairealconsttype(ppufile.getbyte);
  1983. {$ifdef ARM}
  1984. formatoptions:=tformatoptions(ppufile.getbyte);
  1985. {$endif ARM}
  1986. case realtyp of
  1987. aitrealconst_s32bit:
  1988. value.s32val:=ppufile.getreal;
  1989. aitrealconst_s64bit:
  1990. value.s64val:=ppufile.getreal;
  1991. aitrealconst_s80bit:
  1992. value.s80val:=ppufile.getreal;
  1993. aitrealconst_s128bit:
  1994. value.s128val:=ppufile.getreal;
  1995. aitrealconst_s64comp:
  1996. value.s64compval:=comp(ppufile.getint64);
  1997. end;
  1998. end;
  1999. procedure tai_realconst.ppuwrite(ppufile: tcompilerppufile);
  2000. var
  2001. c: comp;
  2002. begin
  2003. inherited ppuwrite(ppufile);
  2004. ppufile.putbyte(byte(realtyp));
  2005. {$ifdef ARM}
  2006. ppufile.putbyte(byte(formatoptions));
  2007. {$endif ARM}
  2008. case realtyp of
  2009. aitrealconst_s32bit:
  2010. ppufile.putreal(value.s32val);
  2011. aitrealconst_s64bit:
  2012. ppufile.putreal(value.s64val);
  2013. aitrealconst_s80bit:
  2014. ppufile.putreal(value.s80val);
  2015. aitrealconst_s128bit:
  2016. ppufile.putreal(value.s128val);
  2017. aitrealconst_s64comp:
  2018. begin
  2019. c:=comp(value.s64compval);
  2020. ppufile.putint64(int64(c));
  2021. end
  2022. end;
  2023. end;
  2024. function tai_realconst.getcopy: tlinkedlistitem;
  2025. begin
  2026. result:=inherited getcopy;
  2027. tai_realconst(result).value:=value;
  2028. tai_realconst(result).realtyp:=realtyp;
  2029. tai_realconst(result).savesize:=savesize;
  2030. {$ifdef ARM}
  2031. tai_realconst(result).formatoptions:=formatoptions;
  2032. {$endif ARM}
  2033. end;
  2034. function tai_realconst.datasize: word;
  2035. begin
  2036. result:=0;
  2037. case realtyp of
  2038. aitrealconst_s32bit:
  2039. result:=4;
  2040. aitrealconst_s64bit,
  2041. aitrealconst_s64comp:
  2042. result:=8;
  2043. aitrealconst_s80bit:
  2044. result:=10;
  2045. aitrealconst_s128bit:
  2046. result:=16;
  2047. end;
  2048. end;
  2049. {****************************************************************************
  2050. TAI_STRING
  2051. ****************************************************************************}
  2052. constructor tai_string.Create(const _str : string);
  2053. begin
  2054. inherited Create;
  2055. typ:=ait_string;
  2056. len:=length(_str);
  2057. getmem(str,len+1);
  2058. move(_str[1],str^,len);
  2059. str[len]:=#0;
  2060. end;
  2061. constructor tai_string.Create_pchar(_str : pchar;length : longint);
  2062. begin
  2063. inherited Create;
  2064. typ:=ait_string;
  2065. str:=_str;
  2066. len:=length;
  2067. end;
  2068. destructor tai_string.destroy;
  2069. begin
  2070. if str<>nil then
  2071. freemem(str);
  2072. inherited Destroy;
  2073. end;
  2074. constructor tai_string.ppuload(t:taitype;ppufile:tcompilerppufile);
  2075. begin
  2076. inherited ppuload(t,ppufile);
  2077. len:=ppufile.getlongint;
  2078. getmem(str,len+1);
  2079. ppufile.getdata(str^,len);
  2080. str[len]:=#0
  2081. end;
  2082. procedure tai_string.ppuwrite(ppufile:tcompilerppufile);
  2083. begin
  2084. inherited ppuwrite(ppufile);
  2085. ppufile.putlongint(len);
  2086. ppufile.putdata(str^,len);
  2087. end;
  2088. function tai_string.getcopy : tlinkedlistitem;
  2089. var
  2090. p : tlinkedlistitem;
  2091. begin
  2092. p:=inherited getcopy;
  2093. getmem(tai_string(p).str,len);
  2094. move(str^,tai_string(p).str^,len);
  2095. getcopy:=p;
  2096. end;
  2097. {****************************************************************************
  2098. TAI_LABEL
  2099. ****************************************************************************}
  2100. constructor tai_label.Create(_labsym : tasmlabel);
  2101. begin
  2102. inherited Create;
  2103. typ:=ait_label;
  2104. labsym:=_labsym;
  2105. labsym.is_set:=true;
  2106. end;
  2107. constructor tai_label.ppuload(t:taitype;ppufile:tcompilerppufile);
  2108. begin
  2109. inherited ppuload(t,ppufile);
  2110. labsym:=tasmlabel(ppufile.getasmsymbol);
  2111. ppufile.getbyte; { was is_global flag, now unused }
  2112. end;
  2113. procedure tai_label.ppuwrite(ppufile:tcompilerppufile);
  2114. begin
  2115. inherited ppuwrite(ppufile);
  2116. ppufile.putasmsymbol(labsym);
  2117. ppufile.putbyte(0); { was is_global flag, now unused }
  2118. end;
  2119. procedure tai_label.derefimpl;
  2120. begin
  2121. labsym.is_set:=true;
  2122. end;
  2123. {****************************************************************************
  2124. tai_comment comment to be inserted in the assembler file
  2125. ****************************************************************************}
  2126. constructor tai_comment.Create(_str : pchar);
  2127. begin
  2128. inherited Create;
  2129. typ:=ait_comment;
  2130. str:=_str;
  2131. end;
  2132. destructor tai_comment.destroy;
  2133. begin
  2134. freemem(str);
  2135. inherited Destroy;
  2136. end;
  2137. constructor tai_comment.ppuload(t:taitype;ppufile:tcompilerppufile);
  2138. var
  2139. len : longint;
  2140. begin
  2141. inherited ppuload(t,ppufile);
  2142. len:=ppufile.getlongint;
  2143. getmem(str,len+1);
  2144. ppufile.getdata(str^,len);
  2145. str[len]:=#0;
  2146. end;
  2147. procedure tai_comment.ppuwrite(ppufile:tcompilerppufile);
  2148. var
  2149. len : longint;
  2150. begin
  2151. inherited ppuwrite(ppufile);
  2152. len:=strlen(str);
  2153. ppufile.putlongint(len);
  2154. ppufile.putdata(str^,len);
  2155. end;
  2156. function tai_comment.getcopy : tlinkedlistitem;
  2157. var
  2158. p : tlinkedlistitem;
  2159. begin
  2160. p:=inherited getcopy;
  2161. getmem(tai_comment(p).str,strlen(str)+1);
  2162. move(str^,tai_comment(p).str^,strlen(str)+1);
  2163. getcopy:=p;
  2164. end;
  2165. {****************************************************************************
  2166. TAI_STABS
  2167. ****************************************************************************}
  2168. constructor tai_stab.create(_stabtype:TStabType;_str : pchar);
  2169. begin
  2170. inherited create;
  2171. typ:=ait_stab;
  2172. str:=_str;
  2173. stabtype:=_stabtype;
  2174. end;
  2175. constructor tai_stab.create_str(_stabtype:TStabType;const s:string);
  2176. begin
  2177. self.create(_stabtype,strpnew(s));
  2178. end;
  2179. constructor tai_stab.create_ansistr(_stabtype:TStabType;const s:ansistring);
  2180. begin
  2181. inherited create;
  2182. typ:=ait_stab;
  2183. stabtype:=_stabtype;
  2184. getmem(str,length(s)+1);
  2185. if length(s)>0 then
  2186. move(s[1],str^,length(s)+1)
  2187. else
  2188. str^:=#0;
  2189. end;
  2190. destructor tai_stab.destroy;
  2191. begin
  2192. freemem(str);
  2193. inherited destroy;
  2194. end;
  2195. {****************************************************************************
  2196. TAI_FORCE_LINE
  2197. ****************************************************************************}
  2198. constructor tai_force_line.create;
  2199. begin
  2200. inherited create;
  2201. typ:=ait_force_line;
  2202. end;
  2203. {****************************************************************************
  2204. TAI_FUNCTION_NAME
  2205. ****************************************************************************}
  2206. constructor tai_function_name.create(const s:string);
  2207. begin
  2208. inherited create;
  2209. typ:=ait_function_name;
  2210. funcname:=stringdup(s);
  2211. end;
  2212. destructor tai_function_name.destroy;
  2213. begin
  2214. stringdispose(funcname);
  2215. inherited destroy;
  2216. end;
  2217. {****************************************************************************
  2218. TAI_CUTOBJECT
  2219. ****************************************************************************}
  2220. constructor tai_cutobject.Create;
  2221. begin
  2222. inherited Create;
  2223. typ:=ait_cutobject;
  2224. place:=cut_normal;
  2225. end;
  2226. constructor tai_cutobject.Create_begin;
  2227. begin
  2228. inherited Create;
  2229. typ:=ait_cutobject;
  2230. place:=cut_begin;
  2231. end;
  2232. constructor tai_cutobject.Create_end;
  2233. begin
  2234. inherited Create;
  2235. typ:=ait_cutobject;
  2236. place:=cut_end;
  2237. end;
  2238. constructor tai_cutobject.ppuload(t:taitype;ppufile:tcompilerppufile);
  2239. begin
  2240. inherited ppuload(t,ppufile);
  2241. place:=TCutPlace(ppufile.getbyte);
  2242. end;
  2243. procedure tai_cutobject.ppuwrite(ppufile:tcompilerppufile);
  2244. begin
  2245. inherited ppuwrite(ppufile);
  2246. ppufile.putbyte(byte(place));
  2247. end;
  2248. {****************************************************************************
  2249. Tai_Marker
  2250. ****************************************************************************}
  2251. constructor Tai_Marker.Create(_Kind: TAsmMarker);
  2252. begin
  2253. Inherited Create;
  2254. typ := ait_marker;
  2255. Kind := _Kind;
  2256. end;
  2257. constructor Tai_Marker.ppuload(t:taitype;ppufile:tcompilerppufile);
  2258. begin
  2259. inherited ppuload(t,ppufile);
  2260. kind:=TAsmMarker(ppufile.getbyte);
  2261. end;
  2262. procedure Tai_Marker.ppuwrite(ppufile:tcompilerppufile);
  2263. begin
  2264. inherited ppuwrite(ppufile);
  2265. ppufile.putbyte(byte(kind));
  2266. end;
  2267. {*****************************************************************************
  2268. tai_tempalloc
  2269. *****************************************************************************}
  2270. constructor tai_tempalloc.alloc(pos,size:longint);
  2271. begin
  2272. inherited Create;
  2273. typ:=ait_tempalloc;
  2274. allocation:=true;
  2275. temppos:=pos;
  2276. tempsize:=size;
  2277. {$ifdef EXTDEBUG}
  2278. problem:=nil;
  2279. {$endif EXTDEBUG}
  2280. end;
  2281. destructor tai_tempalloc.destroy;
  2282. begin
  2283. {$ifdef EXTDEBUG}
  2284. stringdispose(problem);
  2285. {$endif EXTDEBUG}
  2286. inherited destroy;
  2287. end;
  2288. constructor tai_tempalloc.dealloc(pos,size:longint);
  2289. begin
  2290. inherited Create;
  2291. typ:=ait_tempalloc;
  2292. allocation:=false;
  2293. temppos:=pos;
  2294. tempsize:=size;
  2295. {$ifdef EXTDEBUG}
  2296. problem:=nil;
  2297. {$endif EXTDEBUG}
  2298. end;
  2299. {$ifdef EXTDEBUG}
  2300. constructor tai_tempalloc.allocinfo(pos,size:longint;const st:string);
  2301. begin
  2302. inherited Create;
  2303. typ:=ait_tempalloc;
  2304. allocation:=false;
  2305. temppos:=pos;
  2306. tempsize:=size;
  2307. problem:=stringdup(st);
  2308. end;
  2309. {$endif EXTDEBUG}
  2310. constructor tai_tempalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  2311. begin
  2312. inherited ppuload(t,ppufile);
  2313. temppos:=ppufile.getlongint;
  2314. tempsize:=ppufile.getlongint;
  2315. allocation:=ppufile.getboolean;
  2316. {$ifdef EXTDEBUG}
  2317. problem:=nil;
  2318. {$endif EXTDEBUG}
  2319. end;
  2320. procedure tai_tempalloc.ppuwrite(ppufile:tcompilerppufile);
  2321. begin
  2322. inherited ppuwrite(ppufile);
  2323. ppufile.putlongint(temppos);
  2324. ppufile.putlongint(tempsize);
  2325. ppufile.putboolean(allocation);
  2326. end;
  2327. {*****************************************************************************
  2328. tai_regalloc
  2329. *****************************************************************************}
  2330. constructor tai_regalloc.alloc(r : tregister;ainstr:tai);
  2331. begin
  2332. inherited create;
  2333. typ:=ait_regalloc;
  2334. ratype:=ra_alloc;
  2335. reg:=r;
  2336. { ainstr must be an instruction }
  2337. if assigned(ainstr) and
  2338. (ainstr.typ<>ait_instruction) then
  2339. internalerror(200411011);
  2340. instr:=ainstr;
  2341. end;
  2342. constructor tai_regalloc.dealloc(r : tregister;ainstr:tai);
  2343. begin
  2344. inherited create;
  2345. typ:=ait_regalloc;
  2346. ratype:=ra_dealloc;
  2347. reg:=r;
  2348. { ainstr must be an instruction }
  2349. if assigned(ainstr) and
  2350. (ainstr.typ<>ait_instruction) then
  2351. internalerror(200411012);
  2352. instr:=ainstr;
  2353. end;
  2354. constructor tai_regalloc.sync(r : tregister);
  2355. begin
  2356. inherited create;
  2357. typ:=ait_regalloc;
  2358. ratype:=ra_sync;
  2359. reg:=r;
  2360. end;
  2361. constructor tai_regalloc.resize(r : tregister);
  2362. begin
  2363. inherited create;
  2364. typ:=ait_regalloc;
  2365. ratype:=ra_resize;
  2366. reg:=r;
  2367. end;
  2368. constructor tai_regalloc.markused(r : tregister);
  2369. begin
  2370. inherited create;
  2371. typ:=ait_regalloc;
  2372. ratype:=ra_markused;
  2373. reg:=r;
  2374. end;
  2375. constructor tai_regalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  2376. begin
  2377. inherited ppuload(t,ppufile);
  2378. ppufile.getdata(reg,sizeof(Tregister));
  2379. ratype:=tregalloctype(ppufile.getbyte);
  2380. keep:=ppufile.getboolean;
  2381. end;
  2382. procedure tai_regalloc.ppuwrite(ppufile:tcompilerppufile);
  2383. begin
  2384. inherited ppuwrite(ppufile);
  2385. ppufile.putdata(reg,sizeof(Tregister));
  2386. ppufile.putbyte(byte(ratype));
  2387. ppufile.putboolean(keep);
  2388. end;
  2389. {*****************************************************************************
  2390. TaiInstruction
  2391. *****************************************************************************}
  2392. constructor tai_cpu_abstract.Create(op : tasmop);
  2393. begin
  2394. inherited create;
  2395. typ:=ait_instruction;
  2396. is_jmp:=false;
  2397. opcode:=op;
  2398. ops:=0;
  2399. end;
  2400. destructor tai_cpu_abstract.Destroy;
  2401. var
  2402. i : integer;
  2403. begin
  2404. for i:=0 to opercnt-1 do
  2405. freeop(i);
  2406. inherited destroy;
  2407. end;
  2408. { ---------------------------------------------------------------------
  2409. Loading of operands.
  2410. ---------------------------------------------------------------------}
  2411. procedure tai_cpu_abstract.allocate_oper(opers:longint);
  2412. begin
  2413. while (opers>opercnt) do
  2414. begin
  2415. new(oper[opercnt]);
  2416. fillchar(oper[opercnt]^,sizeof(toper),0);
  2417. inc(opercnt);
  2418. end;
  2419. end;
  2420. procedure tai_cpu_abstract.loadconst(opidx:longint;l:tcgint);
  2421. begin
  2422. allocate_oper(opidx+1);
  2423. with oper[opidx]^ do
  2424. begin
  2425. if typ<>top_const then
  2426. clearop(opidx);
  2427. val:=l;
  2428. typ:=top_const;
  2429. end;
  2430. end;
  2431. procedure tai_cpu_abstract.loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  2432. var
  2433. r : treference;
  2434. begin
  2435. reference_reset_symbol(r,s,sofs,1,[]);
  2436. r.refaddr:=addr_full;
  2437. loadref(opidx,r);
  2438. end;
  2439. procedure tai_cpu_abstract.loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset,forceref:boolean);
  2440. begin
  2441. if not assigned(s) then
  2442. internalerror(200204251);
  2443. allocate_oper(opidx+1);
  2444. with oper[opidx]^ do
  2445. begin
  2446. if typ<>top_local then
  2447. begin
  2448. clearop(opidx);
  2449. new(localoper);
  2450. end;
  2451. with oper[opidx]^.localoper^ do
  2452. begin
  2453. localsym:=s;
  2454. localsymofs:=sofs;
  2455. localindexreg:=indexreg;
  2456. localscale:=scale;
  2457. localgetoffset:=getoffset;
  2458. localforceref:=forceref;
  2459. {$ifdef x86}
  2460. localsegment:=NR_NO;
  2461. {$endif x86}
  2462. end;
  2463. typ:=top_local;
  2464. end;
  2465. end;
  2466. procedure tai_cpu_abstract.loadref(opidx:longint;const r:treference);
  2467. {$ifdef x86}
  2468. var
  2469. si_param: ShortInt;
  2470. {$endif}
  2471. begin
  2472. allocate_oper(opidx+1);
  2473. with oper[opidx]^ do
  2474. begin
  2475. if typ<>top_ref then
  2476. begin
  2477. clearop(opidx);
  2478. new(ref);
  2479. end;
  2480. ref^:=r;
  2481. {$ifdef x86}
  2482. { We allow this exception for x86, since overloading this would be
  2483. too much of a a speed penalty}
  2484. if is_x86_parameterized_string_op(opcode) then
  2485. begin
  2486. si_param:=get_x86_string_op_si_param(opcode);
  2487. if (si_param<>-1) and (taicpu(self).OperandOrder=op_att) then
  2488. si_param:=x86_parameterized_string_op_param_count(opcode)-si_param-1;
  2489. if (si_param=opidx) and (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  2490. segprefix:=ref^.segment;
  2491. end
  2492. else if (opcode=A_XLAT) and (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  2493. segprefix:=ref^.segment
  2494. else if (ref^.segment<>NR_NO) and (ref^.segment<>get_default_segment_of_ref(ref^)) then
  2495. segprefix:=ref^.segment;
  2496. {$endif}
  2497. {$ifndef llvm}
  2498. if (cs_create_pic in current_settings.moduleswitches) and
  2499. assigned(r.symbol) and
  2500. not assigned(r.relsymbol) and
  2501. (r.refaddr=addr_no)
  2502. {$ifdef ARM}
  2503. and not(r.base=NR_R15)
  2504. {$endif ARM}
  2505. {$ifdef aarch64}
  2506. and not(r.refaddr in [addr_full,addr_gotpageoffset,addr_gotpage])
  2507. {$endif aarch64}
  2508. then
  2509. internalerror(200502052);
  2510. {$endif not llvm}
  2511. typ:=top_ref;
  2512. if assigned(add_reg_instruction_hook) then
  2513. begin
  2514. add_reg_instruction_hook(self,ref^.base);
  2515. add_reg_instruction_hook(self,ref^.index);
  2516. end;
  2517. { mark symbol as used }
  2518. if assigned(ref^.symbol) then
  2519. ref^.symbol.increfs;
  2520. if assigned(ref^.relsymbol) then
  2521. ref^.relsymbol.increfs;
  2522. end;
  2523. end;
  2524. procedure tai_cpu_abstract.loadreg(opidx:longint;r:tregister);
  2525. begin
  2526. allocate_oper(opidx+1);
  2527. with oper[opidx]^ do
  2528. begin
  2529. if typ<>top_reg then
  2530. clearop(opidx);
  2531. reg:=r;
  2532. typ:=top_reg;
  2533. end;
  2534. if assigned(add_reg_instruction_hook) then
  2535. add_reg_instruction_hook(self,r);
  2536. {$ifdef ARM}
  2537. { R15 is the PC on the ARM thus moves to R15 are jumps.
  2538. Due to speed considerations we don't use a virtual overridden method here.
  2539. Because the pc/r15 isn't handled by the reg. allocator this should never cause
  2540. problems with iregs getting r15.
  2541. }
  2542. is_jmp:=(opcode=A_MOV) and (opidx=0) and (r=NR_R15);
  2543. {$endif ARM}
  2544. end;
  2545. procedure tai_cpu_abstract.loadoper(opidx:longint;o:toper);
  2546. {$ifdef x86}
  2547. var
  2548. si_param: ShortInt;
  2549. {$endif x86}
  2550. begin
  2551. allocate_oper(opidx+1);
  2552. clearop(opidx);
  2553. oper[opidx]^:=o;
  2554. { copy also the reference }
  2555. with oper[opidx]^ do
  2556. begin
  2557. case typ of
  2558. top_reg:
  2559. begin
  2560. if assigned(add_reg_instruction_hook) then
  2561. add_reg_instruction_hook(self,reg);
  2562. end;
  2563. top_ref:
  2564. begin
  2565. new(ref);
  2566. ref^:=o.ref^;
  2567. {$ifdef x86}
  2568. { We allow this exception for x86, since overloading this would be
  2569. too much of a a speed penalty}
  2570. if is_x86_parameterized_string_op(opcode) then
  2571. begin
  2572. si_param:=get_x86_string_op_si_param(opcode);
  2573. if (si_param<>-1) and (taicpu(self).OperandOrder=op_att) then
  2574. si_param:=x86_parameterized_string_op_param_count(opcode)-si_param-1;
  2575. if (si_param=opidx) and (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  2576. segprefix:=ref^.segment;
  2577. end
  2578. else if (opcode=A_XLAT) and (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  2579. segprefix:=ref^.segment
  2580. else if (ref^.segment<>NR_NO) and (ref^.segment<>get_default_segment_of_ref(ref^)) then
  2581. segprefix:=ref^.segment;
  2582. {$endif x86}
  2583. if assigned(add_reg_instruction_hook) then
  2584. begin
  2585. add_reg_instruction_hook(self,ref^.base);
  2586. add_reg_instruction_hook(self,ref^.index);
  2587. end;
  2588. end;
  2589. {$ifdef ARM}
  2590. top_shifterop:
  2591. begin
  2592. new(shifterop);
  2593. shifterop^:=o.shifterop^;
  2594. if assigned(add_reg_instruction_hook) then
  2595. add_reg_instruction_hook(self,shifterop^.rs);
  2596. end;
  2597. {$endif ARM}
  2598. else
  2599. ;
  2600. end;
  2601. end;
  2602. end;
  2603. procedure tai_cpu_abstract.clearop(opidx:longint);
  2604. begin
  2605. with oper[opidx]^ do
  2606. begin
  2607. case typ of
  2608. top_ref:
  2609. dispose(ref);
  2610. top_local:
  2611. dispose(localoper);
  2612. {$ifdef ARM}
  2613. top_shifterop:
  2614. dispose(shifterop);
  2615. top_regset:
  2616. dispose(regset);
  2617. {$endif ARM}
  2618. {$ifdef jvm}
  2619. top_string:
  2620. freemem(pcval);
  2621. top_wstring:
  2622. donewidestring(pwstrval);
  2623. {$endif jvm}
  2624. {$ifdef wasm}
  2625. top_functype:
  2626. FreeAndNil(functype);
  2627. {$endif wasm}
  2628. else
  2629. ;
  2630. end;
  2631. typ:=top_none;
  2632. end;
  2633. end;
  2634. procedure tai_cpu_abstract.freeop(opidx:longint);
  2635. begin
  2636. clearop(opidx);
  2637. dispose(oper[opidx]);
  2638. end;
  2639. { ---------------------------------------------------------------------
  2640. Miscellaneous methods.
  2641. ---------------------------------------------------------------------}
  2642. procedure tai_cpu_abstract.SetCondition(const c:TAsmCond);
  2643. begin
  2644. condition:=c;
  2645. end;
  2646. Function tai_cpu_abstract.getcopy:TLinkedListItem;
  2647. var
  2648. i : longint;
  2649. p : tai_cpu_abstract;
  2650. begin
  2651. p:=tai_cpu_abstract(inherited getcopy);
  2652. { make a copy of the references }
  2653. p.opercnt:=0;
  2654. p.allocate_oper(ops);
  2655. for i:=0 to ops-1 do
  2656. begin
  2657. p.oper[i]^:=oper[i]^;
  2658. case oper[i]^.typ of
  2659. top_local :
  2660. begin
  2661. new(p.oper[i]^.localoper);
  2662. p.oper[i]^.localoper^:=oper[i]^.localoper^;
  2663. end;
  2664. top_ref :
  2665. begin
  2666. new(p.oper[i]^.ref);
  2667. p.oper[i]^.ref^:=oper[i]^.ref^;
  2668. end;
  2669. {$ifdef ARM}
  2670. top_shifterop:
  2671. begin
  2672. new(p.oper[i]^.shifterop);
  2673. p.oper[i]^.shifterop^:=oper[i]^.shifterop^;
  2674. end;
  2675. {$endif ARM}
  2676. else
  2677. ;
  2678. end;
  2679. end;
  2680. getcopy:=p;
  2681. end;
  2682. function tai_cpu_abstract.is_same_reg_move(regtype: Tregistertype):boolean;
  2683. begin
  2684. { When the generic RA is used this needs to be overridden, we don't use
  2685. virtual;abstract; to prevent a lot of warnings of unimplemented abstract methods
  2686. when tai_cpu is created (PFV) }
  2687. internalerror(2004040901);
  2688. result:=false;
  2689. end;
  2690. function tai_cpu_abstract.spilling_get_operation_type(opnr: longint): topertype;
  2691. begin
  2692. internalerror(2004040902);
  2693. result:=operand_readwrite;
  2694. end;
  2695. function tai_cpu_abstract.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
  2696. begin
  2697. result := operand_read;
  2698. end;
  2699. constructor tai_cpu_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  2700. var
  2701. i : integer;
  2702. begin
  2703. inherited ppuload(t,ppufile);
  2704. { hopefully, we don't get problems with big/little endian here when cross compiling :/ }
  2705. ppufile.getdata(condition,sizeof(tasmcond));
  2706. ops := ppufile.getbyte;
  2707. allocate_oper(ops);
  2708. for i:=0 to ops-1 do
  2709. ppuloadoper(ppufile,oper[i]^);
  2710. opcode:=tasmop(ppufile.getword);
  2711. {$ifdef x86}
  2712. ppufile.getdata(segprefix,sizeof(Tregister));
  2713. {$endif x86}
  2714. is_jmp:=ppufile.getboolean;
  2715. end;
  2716. procedure tai_cpu_abstract.ppuwrite(ppufile:tcompilerppufile);
  2717. var
  2718. i : integer;
  2719. begin
  2720. inherited ppuwrite(ppufile);
  2721. ppufile.putdata(condition,sizeof(tasmcond));
  2722. ppufile.putbyte(ops);
  2723. for i:=0 to ops-1 do
  2724. ppuwriteoper(ppufile,oper[i]^);
  2725. ppufile.putword(word(opcode));
  2726. {$ifdef x86}
  2727. ppufile.putdata(segprefix,sizeof(Tregister));
  2728. {$endif x86}
  2729. ppufile.putboolean(is_jmp);
  2730. end;
  2731. procedure tai_cpu_abstract.buildderefimpl;
  2732. var
  2733. i : integer;
  2734. begin
  2735. for i:=0 to ops-1 do
  2736. ppubuildderefimploper(oper[i]^);
  2737. end;
  2738. procedure tai_cpu_abstract.derefimpl;
  2739. var
  2740. i : integer;
  2741. begin
  2742. for i:=0 to ops-1 do
  2743. ppuderefoper(oper[i]^);
  2744. end;
  2745. procedure tai_cpu_abstract.resetpass1;
  2746. begin
  2747. end;
  2748. procedure tai_cpu_abstract.resetpass2;
  2749. begin
  2750. end;
  2751. function tai_cpu_abstract.Pass1(objdata:TObjData):longint;
  2752. begin
  2753. result:=0;
  2754. end;
  2755. procedure tai_cpu_abstract.Pass2(objdata:TObjData);
  2756. begin
  2757. end;
  2758. procedure tai_cpu_abstract.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  2759. begin
  2760. o.typ:=toptype(ppufile.getbyte);
  2761. o.ot:=ppufile.getlongint;
  2762. case o.typ of
  2763. top_reg :
  2764. ppufile.getdata(o.reg,sizeof(Tregister));
  2765. top_ref :
  2766. begin
  2767. new(o.ref);
  2768. {$ifdef x86}
  2769. ppufile.getdata(o.ref^.segment,sizeof(Tregister));
  2770. {$endif x86}
  2771. ppufile.getdata(o.ref^.base,sizeof(Tregister));
  2772. ppufile.getdata(o.ref^.index,sizeof(Tregister));
  2773. ppufile.getdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));
  2774. o.ref^.scalefactor:=ppufile.getbyte;
  2775. o.ref^.offset:=ppufile.getaint;
  2776. o.ref^.symbol:=ppufile.getasmsymbol;
  2777. o.ref^.relsymbol:=ppufile.getasmsymbol;
  2778. end;
  2779. top_const :
  2780. o.val:=ppufile.getaint;
  2781. top_local :
  2782. begin
  2783. new(o.localoper);
  2784. with o.localoper^ do
  2785. begin
  2786. ppufile.getderef(localsymderef);
  2787. localsymofs:=ppufile.getaint;
  2788. {$ifdef x86}
  2789. localsegment:=tregister(ppufile.getlongint);
  2790. {$endif x86}
  2791. localindexreg:=tregister(ppufile.getlongint);
  2792. localscale:=ppufile.getbyte;
  2793. localgetoffset:=(ppufile.getbyte<>0);
  2794. end;
  2795. end;
  2796. else
  2797. internalerror(2007010210);
  2798. end;
  2799. end;
  2800. procedure tai_cpu_abstract.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  2801. begin
  2802. ppufile.putbyte(byte(o.typ));
  2803. ppufile.putlongint(o.ot);
  2804. case o.typ of
  2805. top_reg :
  2806. ppufile.putdata(o.reg,sizeof(Tregister));
  2807. top_ref :
  2808. begin
  2809. {$ifdef x86}
  2810. ppufile.putdata(o.ref^.segment,sizeof(Tregister));
  2811. {$endif x86}
  2812. ppufile.putdata(o.ref^.base,sizeof(Tregister));
  2813. ppufile.putdata(o.ref^.index,sizeof(Tregister));
  2814. ppufile.putdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));
  2815. ppufile.putbyte(o.ref^.scalefactor);
  2816. ppufile.putaint(o.ref^.offset);
  2817. ppufile.putasmsymbol(o.ref^.symbol);
  2818. ppufile.putasmsymbol(o.ref^.relsymbol);
  2819. end;
  2820. top_const :
  2821. ppufile.putaint(o.val);
  2822. top_local :
  2823. begin
  2824. with o.localoper^ do
  2825. begin
  2826. ppufile.putderef(localsymderef);
  2827. ppufile.putaint(localsymofs);
  2828. {$ifdef x86}
  2829. ppufile.putlongint(longint(localsegment));
  2830. {$endif x86}
  2831. ppufile.putlongint(longint(localindexreg));
  2832. ppufile.putbyte(localscale);
  2833. ppufile.putbyte(byte(localgetoffset));
  2834. end;
  2835. end;
  2836. else
  2837. internalerror(2007010211);
  2838. end;
  2839. end;
  2840. {****************************************************************************
  2841. tai_align_abstract
  2842. ****************************************************************************}
  2843. constructor tai_align_abstract.Create(b: byte);
  2844. begin
  2845. inherited Create;
  2846. typ:=ait_align;
  2847. {$ifdef EXTDEBUG}
  2848. if upper(classname)='TAI_ALIGN_ABSTRACT' then
  2849. internalerror(200709191);
  2850. {$endif EXTDEBUG}
  2851. if b in [1,2,4,8,16,32,64] then
  2852. aligntype := b
  2853. else
  2854. aligntype := 1;
  2855. fillsize:=0;
  2856. fillop:=0;
  2857. use_op:=false;
  2858. maxbytes:=aligntype;
  2859. end;
  2860. constructor tai_align_abstract.Create_op(b: byte; _op: byte);
  2861. begin
  2862. inherited Create;
  2863. typ:=ait_align;
  2864. if b in [1,2,4,8,16,32] then
  2865. aligntype := b
  2866. else
  2867. aligntype := 1;
  2868. fillsize:=0;
  2869. fillop:=_op;
  2870. use_op:=true;
  2871. maxbytes:=aligntype;
  2872. end;
  2873. constructor tai_align_abstract.create_max(b : byte; max : byte);
  2874. begin
  2875. inherited Create;
  2876. typ:=ait_align;
  2877. if b in [1,2,4,8,16,32] then
  2878. aligntype := b
  2879. else
  2880. aligntype := 1;
  2881. maxbytes:=max;
  2882. fillsize:=0;
  2883. fillop:=0;
  2884. use_op:=false;
  2885. end;
  2886. constructor tai_align_abstract.create_op_max(b: byte; _op: byte; max: byte);
  2887. begin
  2888. inherited Create;
  2889. typ:=ait_align;
  2890. if b in [1,2,4,8,16,32] then
  2891. aligntype := b
  2892. else
  2893. aligntype := 1;
  2894. fillop:=_op;
  2895. use_op:=true;
  2896. maxbytes:=max;
  2897. fillsize:=0;
  2898. end;
  2899. constructor tai_align_abstract.Create_zeros(b: byte);
  2900. begin
  2901. inherited Create;
  2902. typ:=ait_align;
  2903. if b in [1,2,4,8,16,32] then
  2904. aligntype := b
  2905. else
  2906. aligntype := 1;
  2907. use_op:=true;
  2908. fillsize:=0;
  2909. fillop:=0;
  2910. maxbytes:=aligntype;
  2911. end;
  2912. function tai_align_abstract.calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;
  2913. begin
  2914. if fillsize>sizeof(buf) then
  2915. internalerror(200404293);
  2916. fillchar(buf,high(buf),fillop);
  2917. calculatefillbuf:=pchar(@buf);
  2918. end;
  2919. constructor tai_align_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  2920. begin
  2921. inherited ppuload(t,ppufile);
  2922. aligntype:=ppufile.getbyte;
  2923. fillsize:=0;
  2924. fillop:=ppufile.getbyte;
  2925. use_op:=ppufile.getboolean;
  2926. maxbytes:=ppufile.getbyte;
  2927. end;
  2928. procedure tai_align_abstract.ppuwrite(ppufile:tcompilerppufile);
  2929. begin
  2930. inherited ppuwrite(ppufile);
  2931. ppufile.putbyte(aligntype);
  2932. ppufile.putbyte(fillop);
  2933. ppufile.putboolean(use_op);
  2934. ppufile.putbyte(maxbytes);
  2935. end;
  2936. {****************************************************************************
  2937. tai_seh_directive
  2938. ****************************************************************************}
  2939. const
  2940. datatypemap: array[TAsmSehDirective] of TSehDirectiveDatatype=(
  2941. sd_string, { proc }
  2942. sd_none, { endproc }
  2943. sd_none, { endprologue }
  2944. sd_string, { handler }
  2945. sd_none, { handlerdata }
  2946. sd_none,sd_none,sd_none, { eh, 32, no32 }
  2947. sd_regoffset, { setframe }
  2948. sd_offset, { stackalloc }
  2949. sd_reg, { pushreg }
  2950. sd_regoffset, { savereg }
  2951. sd_regoffset, { savereg_x }
  2952. sd_regoffset, { saveregp }
  2953. sd_regoffset, { saveregp_x }
  2954. sd_regoffset, { savexmm }
  2955. sd_regoffset, { savefreg }
  2956. sd_regoffset, { savefreg_x }
  2957. sd_regoffset, { savefregp }
  2958. sd_regoffset, { savefregp_x }
  2959. sd_none, { pushframe }
  2960. sd_none, { setfp }
  2961. sd_none, { addfp }
  2962. sd_offset, { savefplr }
  2963. sd_offset, { savefplr_x }
  2964. sd_none, { nop }
  2965. sd_reg, { pushnv }
  2966. sd_none { savenv }
  2967. );
  2968. constructor tai_seh_directive.create(_kind:TAsmSehDirective);
  2969. begin
  2970. inherited Create;
  2971. typ:=ait_seh_directive;
  2972. kind:=_kind;
  2973. data.typ:=datatypemap[_kind];
  2974. end;
  2975. constructor tai_seh_directive.create_name(_kind:TAsmSehDirective;const _name:string);
  2976. begin
  2977. create(_kind);
  2978. data.name:=stringdup(_name);
  2979. end;
  2980. constructor tai_seh_directive.create_reg(_kind:TAsmSehDirective;r:TRegister);
  2981. begin
  2982. create(_kind);
  2983. data.reg:=r;
  2984. end;
  2985. constructor tai_seh_directive.create_offset(_kind:TAsmSehDirective;ofs:dword);
  2986. begin
  2987. create(_kind);
  2988. data.offset:=ofs;
  2989. end;
  2990. constructor tai_seh_directive.create_reg_offset(_kind:TAsmSehDirective;
  2991. r:TRegister;ofs:dword);
  2992. begin
  2993. create(_kind);
  2994. data.offset:=ofs;
  2995. data.reg:=r;
  2996. end;
  2997. constructor tai_seh_directive.ppuload(t:taitype;ppufile:tcompilerppufile);
  2998. begin
  2999. inherited ppuload(t, ppufile);
  3000. kind:=TAsmSehDirective(ppufile.getbyte);
  3001. data.typ:=datatypemap[kind];
  3002. case data.typ of
  3003. sd_none: ;
  3004. sd_string:
  3005. begin
  3006. data.name:=ppufile.getpshortstring;
  3007. data.flags:=ppufile.getbyte;
  3008. end;
  3009. sd_reg,sd_offset,sd_regoffset:
  3010. begin
  3011. ppufile.getdata(data.reg,sizeof(TRegister));
  3012. data.offset:=ppufile.getdword;
  3013. end;
  3014. end;
  3015. end;
  3016. destructor tai_seh_directive.destroy;
  3017. begin
  3018. if data.typ=sd_string then
  3019. stringdispose(data.name);
  3020. inherited destroy;
  3021. end;
  3022. procedure tai_seh_directive.ppuwrite(ppufile:tcompilerppufile);
  3023. begin
  3024. inherited ppuwrite(ppufile);
  3025. ppufile.putbyte(ord(kind));
  3026. case data.typ of
  3027. sd_none: ;
  3028. sd_string:
  3029. begin
  3030. ppufile.putstring(data.name^);
  3031. ppufile.putbyte(data.flags);
  3032. end;
  3033. sd_reg,sd_offset,sd_regoffset:
  3034. begin
  3035. ppufile.putdata(data.reg,sizeof(TRegister));
  3036. ppufile.putdword(data.offset);
  3037. end;
  3038. end;
  3039. end;
  3040. procedure tai_seh_directive.generate_code(objdata:TObjData);
  3041. begin
  3042. end;
  3043. {****************************************************************************
  3044. tai_eabi_attribute
  3045. ****************************************************************************}
  3046. constructor tai_eabi_attribute.create(atag,avalue : dword);
  3047. begin
  3048. inherited Create;
  3049. typ:=ait_eabi_attribute;
  3050. eattr_typ:=eattrtype_dword;
  3051. tag:=atag;
  3052. value:=avalue;
  3053. end;
  3054. constructor tai_eabi_attribute.create(atag: dword; const avalue: string);
  3055. begin
  3056. inherited Create;
  3057. typ:=ait_eabi_attribute;
  3058. eattr_typ:=eattrtype_ntbs;
  3059. tag:=atag;
  3060. valuestr:=NewStr(avalue);
  3061. end;
  3062. destructor tai_eabi_attribute.destroy;
  3063. begin
  3064. Inherited Destroy;
  3065. end;
  3066. constructor tai_eabi_attribute.ppuload(t:taitype;ppufile:tcompilerppufile);
  3067. begin
  3068. end;
  3069. procedure tai_eabi_attribute.ppuwrite(ppufile:tcompilerppufile);
  3070. begin
  3071. inherited ppuwrite(ppufile);
  3072. ppufile.putdword(tag);
  3073. ppufile.putdword(value);
  3074. end;
  3075. {$ifdef JVM}
  3076. {****************************************************************************
  3077. tai_jvar
  3078. ****************************************************************************}
  3079. constructor tai_jvar.Create(_stackslot: longint; const _desc: shortstring; _startlab, _stoplab: TAsmSymbol);
  3080. begin
  3081. Inherited create;
  3082. typ:=ait_jvar;
  3083. stackslot:=_stackslot;
  3084. desc:=stringdup(_desc);
  3085. startlab:=_startlab;
  3086. stoplab:=_stoplab;
  3087. end;
  3088. constructor tai_jvar.ppuload(t: taitype; ppufile: tcompilerppufile);
  3089. begin
  3090. inherited ppuload(t, ppufile);
  3091. stackslot:=ppufile.getlongint;
  3092. desc:=ppufile.getpshortstring;
  3093. startlab:=ppufile.getasmsymbol;
  3094. stoplab:=ppufile.getasmsymbol;
  3095. end;
  3096. procedure tai_jvar.ppuwrite(ppufile: tcompilerppufile);
  3097. begin
  3098. inherited ppuwrite(ppufile);
  3099. ppufile.putlongint(stackslot);
  3100. ppufile.putstring(desc^);
  3101. ppufile.putasmsymbol(startlab);
  3102. ppufile.putasmsymbol(stoplab);
  3103. end;
  3104. destructor tai_jvar.destroy;
  3105. begin
  3106. stringdispose(desc);
  3107. inherited destroy;
  3108. end;
  3109. {****************************************************************************
  3110. tai_jcatch
  3111. ****************************************************************************}
  3112. constructor tai_jcatch.Create(const _name: shortstring; _startlab, _stoplab, _handlerlab: TAsmSymbol);
  3113. begin
  3114. Inherited create;
  3115. typ:=ait_jcatch;
  3116. name:=stringdup(_name);
  3117. startlab:=_startlab;
  3118. startlab.increfs;
  3119. stoplab:=_stoplab;
  3120. stoplab.increfs;
  3121. handlerlab:=_handlerlab;
  3122. handlerlab.increfs;
  3123. end;
  3124. destructor tai_jcatch.destroy;
  3125. begin
  3126. stringdispose(name);
  3127. inherited destroy;
  3128. end;
  3129. constructor tai_jcatch.ppuload(t: taitype; ppufile: tcompilerppufile);
  3130. begin
  3131. inherited ppuload(t, ppufile);
  3132. name:=ppufile.getpshortstring;
  3133. startlab:=ppufile.getasmsymbol;
  3134. startlab.increfs;
  3135. stoplab:=ppufile.getasmsymbol;
  3136. stoplab.increfs;
  3137. handlerlab:=ppufile.getasmsymbol;
  3138. handlerlab.increfs;
  3139. end;
  3140. procedure tai_jcatch.ppuwrite(ppufile: tcompilerppufile);
  3141. begin
  3142. inherited ppuwrite(ppufile);
  3143. ppufile.putstring(name^);
  3144. ppufile.putasmsymbol(startlab);
  3145. ppufile.putasmsymbol(stoplab);
  3146. ppufile.putasmsymbol(handlerlab);
  3147. end;
  3148. {$endif JVM}
  3149. begin
  3150. {$ifndef WASM}
  3151. {$push}{$warnings off}
  3152. { taitype should fit into a 4 byte set for speed reasons }
  3153. if ord(high(taitype))>31 then
  3154. internalerror(201108181);
  3155. {$pop}
  3156. {$endif WASM}
  3157. end.