aasmcnst.pas 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364
  1. {
  2. Copyright (c) 2014 by Jonas Maebe, member of the Free Pascal development
  3. team
  4. This unit implements typed constant data elements at the assembler level
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit aasmcnst;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,globtype,constexp,widestr,
  23. aasmbase,aasmdata,aasmtai,
  24. symconst,symbase,symtype,symdef,symsym;
  25. type
  26. { typed const: integer/floating point/string/pointer/... const along with
  27. tdef info }
  28. ttypedconstkind = (tck_simple, tck_array, tck_record);
  29. { the type of the element and its def }
  30. tai_abstracttypedconst = class abstract (tai)
  31. private
  32. procedure setdef(def: tdef);
  33. protected
  34. fadetyp: ttypedconstkind;
  35. { the def of this element }
  36. fdef: tdef;
  37. public
  38. constructor create(_adetyp: ttypedconstkind; _def: tdef);
  39. property adetyp: ttypedconstkind read fadetyp;
  40. property def: tdef read fdef write setdef;
  41. end;
  42. { a simple data element; the value is stored as a tai }
  43. tai_simpletypedconst = class(tai_abstracttypedconst)
  44. private
  45. procedure setval(AValue: tai);
  46. protected
  47. fval: tai;
  48. public
  49. constructor create(_def: tdef; _val: tai);
  50. destructor destroy; override;
  51. property val: tai read fval write setval;
  52. end;
  53. { an aggregate data element (record or array). Values are stored as an
  54. array of tsimpledataelement. }
  55. tai_aggregatetypedconst = class(tai_abstracttypedconst)
  56. public type
  57. { iterator to walk over all individual items in the aggregate }
  58. tadeenumerator = class(tobject)
  59. private
  60. fvalues: tfpobjectlist;
  61. fvaluespos: longint;
  62. function getcurrent: tai_abstracttypedconst;
  63. public
  64. constructor create(data: tai_aggregatetypedconst);
  65. function movenext: boolean;
  66. procedure reset;
  67. property current: tai_abstracttypedconst read getcurrent;
  68. end;
  69. protected
  70. fvalues: tfpobjectlist;
  71. fisstring: boolean;
  72. { converts the existing data to a single tai_string }
  73. procedure convert_to_string;
  74. procedure add_to_string(strtai: tai_string; othertai: tai);
  75. public
  76. constructor create(_adetyp: ttypedconstkind; _fdef: tdef);
  77. function getenumerator: tadeenumerator;
  78. procedure addvalue(val: tai_abstracttypedconst); virtual;
  79. function valuecount: longint;
  80. procedure insertvaluebeforepos(val: tai_abstracttypedconst; pos: longint);
  81. procedure replacevalueatpos(val: tai_abstracttypedconst; pos: longint);
  82. { change the type to a record, regardless of how the aggregate was created;
  83. the size of the original type and the record must match }
  84. procedure changetorecord(_def: trecorddef);
  85. procedure finish;
  86. destructor destroy; override;
  87. end;
  88. tasmlabofs = record
  89. lab: tasmlabel;
  90. ofs: asizeint;
  91. end;
  92. { flags for the finalisation of the typed const builder asmlist }
  93. ttcasmlistoption = (
  94. { the tasmsymbol is a tasmlabel }
  95. tcalo_is_lab,
  96. { start a new section (e.g., because we don't know the current section
  97. type) }
  98. tcalo_new_section,
  99. { this symbol is the start of a block of data that should be
  100. dead-stripable/smartlinkable; may imply starting a new section, but
  101. not necessarily (depends on what the platform requirements are) }
  102. tcalo_make_dead_strippable,
  103. { this symbol should never be removed by the linker }
  104. tcalo_no_dead_strip,
  105. { start of a vectorized but individually dead strippable list of elements,
  106. like the resource strings of a unit: they have to stay in this order,
  107. but individual elements can be removed }
  108. tcalo_vectorized_dead_strip_start,
  109. { item in the above list }
  110. tcalo_vectorized_dead_strip_item,
  111. { end of the above list }
  112. tcalo_vectorized_dead_strip_end,
  113. { symbol should be weakly defined }
  114. tcalo_weak,
  115. { symbol should be registered with the unit's public assembler symbols }
  116. tcalo_is_public_asm,
  117. { symbol should be declared with AT_DATA_FORCEINDIRECT }
  118. tcalo_data_force_indirect,
  119. { apply const_align() to the alignment, for user-defined data }
  120. tcalo_apply_constalign
  121. );
  122. ttcasmlistoptions = set of ttcasmlistoption;
  123. ttcdeadstripsectionsymboloption = (
  124. { define the symbol }
  125. tcdssso_define,
  126. { register the assembler symbol either with the public or extern assembler
  127. symbols of the unit }
  128. tcdssso_register_asmsym,
  129. { use the indirect symbol }
  130. tcdssso_use_indirect
  131. );
  132. ttcdeadstripsectionsymboloptions = set of ttcdeadstripsectionsymboloption;
  133. { information about aggregates we are parsing }
  134. taggregateinformation = class
  135. private
  136. fnextfieldname: TIDString;
  137. function getcuroffset: asizeint;
  138. procedure setnextfieldname(const AValue: TIDString);
  139. protected
  140. { type of the aggregate }
  141. fdef: tdef;
  142. { type of the aggregate }
  143. ftyp: ttypedconstkind;
  144. { symtable entry of the previously emitted field in case of a
  145. record/object (nil if none emitted yet), used to insert alignment bytes
  146. if necessary for variant records and objects }
  147. fcurfield,
  148. { field corresponding to the data that will be emitted next in case of a
  149. record/object (nil if not set), used to handle variant records and
  150. objects }
  151. fnextfield: tfieldvarsym;
  152. { similar as the fcurfield/fnextfield above, but instead of fieldvarsyms
  153. these are indices in the symlist of a recorddef that correspond to
  154. fieldvarsyms. These are used only for non-variant records, simply
  155. traversing the fields in order. We could use the above method here as
  156. well, but to find the next field we'd always have to use
  157. symlist.indexof(fcurfield), which would be quite slow. These have -1 as
  158. value if they're not set }
  159. fcurindex,
  160. fnextindex: longint;
  161. { anonymous record that is being built as we add constant data }
  162. fanonrecord: boolean;
  163. property curindex: longint read fcurindex write fcurindex;
  164. property nextindex: longint read fnextindex write fnextindex;
  165. public
  166. constructor create(_def: tdef; _typ: ttypedconstkind); virtual;
  167. { calculated padding bytes for alignment if needed, and add the def of the
  168. next field in case we are constructing an anonymous record }
  169. function prepare_next_field(nextfielddef: tdef): asizeint; virtual;
  170. property def: tdef read fdef;
  171. property typ: ttypedconstkind read ftyp;
  172. property curfield: tfieldvarsym read fcurfield write fcurfield;
  173. property nextfield: tfieldvarsym read fnextfield write fnextfield;
  174. property nextfieldname: TIDString write setnextfieldname;
  175. property curoffset: asizeint read getcuroffset;
  176. property anonrecord: boolean read fanonrecord write fanonrecord;
  177. end;
  178. taggregateinformationclass = class of taggregateinformation;
  179. { information about a placeholder element that has been added, and which has
  180. to be replaced later with a real data element }
  181. ttypedconstplaceholder = class abstract
  182. def: tdef;
  183. constructor create(d: tdef);
  184. { same usage as ttai_typedconstbuilder.emit_tai }
  185. procedure replace(ai: tai; d: tdef); virtual; abstract;
  186. end;
  187. { Warning: never directly create a ttai_typedconstbuilder instance,
  188. instead create a cai_typedconstbuilder (this class can be overridden) }
  189. ttai_typedconstbuilder = class abstract
  190. { class type to use when creating new aggregate information instances }
  191. protected class var
  192. caggregateinformation: taggregateinformationclass;
  193. private
  194. function getcurragginfo: taggregateinformation;
  195. procedure set_next_field(AValue: tfieldvarsym);
  196. procedure set_next_field_name(const AValue: TIDString);
  197. protected
  198. { temporary list in which all data is collected }
  199. fasmlist: tasmlist;
  200. { options for the final asmlist }
  201. foptions: ttcasmlistoptions;
  202. { while queueing elements of a compound expression, this is the current
  203. offset in the top-level array/record }
  204. fqueue_offset: asizeint;
  205. fqueued_def: tdef;
  206. { array of caggregateinformation instances }
  207. faggregateinformation: tfpobjectlist;
  208. { Support for generating data that is only referenced from the typed
  209. constant data that we are currently generated. Such data can all be put
  210. in the same dead-strippable unit, as it's either all included or none of
  211. it is included. This data can be spread over multiple kinds of sections
  212. though (e.g. rodata and rodata_no_rel), so per section keep track whether
  213. we already started a dead-strippable unit and if so, what the section
  214. name was (so that on platforms that perform the dead stripping based on
  215. sections, we put all data for one typed constant into a single section
  216. with the same name) }
  217. protected type
  218. tinternal_data_section_info = record
  219. secname: TSymStr;
  220. sectype: TAsmSectiontype;
  221. end;
  222. protected var
  223. { all internally generated data must be stored in the same list, as it must
  224. be consecutive (if it's spread over multiple lists, we don't know in
  225. which order they'll be concatenated) -> keep track of this list }
  226. finternal_data_asmlist: tasmlist;
  227. { kind of the last section we started in the finternal_data_asmlist, to
  228. avoid creating unnecessary section statements }
  229. finternal_data_current_section: TAsmSectiontype;
  230. { info about in which kinds of sections we have already emitted internal
  231. data, and what their names were }
  232. finternal_data_section_info: array of tinternal_data_section_info;
  233. { ensure that finalize_asmlist is called only once }
  234. fasmlist_finalized: boolean;
  235. { ensure that if it's vectorized dead strippable data, we called
  236. finalize_vectorized_dead_strip_asmlist instead of finalize_asmlist }
  237. fvectorized_finalize_called: boolean;
  238. { returns whether def must be handled as an aggregate on the current
  239. platform }
  240. function aggregate_kind(def: tdef): ttypedconstkind; virtual;
  241. { finalize the asmlist: add the necessary symbols etc }
  242. procedure finalize_asmlist(asmsym: tasmsymbol; sym: tsym; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions); virtual;
  243. procedure finalize_asmlist_add_indirect_sym(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions); virtual;
  244. { prepare finalization (common for the default and overridden versions }
  245. procedure finalize_asmlist_prepare(const options: ttcasmlistoptions; var alignment: shortint);
  246. { functionality of the above for vectorized dead strippable sections }
  247. procedure finalize_vectorized_dead_strip_asmlist(sym: tsym; def: tdef; const basename, itemname: TSymStr; st: tsymtable; alignment: shortint; options: ttcasmlistoptions); virtual;
  248. { called by the public emit_tai() routines to actually add the typed
  249. constant data; the public ones also take care of adding extra padding
  250. bytes etc (by calling this one) }
  251. procedure do_emit_tai(p: tai; def: tdef); virtual;
  252. { calls prepare_next_field() and adds the padding bytes in the current
  253. location }
  254. procedure pad_next_field(nextfielddef: tdef);
  255. { returns the index in finternal_data_section_info of the info for the
  256. section of type typ. Returns -1 if there is no such info yet }
  257. function get_internal_data_section_index(typ: TAsmSectiontype): longint;
  258. { get a start label for an internal data section (at the start of a
  259. potentially dead-strippable part) }
  260. function get_internal_data_section_start_label: tasmlabel; virtual;
  261. { get a label in the middle of an internal data section (no dead
  262. stripping) }
  263. function get_internal_data_section_internal_label: tasmlabel; virtual;
  264. { adds a new entry to current_module.linkorderedsymbols }
  265. procedure add_link_ordered_symbol(sym: tasmsymbol; const secname: TSymStr); virtual;
  266. { easy access to the top level aggregate information instance }
  267. property curagginfo: taggregateinformation read getcurragginfo;
  268. public
  269. constructor create(const options: ttcasmlistoptions); virtual;
  270. destructor destroy; override;
  271. public
  272. { returns a builder for generating data that is only referrenced by the
  273. typed constant date we are currently generating (e.g. string data for a
  274. pchar constant). Also returns the label that will be placed at the start
  275. of that data. list is the tasmlist to which the data will be added.
  276. secname can be empty to use a default }
  277. procedure start_internal_data_builder(list: tasmlist; sectype: TAsmSectiontype; const secname: TSymStr; out tcb: ttai_typedconstbuilder; out l: tasmlabel);
  278. { finish a previously started internal data builder, including
  279. concatenating all generated data to the provided list and freeing the
  280. builder }
  281. procedure finish_internal_data_builder(var tcb: ttai_typedconstbuilder; l: tasmlabel; def: tdef; alignment: longint);
  282. { add a simple constant data element (p) to the typed constant.
  283. def is the type of the added value }
  284. procedure emit_tai(p: tai; def: tdef); virtual;
  285. { same as above, for a special case: when the def is a procvardef and we
  286. want to use it explicitly as a procdef (i.e., not as a record with a
  287. code and data pointer in case of a complex procvardef) }
  288. procedure emit_tai_procvar2procdef(p: tai; pvdef: tprocvardef); virtual;
  289. protected
  290. procedure maybe_emit_tail_padding(def: tdef); virtual;
  291. function emit_string_const_common(stringtype: tstringtype; len: asizeint; encoding: tstringencoding; var startlab: tasmlabel):tasmlabofs;
  292. function get_dynstring_def_for_type(stringtype: tstringtype; winlikewidestring: boolean): tstringdef;
  293. procedure begin_aggregate_internal(def: tdef; anonymous: boolean); virtual;
  294. procedure end_aggregate_internal(def: tdef; anonymous: boolean); virtual;
  295. { when building an anonymous record, we cannot immediately insert the
  296. alignment before it in case it's nested, since we only know the required
  297. alignment once all fields have been inserted -> mark the location before
  298. the anonymous record, and insert the alignment once it's finished }
  299. procedure mark_anon_aggregate_alignment; virtual; abstract;
  300. procedure insert_marked_aggregate_alignment(def: tdef); virtual; abstract;
  301. class function get_vectorized_dead_strip_section_symbol(const basename: string; st: tsymtable; options: ttcdeadstripsectionsymboloptions; start: boolean): tasmsymbol; virtual;
  302. public
  303. class function get_vectorized_dead_strip_custom_section_name(const basename: TSymStr; st: tsymtable; options: ttcasmlistoptions; out secname: TSymStr): boolean; virtual;
  304. { get the start/end symbol for a dead stripable vectorized section, such
  305. as the resourcestring data of a unit }
  306. class function get_vectorized_dead_strip_section_symbol_start(const basename: string; st: tsymtable; options: ttcdeadstripsectionsymboloptions): tasmsymbol; virtual;
  307. class function get_vectorized_dead_strip_section_symbol_end(const basename: string; st: tsymtable; options: ttcdeadstripsectionsymboloptions): tasmsymbol; virtual;
  308. { returns true if smartlinking of the dead stripable vectorized lists is supported }
  309. class function is_smartlink_vectorized_dead_strip: boolean; virtual;
  310. class function get_dynstring_rec_name(typ: tstringtype; winlike: boolean; len: asizeint): TSymStr;
  311. class function get_dynstring_rec(typ: tstringtype; winlike: boolean; len: asizeint): trecorddef;
  312. { the datalist parameter specifies where the data for the string constant
  313. will be emitted (via an internal data builder) }
  314. function emit_ansistring_const(datalist: TAsmList; data: pchar; len: asizeint; encoding: tstringencoding): tasmlabofs;
  315. function emit_unicodestring_const(datalist: TAsmList; data: tcompilerwidestring; encoding: tstringencoding; winlike: boolean):tasmlabofs;
  316. { emits a tasmlabofs as returned by emit_*string_const }
  317. procedure emit_string_offset(const ll: tasmlabofs; const strlength: longint; const st: tstringtype; const winlikewidestring: boolean; const charptrdef: tdef);virtual;
  318. { emits a tasmlabofs as returned by begin_dynarray_const }
  319. procedure emit_dynarray_offset(const ll:tasmlabofs;const arrlength:asizeint;const arrdef:tarraydef; const arrconstdatadef: trecorddef);virtual;
  320. { starts a dynamic array constant so that its data can be emitted directly afterwards }
  321. function begin_dynarray_const(arrdef:tdef;var startlab:tasmlabel;out arrlengthloc:ttypedconstplaceholder):tasmlabofs;virtual;
  322. { ends a dynamic array constant by updating the count field; the arrlengthloc will be freed }
  323. function end_dynarray_const(arrdef:tdef;arrlength:asizeint;arrlengthloc:ttypedconstplaceholder;llofs:tasmlabofs):tdef;virtual;
  324. { emit a shortstring constant, and return its def }
  325. function emit_shortstring_const(const str: shortstring): tdef;
  326. { emit a pchar string constant (the characters, not a pointer to them), and return its def }
  327. function emit_pchar_const(str: pchar; len: pint; copypchar: boolean): tdef;
  328. { emit a guid constant }
  329. procedure emit_guid_const(const guid: tguid);
  330. { emit a procdef constant }
  331. procedure emit_procdef_const(pd: tprocdef);
  332. { emit an ordinal constant }
  333. procedure emit_ord_const(value: int64; def: tdef);
  334. { emit a reference to a pooled shortstring constant }
  335. procedure emit_pooled_shortstring_const_ref(const str:shortstring);
  336. { begin a potential aggregate type. Must be called for any type
  337. that consists of multiple tai constant data entries, or that
  338. represents an aggregate at the Pascal level (a record, a non-dynamic
  339. array, ... }
  340. procedure maybe_begin_aggregate(def: tdef);
  341. { end a potential aggregate type. Must be paired with every
  342. maybe_begin_aggregate }
  343. procedure maybe_end_aggregate(def: tdef);
  344. { similar as above, but in case
  345. a) it's definitely a record
  346. b) the def of the record should be automatically constructed based on
  347. the types of the emitted fields
  348. packrecords: same as "packrecords x"
  349. recordalign: specify the (minimum) alignment of the start of the record
  350. (no equivalent in source code), used as an alternative for explicit
  351. align statements. Use "1" if it should be calculated based on the
  352. fields
  353. recordalignmin: same as "codealign recordmin=x"
  354. maxcrecordalign: specify maximum C record alignment (no equivalent in
  355. source code)
  356. }
  357. function begin_anonymous_record(const optionalname: string; packrecords, recordalign, recordalignmin: shortint): trecorddef; virtual;
  358. function end_anonymous_record: trecorddef; virtual;
  359. { add a placeholder element at the current position that later can be
  360. filled in with the actual data (via ttypedconstplaceholder.replace)
  361. useful in case you have table preceded by the number of elements, and
  362. you count the elements while building the table }
  363. function emit_placeholder(def: tdef): ttypedconstplaceholder; virtual; abstract;
  364. { Add a comment line
  365. }
  366. procedure emit_comment(const comment : string);
  367. protected
  368. { common code to check whether a placeholder can be added at the current
  369. position }
  370. procedure check_add_placeholder(def: tdef);
  371. public
  372. { The next group of routines are for constructing complex expressions.
  373. While parsing a typed constant these operators are encountered from
  374. outer to inner, so that is also the order in which they should be
  375. added to the queue. Only one queue can be active at a time. }
  376. { Init the queue. Gives an internalerror if a queue was already active }
  377. procedure queue_init(todef: tdef); virtual;
  378. { queue an array/string indexing operation (performs all range checking,
  379. so it doesn't have to be duplicated in all descendents). }
  380. procedure queue_vecn(def: tdef; const index: tconstexprint); virtual;
  381. { queue a subscripting operation }
  382. procedure queue_subscriptn(def: tabstractrecorddef; vs: tfieldvarsym); virtual;
  383. { queue indexing a record recursively via several field names. The fields
  384. are specified in the inner to outer order (i.e., def.field1.field2) }
  385. function queue_subscriptn_multiple_by_name(def: tabstractrecorddef; const fields: array of TIDString): tdef;
  386. { queue a type conversion operation }
  387. procedure queue_typeconvn(fromdef, todef: tdef); virtual;
  388. { queue a pointer add operation }
  389. procedure queue_pointeraddn(def: tpointerdef; const index: tconstexprint); virtual;
  390. { queue a pointer sub operation }
  391. procedure queue_pointersubn(def: tpointerdef; const index: tconstexprint); virtual;
  392. { finalise the queue (so a new one can be created) and flush the
  393. previously queued operations, applying them in reverse order on a...}
  394. { ... procdef }
  395. procedure queue_emit_proc(pd: tprocdef); virtual;
  396. { ... staticvarsym }
  397. procedure queue_emit_staticvar(vs: tstaticvarsym); virtual;
  398. { ... labelsym }
  399. procedure queue_emit_label(l: tlabelsym); virtual;
  400. { ... constsym }
  401. procedure queue_emit_const(cs: tconstsym); virtual;
  402. { ... asmsym/asmlabel }
  403. procedure queue_emit_asmsym(sym: tasmsymbol; def: tdef); virtual;
  404. { ... an ordinal constant }
  405. procedure queue_emit_ordconst(value: int64; def: tdef); virtual;
  406. protected
  407. { returns whether queue_init has been called without a corresponding
  408. queue_emit_* to finish it }
  409. function queue_is_active: boolean;
  410. public
  411. { finalize the internal asmlist (if necessary) and return it.
  412. This asmlist will be freed when the builder is destroyed, so add its
  413. contents to another list first. This property should only be accessed
  414. once all data has been added. }
  415. function get_final_asmlist(asmsym: tasmsymbol; sym: tsym; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: longint): tasmlist;
  416. { same as above, passes nil as sym }
  417. function get_final_asmlist(asmsym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: longint): tasmlist; inline;
  418. function get_final_asmlist_vectorized_dead_strip(sym: tsym; def: tdef; const basename, itemname: TSymStr; st: TSymtable; alignment: longint): tasmlist;
  419. { returns the offset of the string data relative to ansi/unicode/widestring
  420. constant labels. On most platforms, this is 0 (with the header at a
  421. negative offset), but on some platforms such negative offsets are not
  422. supported this is equal to the header size }
  423. class function get_string_symofs(typ: tstringtype; winlikewidestring: boolean): pint; virtual;
  424. { returns the offset of the array data relatve to dynamic array constant
  425. labels. On most platforms, this is 0 (with the header at a negative
  426. offset), but on some platforms such negative offsets are not supported
  427. and thus this is equal to the header size }
  428. class function get_dynarray_symofs:pint;virtual;
  429. { set the fieldvarsym whose data we will emit next; needed
  430. in case of variant records, so we know which part of the variant gets
  431. initialised. Also in case of objects, because the fieldvarsyms are spread
  432. over the symtables of the entire inheritance tree }
  433. property next_field: tfieldvarsym write set_next_field;
  434. { set the name of the next field that will be emitted for an anonymous
  435. record (also if that field is a nested anonymous record) }
  436. property next_field_name: TIDString write set_next_field_name;
  437. protected
  438. { these ones always return the actual offset, called by the above (and
  439. overridden versions) }
  440. class function get_string_header_size(typ: tstringtype; winlikewidestring: boolean): pint;
  441. class function get_dynarray_header_size:pint;
  442. end;
  443. ttai_typedconstbuilderclass = class of ttai_typedconstbuilder;
  444. tlowlevelaggregateinformation = class(taggregateinformation)
  445. protected
  446. fanonrecmarker: tai;
  447. public
  448. property anonrecmarker: tai read fanonrecmarker write fanonrecmarker;
  449. end;
  450. tlowleveltypedconstplaceholder = class(ttypedconstplaceholder)
  451. list: tasmlist;
  452. insertpos: tai;
  453. constructor create(l: tasmlist; pos: tai; d: tdef);
  454. procedure replace(ai: tai; d: tdef); override;
  455. end;
  456. ttai_lowleveltypedconstbuilder = class(ttai_typedconstbuilder)
  457. protected
  458. procedure mark_anon_aggregate_alignment; override;
  459. procedure insert_marked_aggregate_alignment(def: tdef); override;
  460. procedure finalize_asmlist(asmsym: tasmsymbol; sym: tsym; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions); override;
  461. public
  462. { set the default value for caggregateinformation (= tlowlevelaggregateinformation) }
  463. class constructor classcreate;
  464. function emit_placeholder(def: tdef): ttypedconstplaceholder; override;
  465. end;
  466. var
  467. ctai_typedconstbuilder: ttai_typedconstbuilderclass;
  468. implementation
  469. uses
  470. cutils,
  471. verbose,globals,systems,
  472. fmodule,
  473. symtable,symutil,defutil;
  474. {****************************************************************************
  475. taggregateinformation
  476. ****************************************************************************}
  477. function taggregateinformation.getcuroffset: asizeint;
  478. var
  479. field: tfieldvarsym;
  480. begin
  481. if assigned(curfield) then
  482. result:=curfield.fieldoffset+curfield.vardef.size
  483. else if curindex<>-1 then
  484. begin
  485. field:=tfieldvarsym(tabstractrecorddef(def).symtable.symlist[curindex]);
  486. result:=field.fieldoffset+field.vardef.size
  487. end
  488. else
  489. result:=0
  490. end;
  491. procedure taggregateinformation.setnextfieldname(const AValue: TIDString);
  492. begin
  493. if (fnextfieldname<>'') or
  494. not anonrecord then
  495. internalerror(2015071503);
  496. fnextfieldname:=AValue;
  497. end;
  498. constructor taggregateinformation.create(_def: tdef; _typ: ttypedconstkind);
  499. begin
  500. fdef:=_def;
  501. ftyp:=_typ;
  502. fcurindex:=-1;
  503. fnextindex:=-1;
  504. end;
  505. function taggregateinformation.prepare_next_field(nextfielddef: tdef): asizeint;
  506. var
  507. sym: tsym;
  508. currentoffset,nextoffset: asizeint;
  509. i: longint;
  510. begin
  511. { get the next field and its offset, and make that next field the current
  512. one }
  513. if assigned(nextfield) then
  514. begin
  515. nextoffset:=nextfield.fieldoffset;
  516. currentoffset:=curoffset;
  517. curfield:=nextfield;
  518. end
  519. else
  520. begin
  521. { must set nextfield for unions and objects, as we cannot
  522. automatically detect the "next" field in that case }
  523. if ((def.typ=recorddef) and
  524. trecorddef(def).isunion) or
  525. is_object(def) then
  526. internalerror(2014091202);
  527. { if we are constructing this record as data gets emitted, add a field
  528. for this data }
  529. if anonrecord then
  530. begin
  531. trecorddef(def).add_field_by_def(fnextfieldname,nextfielddef);
  532. fnextfieldname:='';
  533. end
  534. else if fnextfieldname<>'' then
  535. internalerror(2015071501);
  536. currentoffset:=curoffset;
  537. { find next field }
  538. i:=curindex;
  539. repeat
  540. inc(i);
  541. sym:=tsym(tabstractrecorddef(def).symtable.symlist[i]);
  542. until is_normal_fieldvarsym(sym);
  543. curfield:=tfieldvarsym(sym);
  544. nextoffset:=curfield.fieldoffset;
  545. curindex:=i;
  546. end;
  547. { need padding? }
  548. result:=nextoffset-currentoffset;
  549. end;
  550. {****************************************************************************
  551. ttypedconstplaceholder
  552. ****************************************************************************}
  553. constructor ttypedconstplaceholder.create(d: tdef);
  554. begin
  555. def:=d;
  556. end;
  557. {****************************************************************************
  558. tai_abstracttypedconst
  559. ****************************************************************************}
  560. procedure tai_abstracttypedconst.setdef(def: tdef);
  561. begin
  562. { should not be changed, rewrite the calling code if this happens }
  563. if assigned(fdef) then
  564. Internalerror(2014080203);
  565. fdef:=def;
  566. end;
  567. constructor tai_abstracttypedconst.create(_adetyp: ttypedconstkind; _def: tdef);
  568. begin
  569. inherited create;
  570. typ:=ait_typedconst;
  571. fadetyp:=_adetyp;
  572. fdef:=_def;
  573. end;
  574. {****************************************************************************
  575. tai_simpletypedconst
  576. ****************************************************************************}
  577. procedure tai_simpletypedconst.setval(AValue: tai);
  578. begin
  579. fval:=AValue;
  580. end;
  581. constructor tai_simpletypedconst.create(_def: tdef; _val: tai);
  582. begin
  583. inherited create(tck_simple,_def);
  584. fval:=_val;
  585. end;
  586. destructor tai_simpletypedconst.destroy;
  587. begin
  588. fval.free;
  589. inherited destroy;
  590. end;
  591. {****************************************************************************
  592. tai_aggregatetypedconst.tadeenumerator
  593. ****************************************************************************}
  594. constructor tai_aggregatetypedconst.tadeenumerator.create(data: tai_aggregatetypedconst);
  595. begin
  596. fvalues:=data.fvalues;
  597. fvaluespos:=-1;
  598. end;
  599. function tai_aggregatetypedconst.tadeenumerator.getcurrent: tai_abstracttypedconst;
  600. begin
  601. result:=tai_abstracttypedconst(fvalues[fvaluespos]);
  602. end;
  603. function tai_aggregatetypedconst.tadeenumerator.movenext: boolean;
  604. begin
  605. if fvaluespos<pred(fvalues.count) then
  606. begin
  607. inc(fvaluespos);
  608. result:=true
  609. end
  610. else
  611. result:=false;
  612. end;
  613. procedure tai_aggregatetypedconst.tadeenumerator.reset;
  614. begin
  615. fvaluespos:=0
  616. end;
  617. {****************************************************************************
  618. tai_aggregatetypedconst
  619. ****************************************************************************}
  620. procedure tai_aggregatetypedconst.convert_to_string;
  621. var
  622. ai: tai_abstracttypedconst;
  623. newstr: tai_string;
  624. begin
  625. newstr:=tai_string.Create('');
  626. for ai in self do
  627. begin
  628. if ai.adetyp<>tck_simple then
  629. internalerror(2014070103);
  630. add_to_string(newstr,tai_simpletypedconst(ai).val);
  631. ai.free;
  632. end;
  633. fvalues.count:=0;
  634. { the "nil" def will be replaced with an array def of the appropriate
  635. size once we're finished adding data, so we don't create intermediate
  636. arraydefs all the time }
  637. fvalues.add(tai_simpletypedconst.create(nil,newstr));
  638. end;
  639. procedure tai_aggregatetypedconst.add_to_string(strtai: tai_string; othertai: tai);
  640. var
  641. len1,len2,lent : Integer;
  642. lother_string : tai_string absolute othertai;
  643. begin
  644. case othertai.typ of
  645. ait_string:
  646. begin
  647. // lengths without terminating 0
  648. len1:=length(strtai.str)-1;
  649. len2:=length(lother_string.str)-1;
  650. lent:=len1+len2;
  651. SetLength(strtai.str,lent+1);
  652. { also copy null terminator }
  653. move(lother_string.str[0],strtai.str[len1],len2+1);
  654. end;
  655. ait_const:
  656. begin
  657. if tai_const(othertai).size<>1 then
  658. internalerror(2014070101);
  659. { it was already len+1 to hold the #0 -> realloc to len+2 }
  660. len1:=length(strtai.str);
  661. SetLength(strtai.str,len1+1);
  662. strtai.str[len1-1]:=ansichar(tai_const(othertai).value);
  663. strtai.str[len1]:=#0;
  664. end;
  665. else
  666. internalerror(2014070102);
  667. end;
  668. end;
  669. constructor tai_aggregatetypedconst.create(_adetyp: ttypedconstkind; _fdef: tdef);
  670. begin
  671. inherited;
  672. fisstring:=false;
  673. fvalues:=tfpobjectlist.create(true);
  674. end;
  675. function tai_aggregatetypedconst.getenumerator: tadeenumerator;
  676. begin
  677. result:=tadeenumerator.create(self);
  678. end;
  679. procedure tai_aggregatetypedconst.addvalue(val: tai_abstracttypedconst);
  680. begin
  681. { merge string constants and ordinal constants added in an array of
  682. char, to unify the length and the string data }
  683. if fisstring or
  684. ((val.adetyp=tck_simple) and
  685. (tai_simpletypedconst(val).val.typ=ait_string)) then
  686. begin
  687. if not fisstring and
  688. (fvalues.count>0) then
  689. convert_to_string;
  690. fisstring:=true;
  691. case fvalues.count of
  692. 0: fvalues.add(val);
  693. 1:
  694. begin
  695. add_to_string(tai_string(tai_simpletypedconst(fvalues[0]).val),tai_simpletypedconst(val).val);
  696. val.free
  697. end
  698. else
  699. internalerror(2014070104);
  700. end;
  701. end
  702. else
  703. fvalues.add(val);
  704. end;
  705. function tai_aggregatetypedconst.valuecount: longint;
  706. begin
  707. result:=fvalues.count;
  708. end;
  709. procedure tai_aggregatetypedconst.insertvaluebeforepos(val: tai_abstracttypedconst; pos: longint);
  710. begin
  711. fvalues.insert(pos,val);
  712. end;
  713. procedure tai_aggregatetypedconst.replacevalueatpos(val: tai_abstracttypedconst; pos: longint);
  714. begin
  715. { since fvalues owns its elements, it will automatically free the old value }
  716. fvalues[pos]:=val;
  717. end;
  718. procedure tai_aggregatetypedconst.changetorecord(_def: trecorddef);
  719. begin
  720. { must be a record of the same size as the current data }
  721. if assigned(fdef) and
  722. (fdef.size<>_def.size) then
  723. internalerror(2015122402);
  724. fdef:=_def;
  725. fadetyp:=tck_record;
  726. end;
  727. procedure tai_aggregatetypedconst.finish;
  728. var
  729. lString : tai_string;
  730. len : integer;
  731. begin
  732. if fisstring then
  733. begin
  734. { set the def: an array of char with the same length as the string
  735. data }
  736. if fvalues.count<>1 then
  737. internalerror(2014070105);
  738. lString:=tai_string(tai_simpletypedconst(fvalues[0]).val);
  739. len:=length(lString.str)-1;
  740. tai_simpletypedconst(fvalues[0]).fdef:=carraydef.getreusable(cansichartype,len);
  741. end;
  742. end;
  743. destructor tai_aggregatetypedconst.destroy;
  744. begin
  745. fvalues.free;
  746. inherited destroy;
  747. end;
  748. {*****************************************************************************
  749. ttai_typedconstbuilder
  750. *****************************************************************************}
  751. function ttai_typedconstbuilder.getcurragginfo: taggregateinformation;
  752. begin
  753. if assigned(faggregateinformation) and
  754. (faggregateinformation.count>0) then
  755. result:=taggregateinformation(faggregateinformation[faggregateinformation.count-1])
  756. else
  757. result:=nil;
  758. end;
  759. procedure ttai_typedconstbuilder.set_next_field(AValue: tfieldvarsym);
  760. var
  761. info: taggregateinformation;
  762. begin
  763. info:=curagginfo;
  764. if not assigned(info) then
  765. internalerror(2014091206);
  766. info.nextfield:=AValue;
  767. end;
  768. procedure ttai_typedconstbuilder.set_next_field_name(const AValue: TIDString);
  769. var
  770. info: taggregateinformation;
  771. begin
  772. info:=curagginfo;
  773. if not assigned(info) then
  774. internalerror(2015071502);
  775. info.nextfieldname:='$'+AValue;
  776. end;
  777. procedure ttai_typedconstbuilder.pad_next_field(nextfielddef: tdef);
  778. var
  779. fillbytes: asizeint;
  780. begin
  781. fillbytes:=curagginfo.prepare_next_field(nextfielddef);
  782. while fillbytes>0 do
  783. begin
  784. do_emit_tai(tai_const.create_8bit(0),u8inttype);
  785. dec(fillbytes);
  786. end;
  787. end;
  788. function ttai_typedconstbuilder.get_internal_data_section_index(typ: TAsmSectiontype): longint;
  789. begin
  790. { avoid wrong warning by -Oodfa }
  791. result:=-1;
  792. for result:=low(finternal_data_section_info) to high(finternal_data_section_info) do
  793. if finternal_data_section_info[result].sectype=typ then
  794. exit;
  795. result:=-1;
  796. end;
  797. function ttai_typedconstbuilder.get_internal_data_section_start_label: tasmlabel;
  798. begin
  799. { on Darwin, dead code/data stripping happens based on non-temporary
  800. labels (any label that doesn't start with "L" -- it doesn't have
  801. to be global) }
  802. if target_info.system in systems_darwin then
  803. current_asmdata.getstaticdatalabel(result)
  804. else if create_smartlink_library then
  805. current_asmdata.getglobaldatalabel(result)
  806. else
  807. current_asmdata.getlocaldatalabel(result);
  808. end;
  809. function ttai_typedconstbuilder.get_internal_data_section_internal_label: tasmlabel;
  810. begin
  811. if create_smartlink_library then
  812. { all labels need to be global in case they're in another object }
  813. current_asmdata.getglobaldatalabel(result)
  814. else
  815. { no special requirement for the label -> just get a local one }
  816. current_asmdata.getlocaldatalabel(result);
  817. end;
  818. procedure ttai_typedconstbuilder.add_link_ordered_symbol(sym: tasmsymbol; const secname: TSymStr);
  819. begin
  820. current_module.linkorderedsymbols.concat(sym.Name);
  821. end;
  822. function ttai_typedconstbuilder.aggregate_kind(def: tdef): ttypedconstkind;
  823. begin
  824. if (def.typ in [recorddef,filedef,variantdef]) or
  825. is_object(def) or
  826. ((def.typ=procvardef) and
  827. not tprocvardef(def).is_addressonly and
  828. not is_block(def)) then
  829. result:=tck_record
  830. else if ((def.typ=arraydef) and
  831. not is_dynamic_array(def)) or
  832. ((def.typ=setdef) and
  833. not is_smallset(def)) or
  834. is_shortstring(def) then
  835. result:=tck_array
  836. else
  837. result:=tck_simple;
  838. end;
  839. procedure ttai_typedconstbuilder.finalize_asmlist_prepare(const options: ttcasmlistoptions; var alignment: shortint);
  840. begin
  841. if tcalo_apply_constalign in options then
  842. alignment:=const_align(alignment);
  843. { have we finished all aggregates? }
  844. if (getcurragginfo<>nil) and
  845. { in case of syntax errors, the aggregate may not have been finished }
  846. (ErrorCount=0) then
  847. internalerror(2015072301);
  848. { must call finalize_vectorized_dead_strip_asmlist() instead }
  849. if (([tcalo_vectorized_dead_strip_start,
  850. tcalo_vectorized_dead_strip_item,
  851. tcalo_vectorized_dead_strip_end]*options)<>[]) and
  852. not fvectorized_finalize_called then
  853. internalerror(2015110602);
  854. end;
  855. procedure ttai_typedconstbuilder.finalize_asmlist(asmsym: tasmsymbol; sym: tsym; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions);
  856. var
  857. prelist: tasmlist;
  858. begin
  859. finalize_asmlist_prepare(options, alignment);
  860. prelist:=tasmlist.create;
  861. { only now add items based on the symbolname, because it may be
  862. modified by the "section" specifier in case of a typed constant }
  863. { both in case the data should be dead strippable and never dead
  864. stripped, it should be in a separate section (so this property doesn't
  865. affect other data) }
  866. if ([tcalo_no_dead_strip,tcalo_make_dead_strippable]*options)<>[] then
  867. begin
  868. maybe_new_object_file(prelist);
  869. { we always need a new section here, since if we started a new
  870. object file then we have to say what the section is, and otherwise
  871. we need a new section because that's how the dead stripping works }
  872. new_section(prelist,section,secname,alignment);
  873. end
  874. else if tcalo_new_section in options then
  875. new_section(prelist,section,secname,alignment)
  876. else
  877. prelist.concat(cai_align.Create(alignment));
  878. { On Darwin, use .reference to ensure the data doesn't get dead stripped.
  879. On other platforms, the data must be in the .fpc section (which is
  880. kept via the linker script) }
  881. if tcalo_no_dead_strip in options then
  882. begin
  883. if (target_info.system in systems_darwin) then
  884. begin
  885. { Objective-C section declarations contain "no_dead_strip"
  886. attributes if none of their symbols need to be stripped -> don't
  887. add extra ".reference" statement for their symbols (gcc/clang
  888. don't either) }
  889. if not(section in [low(TObjCAsmSectionType)..high(TObjCAsmSectionType)]) then
  890. prelist.concat(tai_directive.Create(asd_reference,asmsym.name))
  891. end
  892. else if section<>sec_fpc then
  893. internalerror(2015101402);
  894. end;
  895. if not(tcalo_is_lab in options) then
  896. if asmsym.bind=AB_LOCAL then
  897. prelist.concat(tai_symbol.Create(asmsym,0))
  898. else
  899. prelist.concat(tai_symbol.Create_Global(asmsym,0))
  900. else
  901. prelist.concat(tai_label.Create(tasmlabel(asmsym)));
  902. { Newly referenced symbol }
  903. asmsym.increfs;
  904. if tcalo_weak in options then
  905. prelist.concat(tai_directive.Create(asd_weak_definition,asmsym.name));
  906. { insert the symbol information before the data }
  907. fasmlist.insertlist(prelist);
  908. { end of the symbol }
  909. fasmlist.concat(tai_symbol_end.Createname(asmsym.name));
  910. { free the temporary list }
  911. prelist.free;
  912. end;
  913. procedure ttai_typedconstbuilder.finalize_asmlist_add_indirect_sym(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions);
  914. var
  915. ptrdef : tdef;
  916. symind : tasmsymbol;
  917. indtcb : ttai_typedconstbuilder;
  918. indsecname : tsymstr;
  919. begin
  920. if (tcalo_data_force_indirect in options) and
  921. (sym.bind in [AB_GLOBAL,AB_COMMON]) and
  922. (sym.typ=AT_DATA) then
  923. begin
  924. ptrdef:=cpointerdef.getreusable(def);
  925. symind:=current_asmdata.DefineAsmSymbol(sym.name,AB_INDIRECT,AT_DATA,ptrdef);
  926. { reuse the section if possible }
  927. if section=sec_rodata then
  928. indsecname:=secname
  929. else
  930. indsecname:=lower(symind.name);
  931. indtcb:=ctai_typedconstbuilder.create([tcalo_new_section,tcalo_make_dead_strippable]);
  932. indtcb.emit_tai(tai_const.create_sym_offset(sym,0),ptrdef);
  933. current_asmdata.asmlists[al_indirectglobals].concatlist(indtcb.get_final_asmlist(
  934. symind,
  935. ptrdef,
  936. sec_rodata,
  937. indsecname,
  938. ptrdef.alignment));
  939. symind.increfs;
  940. indtcb.free;
  941. if not (target_info.system in systems_indirect_var_imports) then
  942. current_module.add_public_asmsym(symind.name,AB_INDIRECT,AT_DATA);
  943. end;
  944. end;
  945. procedure ttai_typedconstbuilder.finalize_vectorized_dead_strip_asmlist(sym: tsym; def: tdef; const basename, itemname: TSymStr; st: tsymtable; alignment: shortint; options: ttcasmlistoptions);
  946. var
  947. asmsym: tasmsymbol;
  948. secname: TSymStr;
  949. sectype: TAsmSectiontype;
  950. asmtype : TAsmsymtype;
  951. customsecname: boolean;
  952. dsopts : ttcdeadstripsectionsymboloptions;
  953. begin
  954. fvectorized_finalize_called:=true;
  955. asmsym:=nil;
  956. customsecname:=get_vectorized_dead_strip_custom_section_name(basename,st,options,secname);
  957. if customsecname then
  958. sectype:=sec_user
  959. else
  960. sectype:=sec_data;
  961. dsopts:=[tcdssso_define];
  962. if tcalo_is_public_asm in options then
  963. include(dsopts,tcdssso_register_asmsym);
  964. if tcalo_data_force_indirect in options then
  965. include(dsopts,tcdssso_use_indirect);
  966. if tcalo_vectorized_dead_strip_start in options then
  967. begin
  968. { the start and end names are predefined }
  969. if itemname<>'' then
  970. internalerror(2015110801);
  971. asmsym:=get_vectorized_dead_strip_section_symbol_start(basename,st,dsopts);
  972. if not customsecname then
  973. secname:=make_mangledname(basename,st,'1_START');
  974. end
  975. else if tcalo_vectorized_dead_strip_end in options then
  976. begin
  977. { the start and end names are predefined }
  978. if itemname<>'' then
  979. internalerror(2015110802);
  980. asmsym:=get_vectorized_dead_strip_section_symbol_end(basename,st,dsopts);
  981. if not customsecname then
  982. secname:=make_mangledname(basename,st,'3_END');
  983. end
  984. else if tcalo_vectorized_dead_strip_item in options then
  985. begin
  986. if tcalo_data_force_indirect in options then
  987. asmtype:=AT_DATA_FORCEINDIRECT
  988. else
  989. asmtype:=AT_DATA;
  990. asmsym:=current_asmdata.DefineAsmSymbol(make_mangledname(basename,st,itemname),AB_GLOBAL,asmtype,def);
  991. if tcalo_is_public_asm in options then
  992. current_module.add_public_asmsym(asmsym);
  993. if not customsecname then
  994. secname:=make_mangledname(basename,st,'2_'+itemname);
  995. end;
  996. add_link_ordered_symbol(asmsym,secname);
  997. if is_smartlink_vectorized_dead_strip then
  998. options:=options+[tcalo_new_section,tcalo_make_dead_strippable]
  999. else
  1000. begin
  1001. { if smartlinking of vectorized lists is not supported,
  1002. put the whole list into a single section. }
  1003. options:=options-[tcalo_new_section,tcalo_make_dead_strippable];
  1004. if tcalo_vectorized_dead_strip_start in options then
  1005. include(options,tcalo_new_section);
  1006. end;
  1007. finalize_asmlist(asmsym,sym,def,sectype,secname,alignment,options);
  1008. end;
  1009. procedure ttai_typedconstbuilder.do_emit_tai(p: tai; def: tdef);
  1010. begin
  1011. { by default we don't care about the type }
  1012. fasmlist.concat(p);
  1013. end;
  1014. function ttai_typedconstbuilder.get_final_asmlist(asmsym: tasmsymbol; sym: tsym; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: longint): tasmlist;
  1015. begin
  1016. if not fasmlist_finalized then
  1017. begin
  1018. finalize_asmlist(asmsym,sym,def,section,secname,alignment,foptions);
  1019. finalize_asmlist_add_indirect_sym(asmsym,def,section,secname,alignment,foptions);
  1020. fasmlist_finalized:=true;
  1021. end;
  1022. result:=fasmlist;
  1023. end;
  1024. function ttai_typedconstbuilder.get_final_asmlist(asmsym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: longint): tasmlist; inline;
  1025. begin
  1026. result:=get_final_asmlist(asmsym,nil,def,section,secname,alignment);
  1027. end;
  1028. function ttai_typedconstbuilder.get_final_asmlist_vectorized_dead_strip(sym: tsym; def: tdef; const basename, itemname: TSymStr; st: TSymtable; alignment: longint): tasmlist;
  1029. begin
  1030. if not fasmlist_finalized then
  1031. begin
  1032. finalize_vectorized_dead_strip_asmlist(sym,def,basename,itemname,st,alignment,foptions);
  1033. fasmlist_finalized:=true;
  1034. end;
  1035. result:=fasmlist;
  1036. end;
  1037. class function ttai_typedconstbuilder.get_string_symofs(typ: tstringtype; winlikewidestring: boolean): pint;
  1038. begin
  1039. { darwin's linker does not support negative offsets }
  1040. if not(target_info.system in systems_darwin+systems_wasm) and
  1041. { it seems that clang's assembler has a bug with the ADRP instruction... }
  1042. (target_info.system<>system_aarch64_win64) then
  1043. result:=0
  1044. else
  1045. result:=get_string_header_size(typ,winlikewidestring);
  1046. end;
  1047. class function ttai_typedconstbuilder.get_dynarray_symofs:pint;
  1048. begin
  1049. { darwin's linker does not support negative offsets }
  1050. if not (target_info.system in systems_darwin) and
  1051. { it seems that clang's assembler has a bug with the ADRP instruction... }
  1052. (target_info.system<>system_aarch64_win64) then
  1053. result:=0
  1054. else
  1055. result:=get_dynarray_header_size;
  1056. end;
  1057. class function ttai_typedconstbuilder.get_string_header_size(typ: tstringtype; winlikewidestring: boolean): pint;
  1058. var
  1059. ansistring_header_size: pint;
  1060. unicodestring_header_size: pint;
  1061. begin
  1062. ansistring_header_size:=
  1063. { encoding }
  1064. 2 +
  1065. { elesize }
  1066. 2 +
  1067. { reference count }
  1068. {$ifdef cpu64bitaddr}
  1069. s32inttype.size +
  1070. {$else !cpu64bitaddr}
  1071. sizesinttype.size +
  1072. {$endif !cpu64bitaddr}
  1073. { length }
  1074. sizesinttype.size;
  1075. unicodestring_header_size:=ansistring_header_size;
  1076. case typ of
  1077. st_ansistring:
  1078. result:=ansistring_header_size;
  1079. st_unicodestring:
  1080. result:=unicodestring_header_size;
  1081. st_widestring:
  1082. if winlikewidestring then
  1083. result:=0
  1084. else
  1085. result:=unicodestring_header_size;
  1086. else
  1087. result:=0;
  1088. end;
  1089. end;
  1090. class function ttai_typedconstbuilder.get_dynarray_header_size:pint;
  1091. begin
  1092. result:=
  1093. { reference count }
  1094. ptrsinttype.size +
  1095. { high value }
  1096. sizesinttype.size;
  1097. end;
  1098. constructor ttai_typedconstbuilder.create(const options: ttcasmlistoptions);
  1099. begin
  1100. inherited create;
  1101. fasmlist:=tasmlist.create;
  1102. foptions:=options;
  1103. { queue is empty }
  1104. fqueue_offset:=low(fqueue_offset);
  1105. finternal_data_current_section:=sec_none;
  1106. end;
  1107. destructor ttai_typedconstbuilder.destroy;
  1108. begin
  1109. { the queue should have been flushed if it was used
  1110. (but if there were errors, we may have aborted before that happened) }
  1111. if (fqueue_offset<>low(fqueue_offset)) and
  1112. (ErrorCount=0) then
  1113. internalerror(2014062901);
  1114. faggregateinformation.free;
  1115. fasmlist.free;
  1116. inherited destroy;
  1117. end;
  1118. procedure ttai_typedconstbuilder.start_internal_data_builder(list: tasmlist; sectype: TAsmSectiontype; const secname: TSymStr; out tcb: ttai_typedconstbuilder; out l: tasmlabel);
  1119. var
  1120. options: ttcasmlistoptions;
  1121. foundsec: longint;
  1122. begin
  1123. { you can't start multiple concurrent internal data builders for the
  1124. same tcb, finish the first before starting another }
  1125. if finternal_data_current_section<>sec_none then
  1126. internalerror(2016082801);
  1127. { we don't know what was previously added to this list, so always add
  1128. a section header. We'll use the same section name in case multiple
  1129. items are added to the same kind of section (rodata, rodata_no_rel,
  1130. ...), so that everything will still end up in the same section even if
  1131. there are multiple section headers }
  1132. options:=[tcalo_is_lab,tcalo_new_section];
  1133. finternal_data_current_section:=sectype;
  1134. l:=nil;
  1135. { did we already create a section of this type for the internal data of
  1136. this builder? }
  1137. foundsec:=get_internal_data_section_index(sectype);
  1138. if foundsec=-1 then
  1139. begin
  1140. { we only need to start a dead-strippable section of data at the
  1141. start of the first subsection of this kind for this block.
  1142. exception: if dead stripping happens based on objects/libraries,
  1143. then we only have to create a new object file for the first
  1144. internal data section of any kind (all the rest will simply be put
  1145. in the same object file) }
  1146. if create_smartlink then
  1147. begin
  1148. if not create_smartlink_library or
  1149. (length(finternal_data_section_info)=0) then
  1150. include(options,tcalo_make_dead_strippable);
  1151. { on Darwin, dead code/data stripping happens based on non-
  1152. temporary labels (any label that doesn't start with "L" -- it
  1153. doesn't have to be global) -> add a non-temporary lobel at the
  1154. start of every kind of subsection created in this builder }
  1155. if target_info.system in systems_darwin then
  1156. l:=get_internal_data_section_start_label;
  1157. end;
  1158. foundsec:=length(finternal_data_section_info);
  1159. setlength(finternal_data_section_info,foundsec+1);
  1160. finternal_data_section_info[foundsec].sectype:=sectype;
  1161. end;
  1162. if not assigned(finternal_data_asmlist) and
  1163. (cs_create_smart in current_settings.moduleswitches) then
  1164. begin
  1165. l:=get_internal_data_section_start_label;
  1166. { the internal data list should only be assigned by this routine,
  1167. the first time that an internal data block is started }
  1168. if not assigned(list) or
  1169. assigned(finternal_data_asmlist) then
  1170. internalerror(2015032101);
  1171. finternal_data_asmlist:=list;
  1172. end
  1173. { all internal data for this tcb must go to the same list (otherwise all
  1174. data we want to add to the dead-strippable block is not guaranteed to
  1175. be sequential and e.g. in the same object file in case of library-based
  1176. dead stripping) }
  1177. else if (assigned(finternal_data_asmlist) and
  1178. (list<>finternal_data_asmlist)) or
  1179. not assigned(list) then
  1180. internalerror(2015032102);
  1181. finternal_data_asmlist:=list;
  1182. if not assigned(l) then
  1183. l:=get_internal_data_section_internal_label;
  1184. { first section of this kind -> set name }
  1185. if finternal_data_section_info[foundsec].secname='' then
  1186. if secname='' then
  1187. finternal_data_section_info[foundsec].secname:=l.Name
  1188. else
  1189. finternal_data_section_info[foundsec].secname:=secname
  1190. { if the name is specified multiple times, it must match }
  1191. else if (secname<>'') and
  1192. (finternal_data_section_info[foundsec].secname<>secname) then
  1193. internalerror(2015032401);
  1194. tcb:=ttai_typedconstbuilderclass(classtype).create(options);
  1195. end;
  1196. procedure ttai_typedconstbuilder.finish_internal_data_builder(var tcb: ttai_typedconstbuilder; l: tasmlabel; def: tdef; alignment: longint);
  1197. begin
  1198. finternal_data_asmlist.concatList(tcb.get_final_asmlist(l,def,
  1199. finternal_data_current_section,
  1200. finternal_data_section_info[get_internal_data_section_index(finternal_data_current_section)].secname,
  1201. alignment));
  1202. tcb.free;
  1203. tcb:=nil;
  1204. finternal_data_current_section:=sec_none;
  1205. end;
  1206. procedure ttai_typedconstbuilder.emit_tai(p: tai; def: tdef);
  1207. var
  1208. kind: ttypedconstkind;
  1209. info: taggregateinformation;
  1210. begin
  1211. { these elements can be aggregates themselves, e.g. a shortstring can
  1212. be emitted as a series of bytes and char arrays }
  1213. kind:=aggregate_kind(def);
  1214. info:=curagginfo;
  1215. if (kind<>tck_simple) and
  1216. (not assigned(info) or
  1217. (info.typ<>kind)) then
  1218. internalerror(2014091001);
  1219. { if we're emitting a record, handle the padding bytes, and in case of
  1220. an anonymous record also add the next field }
  1221. if assigned(info) then
  1222. begin
  1223. { queue_init already adds padding }
  1224. if not queue_is_active and
  1225. (is_record(info.def) or
  1226. is_object(info.def)) and
  1227. { may add support for these later }
  1228. not is_packed_record_or_object(info.def) then
  1229. pad_next_field(def);
  1230. end;
  1231. { emit the data }
  1232. do_emit_tai(p,def);
  1233. end;
  1234. procedure ttai_typedconstbuilder.emit_tai_procvar2procdef(p: tai; pvdef: tprocvardef);
  1235. begin
  1236. { nothing special by default, since we don't care about the type }
  1237. emit_tai(p,pvdef);
  1238. end;
  1239. procedure ttai_typedconstbuilder.maybe_emit_tail_padding(def: tdef);
  1240. var
  1241. info: taggregateinformation;
  1242. fillbytes: asizeint;
  1243. begin
  1244. info:=curagginfo;
  1245. if not assigned(info) then
  1246. internalerror(2014091002);
  1247. if def<>info.def then
  1248. internalerror(2014091205);
  1249. if (is_record(def) or
  1250. is_object(def)) and
  1251. not is_packed_record_or_object(def) then
  1252. begin
  1253. fillbytes:=def.size-info.curoffset;
  1254. while fillbytes>0 do
  1255. begin
  1256. do_emit_tai(Tai_const.Create_8bit(0),u8inttype);
  1257. dec(fillbytes)
  1258. end;
  1259. end;
  1260. end;
  1261. function ttai_typedconstbuilder.emit_string_const_common(stringtype: tstringtype; len: asizeint; encoding: tstringencoding; var startlab: tasmlabel): tasmlabofs;
  1262. var
  1263. string_symofs: asizeint;
  1264. charptrdef: tdef;
  1265. elesize: word;
  1266. begin
  1267. result.lab:=startlab;
  1268. result.ofs:=0;
  1269. { pack the data, so that we don't add unnecessary null bytes after the
  1270. constant string }
  1271. begin_anonymous_record('$'+get_dynstring_rec_name(stringtype,false,len),1,sizeof(TConstPtrUInt),1);
  1272. string_symofs:=get_string_symofs(stringtype,false);
  1273. { encoding }
  1274. emit_tai(tai_const.create_16bit(encoding),u16inttype);
  1275. inc(result.ofs,2);
  1276. { element size }
  1277. case stringtype of
  1278. st_ansistring:
  1279. begin
  1280. elesize:=1;
  1281. charptrdef:=charpointertype;
  1282. end;
  1283. st_unicodestring:
  1284. begin
  1285. elesize:=2;
  1286. charptrdef:=widecharpointertype;
  1287. end
  1288. else
  1289. internalerror(2014080401);
  1290. end;
  1291. emit_tai(tai_const.create_16bit(elesize),u16inttype);
  1292. inc(result.ofs,2);
  1293. {$ifdef cpu64bitaddr}
  1294. emit_tai(tai_const.Create_32bit(-1),s32inttype);
  1295. inc(result.ofs,s32inttype.size);
  1296. {$else !cpu64bitaddr}
  1297. emit_tai(tai_const.create_sizeint(-1),sizesinttype);
  1298. inc(result.ofs,sizesinttype.size);
  1299. {$endif !cpu64bitaddr}
  1300. emit_tai(tai_const.create_sizeint(len),sizesinttype);
  1301. inc(result.ofs,sizesinttype.size);
  1302. if string_symofs=0 then
  1303. begin
  1304. { results in slightly more efficient code }
  1305. emit_tai(tai_label.create(result.lab),charptrdef);
  1306. result.ofs:=0;
  1307. { create new label of the same kind (including whether or not the
  1308. name starts with target_asm.labelprefix in case it's AB_LOCAL,
  1309. so we keep the difference depending on whether the original was
  1310. allocated via getstatic/getlocal/getglobal datalabel) }
  1311. startlab:=tasmlabel.create(current_asmdata.AsmSymbolDict,startlab.name+'$strlab',startlab.bind,startlab.typ);
  1312. end;
  1313. { sanity check }
  1314. if result.ofs<>string_symofs then
  1315. internalerror(2012051701);
  1316. end;
  1317. function ttai_typedconstbuilder.get_dynstring_def_for_type(stringtype: tstringtype; winlikewidestring: boolean): tstringdef;
  1318. begin
  1319. if stringtype=st_ansistring then
  1320. result:=tstringdef(cansistringtype)
  1321. else if (stringtype=st_unicodestring) or
  1322. ((stringtype=st_widestring) and
  1323. not winlikewidestring) then
  1324. result:=tstringdef(cunicodestringtype)
  1325. else if stringtype=st_widestring then
  1326. result:=tstringdef(cwidestringtype)
  1327. else
  1328. internalerror(2015122101);
  1329. end;
  1330. procedure ttai_typedconstbuilder.begin_aggregate_internal(def: tdef; anonymous: boolean);
  1331. var
  1332. info: taggregateinformation;
  1333. tck: ttypedconstkind;
  1334. begin
  1335. tck:=aggregate_kind(def);
  1336. if tck=tck_simple then
  1337. exit;
  1338. if not assigned(faggregateinformation) then
  1339. faggregateinformation:=tfpobjectlist.create
  1340. { if we're starting an anonymous record, we can't align it yet because
  1341. the alignment depends on the fields that will be added -> we'll do
  1342. it at the end }
  1343. else if not anonymous or
  1344. ((def.typ<>recorddef) and
  1345. not is_object(def)) then
  1346. begin
  1347. { add padding if necessary, and update the current field/offset }
  1348. info:=curagginfo;
  1349. if (is_record(curagginfo.def) or
  1350. is_object(curagginfo.def)) and
  1351. not is_packed_record_or_object(curagginfo.def) then
  1352. begin
  1353. if queue_is_active then
  1354. internalerror(2015073001);
  1355. pad_next_field(def);
  1356. end;
  1357. end
  1358. { if this is the outer record, no padding is required; the alignment
  1359. has to be specified explicitly in that case via get_final_asmlist() }
  1360. else if assigned(curagginfo) and
  1361. (curagginfo.def.typ=recorddef) then
  1362. { mark where we'll have to insert the padding bytes at the end }
  1363. mark_anon_aggregate_alignment;
  1364. info:=caggregateinformation.create(def,aggregate_kind(def));
  1365. faggregateinformation.add(info);
  1366. end;
  1367. procedure ttai_typedconstbuilder.end_aggregate_internal(def: tdef; anonymous: boolean);
  1368. var
  1369. info: taggregateinformation;
  1370. tck: ttypedconstkind;
  1371. begin
  1372. tck:=aggregate_kind(def);
  1373. if tck=tck_simple then
  1374. exit;
  1375. { add tail padding if necessary }
  1376. maybe_emit_tail_padding(def);
  1377. { pop and free the information }
  1378. info:=curagginfo;
  1379. faggregateinformation.count:=faggregateinformation.count-1;
  1380. info.free;
  1381. end;
  1382. class function ttai_typedconstbuilder.get_vectorized_dead_strip_section_symbol(const basename: string; st: tsymtable; options: ttcdeadstripsectionsymboloptions; start: boolean): tasmsymbol;
  1383. var
  1384. name: TSymStr;
  1385. asmtype : TAsmsymtype;
  1386. begin
  1387. if start then
  1388. name:=make_mangledname(basename,st,'START')
  1389. else
  1390. name:=make_mangledname(basename,st,'END');
  1391. if tcdssso_define in options then
  1392. begin
  1393. if tcdssso_use_indirect in options then
  1394. asmtype:=AT_DATA_FORCEINDIRECT
  1395. else
  1396. asmtype:=AT_DATA;
  1397. result:=current_asmdata.DefineAsmSymbol(name,AB_GLOBAL,asmtype,voidpointertype);
  1398. if tcdssso_register_asmsym in options then
  1399. current_module.add_public_asmsym(result);
  1400. end
  1401. else
  1402. begin
  1403. result:=current_asmdata.RefAsmSymbol(name,AT_DATA,tcdssso_use_indirect in options);
  1404. if tcdssso_register_asmsym in options then
  1405. current_module.add_extern_asmsym(result);
  1406. end;
  1407. end;
  1408. class function ttai_typedconstbuilder.get_vectorized_dead_strip_custom_section_name(const basename: TSymStr; st: tsymtable; options: ttcasmlistoptions; out secname: TSymStr): boolean;
  1409. begin
  1410. result:=false;
  1411. end;
  1412. class function ttai_typedconstbuilder.get_vectorized_dead_strip_section_symbol_start(const basename: string; st: tsymtable; options: ttcdeadstripsectionsymboloptions): tasmsymbol;
  1413. begin
  1414. result:=get_vectorized_dead_strip_section_symbol(basename,st,options,true);
  1415. end;
  1416. class function ttai_typedconstbuilder.get_vectorized_dead_strip_section_symbol_end(const basename: string; st: tsymtable; options: ttcdeadstripsectionsymboloptions): tasmsymbol;
  1417. begin
  1418. result:=get_vectorized_dead_strip_section_symbol(basename,st,options,false);
  1419. end;
  1420. class function ttai_typedconstbuilder.is_smartlink_vectorized_dead_strip: boolean;
  1421. begin
  1422. result:=(tf_smartlink_sections in target_info.flags) and
  1423. (not(target_info.system in systems_darwin) or
  1424. (tf_supports_symbolorderfile in target_info.flags));
  1425. end;
  1426. class function ttai_typedconstbuilder.get_dynstring_rec_name(typ: tstringtype; winlike: boolean; len: asizeint): TSymStr;
  1427. begin
  1428. case typ of
  1429. st_ansistring:
  1430. result:='ansistrrec';
  1431. st_unicodestring,
  1432. st_widestring:
  1433. if (typ=st_unicodestring) or
  1434. not winlike then
  1435. result:='unicodestrrec'
  1436. else
  1437. result:='widestrrec';
  1438. else
  1439. internalerror(2014080402);
  1440. end;
  1441. result:=result+tostr(len);
  1442. end;
  1443. class function ttai_typedconstbuilder.get_dynstring_rec(typ: tstringtype; winlike: boolean; len: asizeint): trecorddef;
  1444. var
  1445. name: TSymStr;
  1446. streledef: tdef;
  1447. strtypesym: ttypesym;
  1448. srsym: tsym;
  1449. srsymtable: tsymtable;
  1450. begin
  1451. name:=get_dynstring_rec_name(typ,winlike,len);
  1452. { search in the interface of all units for the type to reuse it }
  1453. if searchsym_type(name,srsym,srsymtable) then
  1454. begin
  1455. result:=trecorddef(ttypesym(srsym).typedef);
  1456. exit;
  1457. end
  1458. else
  1459. begin
  1460. { also search the implementation of the current unit }
  1461. strtypesym:=try_search_current_module_type(name);
  1462. if assigned(strtypesym) then
  1463. begin
  1464. result:=trecorddef(strtypesym.typedef);
  1465. exit;
  1466. end;
  1467. end;
  1468. if (typ<>st_widestring) or
  1469. not winlike then
  1470. begin
  1471. result:=crecorddef.create_global_internal('$'+name,1,1);
  1472. { encoding }
  1473. result.add_field_by_def('',u16inttype);
  1474. { element size }
  1475. result.add_field_by_def('',u16inttype);
  1476. { elements }
  1477. case typ of
  1478. st_ansistring:
  1479. streledef:=cansichartype;
  1480. st_unicodestring:
  1481. streledef:=cwidechartype;
  1482. else
  1483. internalerror(2016082301);
  1484. end;
  1485. { reference count }
  1486. {$ifdef cpu64bitaddr}
  1487. result.add_field_by_def('',s32inttype);
  1488. {$else !cpu64bitaddr}
  1489. result.add_field_by_def('',sizesinttype);
  1490. {$endif !cpu64bitaddr}
  1491. { length in elements }
  1492. result.add_field_by_def('',sizesinttype);
  1493. end
  1494. else
  1495. begin
  1496. result:=crecorddef.create_global_internal('$'+name,4,
  1497. targetinfos[target_info.system]^.alignment.recordalignmin);
  1498. { length in bytes }
  1499. result.add_field_by_def('',s32inttype);
  1500. streledef:=cwidechartype;
  1501. end;
  1502. { data (include zero terminator) }
  1503. result.add_field_by_def('',carraydef.getreusable(streledef,len+1));
  1504. trecordsymtable(trecorddef(result).symtable).addalignmentpadding;
  1505. end;
  1506. function ttai_typedconstbuilder.emit_ansistring_const(datalist: TAsmList; data: pchar; len: asizeint; encoding: tstringencoding): tasmlabofs;
  1507. var
  1508. s: PChar;
  1509. startlab: tasmlabel;
  1510. ansistrrecdef: trecorddef;
  1511. datadef: tdef;
  1512. datatcb: ttai_typedconstbuilder;
  1513. ts : Tai_string;
  1514. begin
  1515. start_internal_data_builder(datalist,sec_rodata_norel,'',datatcb,startlab);
  1516. result:=datatcb.emit_string_const_common(st_ansistring,len,encoding,startlab);
  1517. { terminating zero included }
  1518. datadef:=carraydef.getreusable(cansichartype,len+1);
  1519. datatcb.maybe_begin_aggregate(datadef);
  1520. ts:=tai_string.create_pchar(data,len+1); // +1 to include terminating 0
  1521. datatcb.emit_tai(ts,datadef);
  1522. datatcb.maybe_end_aggregate(datadef);
  1523. ansistrrecdef:=datatcb.end_anonymous_record;
  1524. finish_internal_data_builder(datatcb,startlab,ansistrrecdef,const_align(voidpointertype.alignment));
  1525. end;
  1526. function ttai_typedconstbuilder.emit_unicodestring_const(datalist: TAsmList; data: tcompilerwidestring; encoding: tstringencoding; winlike: boolean):tasmlabofs;
  1527. var
  1528. i, strlength: longint;
  1529. string_symofs: asizeint;
  1530. startlab: tasmlabel;
  1531. datadef: tdef;
  1532. datatcb: ttai_typedconstbuilder;
  1533. unicodestrrecdef: trecorddef;
  1534. begin
  1535. start_internal_data_builder(datalist,sec_rodata_norel,'',datatcb,startlab);
  1536. strlength:=getlengthwidestring(data);
  1537. if winlike then
  1538. begin
  1539. result.lab:=startlab;
  1540. datatcb.begin_anonymous_record('$'+get_dynstring_rec_name(st_widestring,true,strlength),
  1541. 4,4,
  1542. targetinfos[target_info.system]^.alignment.recordalignmin);
  1543. datatcb.emit_tai(Tai_const.Create_32bit(strlength*cwidechartype.size),s32inttype);
  1544. { can we optimise by placing the string constant label at the
  1545. required offset? }
  1546. string_symofs:=get_string_symofs(st_widestring,true);
  1547. if string_symofs=0 then
  1548. begin
  1549. { yes }
  1550. datatcb.emit_tai(Tai_label.Create(result.lab),widecharpointertype);
  1551. { allocate a separate label for the start of the data (see
  1552. emit_string_const_common() for explanation) }
  1553. startlab:=tasmlabel.create(current_asmdata.AsmSymbolDict,startlab.name+'$strlab',startlab.bind,startlab.typ);
  1554. end
  1555. else
  1556. internalerror(2015031502);
  1557. result.ofs:=string_symofs;
  1558. end
  1559. else
  1560. begin
  1561. result:=datatcb.emit_string_const_common(st_unicodestring,strlength,encoding,startlab);
  1562. end;
  1563. if cwidechartype.size = 2 then
  1564. begin
  1565. datadef:=carraydef.getreusable(cwidechartype,strlength+1);
  1566. datatcb.maybe_begin_aggregate(datadef);
  1567. for i:=0 to strlength-1 do
  1568. datatcb.emit_tai(Tai_const.Create_16bit(data.data[i]),cwidechartype);
  1569. { ending #0 }
  1570. datatcb.emit_tai(Tai_const.Create_16bit(0),cwidechartype);
  1571. datatcb.maybe_end_aggregate(datadef);
  1572. unicodestrrecdef:=datatcb.end_anonymous_record;
  1573. end
  1574. else
  1575. { code generation for other sizes must be written }
  1576. internalerror(200904271);
  1577. finish_internal_data_builder(datatcb,startlab,unicodestrrecdef,const_align(voidpointertype.alignment));
  1578. end;
  1579. procedure ttai_typedconstbuilder.emit_string_offset(const ll: tasmlabofs; const strlength: longint; const st: tstringtype; const winlikewidestring: boolean; const charptrdef: tdef);
  1580. begin
  1581. emit_tai(Tai_const.Create_sym_offset(ll.lab,ll.ofs),get_dynstring_def_for_type(st,winlikewidestring));
  1582. end;
  1583. procedure ttai_typedconstbuilder.emit_dynarray_offset(const ll:tasmlabofs;const arrlength:asizeint;const arrdef:tarraydef; const arrconstdatadef: trecorddef);
  1584. begin
  1585. emit_tai(tai_const.create_sym_offset(ll.lab,ll.ofs),arrdef);
  1586. end;
  1587. function ttai_typedconstbuilder.begin_dynarray_const(arrdef:tdef;var startlab:tasmlabel;out arrlengthloc:ttypedconstplaceholder):tasmlabofs;
  1588. var
  1589. dynarray_symofs: asizeint;
  1590. begin
  1591. result.lab:=startlab;
  1592. result.ofs:=0;
  1593. { pack the data, so that we don't add unnecessary null bytes after the
  1594. constant string }
  1595. begin_anonymous_record('',1,sizeof(TConstPtrUInt),1);
  1596. dynarray_symofs:=get_dynarray_symofs;
  1597. { what to do if ptrsinttype <> sizesinttype??? }
  1598. emit_tai(tai_const.create_sizeint(-1),ptrsinttype);
  1599. inc(result.ofs,ptrsinttype.size);
  1600. arrlengthloc:=emit_placeholder(sizesinttype);
  1601. inc(result.ofs,sizesinttype.size);
  1602. if dynarray_symofs=0 then
  1603. begin
  1604. { results in slightly more efficient code }
  1605. emit_tai(tai_label.create(result.lab),arrdef);
  1606. result.ofs:=0;
  1607. { create new label of the same kind (including whether or not the
  1608. name starts with target_asm.labelprefix in case it's AB_LOCAL,
  1609. so we keep the difference depending on whether the original was
  1610. allocated via getstatic/getlocal/getglobal datalabel) }
  1611. startlab:=tasmlabel.create(current_asmdata.AsmSymbolDict,startlab.name+'$dynarrlab',startlab.bind,startlab.typ);
  1612. end;
  1613. { sanity check }
  1614. if result.ofs<>dynarray_symofs then
  1615. internalerror(2018020601);
  1616. end;
  1617. function ttai_typedconstbuilder.end_dynarray_const(arrdef:tdef;arrlength:asizeint;arrlengthloc:ttypedconstplaceholder;llofs:tasmlabofs):tdef;
  1618. begin
  1619. { we emit the high value, not the count }
  1620. arrlengthloc.replace(tai_const.Create_sizeint(arrlength-1),sizesinttype);
  1621. arrlengthloc.free;
  1622. if get_dynarray_symofs=0 then
  1623. emit_tai(tai_symbol_end.create(llofs.lab),arrdef);
  1624. result:=end_anonymous_record;
  1625. end;
  1626. function ttai_typedconstbuilder.emit_shortstring_const(const str: shortstring): tdef;
  1627. begin
  1628. { we use an arraydef instead of a shortstringdef, because we don't have
  1629. functionality in place yet to reuse shortstringdefs of the same length
  1630. and neither the lowlevel nor the llvm typedconst builder cares about
  1631. this difference }
  1632. result:=carraydef.getreusable(cansichartype,length(str)+1);
  1633. maybe_begin_aggregate(result);
  1634. emit_tai(Tai_const.Create_8bit(length(str)),u8inttype);
  1635. if str<>'' then
  1636. emit_tai(Tai_string.Create(str),carraydef.getreusable(cansichartype,length(str)));
  1637. maybe_end_aggregate(result);
  1638. end;
  1639. function ttai_typedconstbuilder.emit_pchar_const(str: pchar; len: pint; copypchar: boolean): tdef;
  1640. var
  1641. newstr: pchar;
  1642. begin
  1643. result:=carraydef.getreusable(cansichartype,len+1);
  1644. maybe_begin_aggregate(result);
  1645. if (len=0) and
  1646. (not assigned(str) or
  1647. copypchar) then
  1648. emit_tai(Tai_const.Create_8bit(0),cansichartype)
  1649. else
  1650. begin
  1651. if copypchar then
  1652. begin
  1653. getmem(newstr,len+1);
  1654. move(str^,newstr^,len+1);
  1655. str:=newstr;
  1656. end;
  1657. emit_tai(Tai_string.Create_pchar(str,len+1),result);
  1658. end;
  1659. maybe_end_aggregate(result);
  1660. end;
  1661. procedure ttai_typedconstbuilder.emit_guid_const(const guid: tguid);
  1662. var
  1663. i: longint;
  1664. field: tfieldvarsym;
  1665. begin
  1666. maybe_begin_aggregate(rec_tguid);
  1667. { variant record -> must specify which fields get initialised }
  1668. next_field:=tfieldvarsym(rec_tguid.symtable.Find('DATA1'));
  1669. emit_tai(Tai_const.Create_32bit(longint(guid.D1)),u32inttype);
  1670. next_field:=tfieldvarsym(rec_tguid.symtable.Find('DATA2'));
  1671. emit_tai(Tai_const.Create_16bit(guid.D2),u16inttype);
  1672. next_field:=tfieldvarsym(rec_tguid.symtable.Find('DATA3'));
  1673. emit_tai(Tai_const.Create_16bit(guid.D3),u16inttype);
  1674. field:=tfieldvarsym(rec_tguid.symtable.Find('DATA4'));
  1675. next_field:=field;
  1676. { the array }
  1677. maybe_begin_aggregate(field.vardef);
  1678. for i:=Low(guid.D4) to High(guid.D4) do
  1679. emit_tai(Tai_const.Create_8bit(guid.D4[i]),u8inttype);
  1680. maybe_end_aggregate(field.vardef);
  1681. maybe_end_aggregate(rec_tguid);
  1682. end;
  1683. procedure ttai_typedconstbuilder.emit_procdef_const(pd: tprocdef);
  1684. begin
  1685. emit_tai(Tai_const.Createname(pd.mangledname,AT_FUNCTION,0),cprocvardef.getreusableprocaddr(pd,pc_address_only));
  1686. end;
  1687. procedure ttai_typedconstbuilder.emit_ord_const(value: int64; def: tdef);
  1688. begin
  1689. case def.size of
  1690. 1:
  1691. emit_tai(Tai_const.Create_8bit(byte(value)),def);
  1692. 2:
  1693. emit_tai(Tai_const.Create_16bit(word(value)),def);
  1694. 4:
  1695. emit_tai(Tai_const.Create_32bit(longint(value)),def);
  1696. 8:
  1697. emit_tai(Tai_const.Create_64bit(value),def);
  1698. else
  1699. internalerror(2014100501);
  1700. end;
  1701. end;
  1702. procedure ttai_typedconstbuilder.emit_pooled_shortstring_const_ref(const str:shortstring);
  1703. var
  1704. pool : thashset;
  1705. entry : phashsetitem;
  1706. strlab : tasmlabel;
  1707. l : longint;
  1708. pc : pansichar;
  1709. datadef : tdef;
  1710. strtcb : ttai_typedconstbuilder;
  1711. begin
  1712. pool:=current_asmdata.ConstPools[sp_shortstr];
  1713. entry:=pool.FindOrAdd(@str[1],length(str));
  1714. { :-(, we must generate a new entry }
  1715. if not assigned(entry^.Data) then
  1716. begin
  1717. current_asmdata.getglobaldatalabel(strlab);
  1718. { include length and terminating zero for quick conversion to pchar }
  1719. l:=length(str);
  1720. getmem(pc,l+2);
  1721. move(str[1],pc[1],l);
  1722. pc[0]:=chr(l);
  1723. pc[l+1]:=#0;
  1724. datadef:=carraydef.getreusable(cansichartype,l+2);
  1725. { we start a new constbuilder as we don't know whether we're called
  1726. from inside an internal constbuilder }
  1727. strtcb:=ctai_typedconstbuilder.create([tcalo_is_lab,tcalo_make_dead_strippable,tcalo_apply_constalign]);
  1728. strtcb.maybe_begin_aggregate(datadef);
  1729. strtcb.emit_tai(Tai_string.Create_pchar(pc,l+2),datadef);
  1730. strtcb.maybe_end_aggregate(datadef);
  1731. current_asmdata.asmlists[al_typedconsts].concatList(
  1732. strtcb.get_final_asmlist(strlab,datadef,sec_rodata_norel,strlab.name,const_align(sizeof(pint)))
  1733. );
  1734. strtcb.free;
  1735. entry^.Data:=strlab;
  1736. { Make sure strlab has a reference }
  1737. strlab.increfs;
  1738. end
  1739. else
  1740. strlab:=tasmlabel(entry^.Data);
  1741. emit_tai(tai_const.Create_sym(strlab),charpointertype);
  1742. end;
  1743. procedure ttai_typedconstbuilder.maybe_begin_aggregate(def: tdef);
  1744. begin
  1745. begin_aggregate_internal(def,false);
  1746. end;
  1747. procedure ttai_typedconstbuilder.maybe_end_aggregate(def: tdef);
  1748. begin
  1749. end_aggregate_internal(def,false);
  1750. end;
  1751. function ttai_typedconstbuilder.begin_anonymous_record(const optionalname: string; packrecords, recordalign, recordalignmin: shortint): trecorddef;
  1752. var
  1753. anonrecorddef: trecorddef;
  1754. typesym: ttypesym;
  1755. begin
  1756. { if the name is specified, we create a typesym with that name in order
  1757. to ensure we can find it again later with that name -> reuse here as
  1758. well if possible (and that also avoids duplicate type name issues) }
  1759. if optionalname<>'' then
  1760. begin
  1761. typesym:=try_search_current_module_type(optionalname);
  1762. if assigned(typesym) then
  1763. begin
  1764. if typesym.typedef.typ<>recorddef then
  1765. internalerror(2015071401);
  1766. result:=trecorddef(typesym.typedef);
  1767. maybe_begin_aggregate(result);
  1768. exit;
  1769. end;
  1770. end;
  1771. { create skeleton def }
  1772. anonrecorddef:=crecorddef.create_global_internal(optionalname,packrecords,recordalignmin);
  1773. trecordsymtable(anonrecorddef.symtable).recordalignment:=recordalign;
  1774. { generic aggregate housekeeping }
  1775. begin_aggregate_internal(anonrecorddef,true);
  1776. { mark as anonymous record }
  1777. curagginfo.anonrecord:=true;
  1778. { in case a descendent wants to do something with the anonrecorddef too }
  1779. result:=anonrecorddef;
  1780. end;
  1781. function ttai_typedconstbuilder.end_anonymous_record: trecorddef;
  1782. var
  1783. info: taggregateinformation;
  1784. anonrecord: boolean;
  1785. begin
  1786. info:=curagginfo;
  1787. if not assigned(info) or
  1788. (info.def.typ<>recorddef) then
  1789. internalerror(2014080201);
  1790. result:=trecorddef(info.def);
  1791. { make a copy, as we need it after info has been freed by
  1792. maybe_end_aggregate(result) }
  1793. anonrecord:=info.anonrecord;
  1794. { finalise the record skeleton (all fields have been added already by
  1795. emit_tai()) -- anonrecord may not be set in case we reused an earlier
  1796. constructed def }
  1797. if anonrecord then
  1798. trecordsymtable(result.symtable).addalignmentpadding;
  1799. end_aggregate_internal(result,true);
  1800. if anonrecord and
  1801. assigned(curagginfo) and
  1802. (curagginfo.def.typ=recorddef) then
  1803. insert_marked_aggregate_alignment(result);
  1804. end;
  1805. procedure ttai_typedconstbuilder.emit_comment(const comment: string);
  1806. var
  1807. comm: tai_comment;
  1808. begin
  1809. if (length(comment)=0) then
  1810. exit;
  1811. comm:=tai_comment.Create(strpnew(comment));
  1812. fasmlist.concat(comm);
  1813. end;
  1814. procedure ttai_typedconstbuilder.check_add_placeholder(def: tdef);
  1815. begin
  1816. { it only makes sense to add a placeholder inside an aggregate
  1817. (otherwise there can be but one element)
  1818. we cannot add a placeholder in the middle of a queued expression
  1819. either
  1820. the placeholder cannot be an aggregate }
  1821. if not assigned(curagginfo) or
  1822. queue_is_active or
  1823. (aggregate_kind(def)<>tck_simple) then
  1824. internalerror(2015091001);
  1825. end;
  1826. procedure ttai_typedconstbuilder.queue_init(todef: tdef);
  1827. var
  1828. info: taggregateinformation;
  1829. begin
  1830. { nested call to init? }
  1831. if fqueue_offset<>low(fqueue_offset) then
  1832. internalerror(2014062101);
  1833. { insert padding bytes before starting the queue, so that the first
  1834. padding byte won't be interpreted as the emitted value for this queue }
  1835. info:=curagginfo;
  1836. if assigned(info) then
  1837. begin
  1838. if ((info.def.typ=recorddef) or
  1839. is_object(info.def)) and
  1840. { may add support for these later }
  1841. not is_packed_record_or_object(info.def) then
  1842. pad_next_field(todef);
  1843. end;
  1844. fqueue_offset:=0;
  1845. fqueued_def:=todef;
  1846. end;
  1847. procedure ttai_typedconstbuilder.queue_vecn(def: tdef; const index: tconstexprint);
  1848. var
  1849. elelen,
  1850. vecbase: asizeint;
  1851. v: tconstexprint;
  1852. begin
  1853. elelen:=1;
  1854. vecbase:=0;
  1855. case def.typ of
  1856. stringdef :
  1857. ;
  1858. arraydef :
  1859. begin
  1860. if not is_packed_array(def) then
  1861. begin
  1862. elelen:=tarraydef(def).elesize;
  1863. vecbase:=tarraydef(def).lowrange;
  1864. end
  1865. else
  1866. Message(parser_e_packed_dynamic_open_array);
  1867. end;
  1868. else
  1869. Message(parser_e_illegal_expression);
  1870. end;
  1871. { Prevent overflow }
  1872. v:=index-vecbase;
  1873. if (v<int64(low(fqueue_offset))) or (v>int64(high(fqueue_offset))) then
  1874. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(fqueue_offset)),tostr(high(fqueue_offset)));
  1875. if high(fqueue_offset)-fqueue_offset div elelen>v then
  1876. inc(fqueue_offset,elelen*v.svalue)
  1877. else
  1878. message3(type_e_range_check_error_bounds,tostr(index),tostr(vecbase),tostr(high(fqueue_offset)-fqueue_offset div elelen+vecbase))
  1879. end;
  1880. procedure ttai_typedconstbuilder.queue_pointeraddn(def: tpointerdef; const index: tconstexprint);
  1881. begin
  1882. inc(fqueue_offset,def.pointeddef.size*int64(index));
  1883. end;
  1884. procedure ttai_typedconstbuilder.queue_pointersubn(def: tpointerdef; const index: tconstexprint);
  1885. begin
  1886. dec(fqueue_offset,def.pointeddef.size*int64(index));
  1887. end;
  1888. procedure ttai_typedconstbuilder.queue_subscriptn(def: tabstractrecorddef; vs: tfieldvarsym);
  1889. begin
  1890. inc(fqueue_offset,vs.fieldoffset);
  1891. end;
  1892. function ttai_typedconstbuilder.queue_subscriptn_multiple_by_name(def: tabstractrecorddef; const fields: array of TIDString): tdef;
  1893. var
  1894. syms,
  1895. parentdefs: tfplist;
  1896. sym: tsym;
  1897. curdef: tdef;
  1898. i: longint;
  1899. begin
  1900. result:=nil;
  1901. if length(fields)=0 then
  1902. internalerror(2015071601);
  1903. syms:=tfplist.Create;
  1904. syms.count:=length(fields);
  1905. parentdefs:=tfplist.create;
  1906. parentdefs.Count:=length(fields);
  1907. curdef:=def;
  1908. for i:=low(fields) to high(fields) do
  1909. begin
  1910. sym:=search_struct_member_no_helper(tabstractrecorddef(curdef),fields[i]);
  1911. if not assigned(sym) or
  1912. not is_normal_fieldvarsym(sym) or
  1913. ((i<>high(fields)) and
  1914. not(tfieldvarsym(sym).vardef.typ in [objectdef,recorddef])) then
  1915. internalerror(2015071505);
  1916. syms[i]:=sym;
  1917. parentdefs[i]:=curdef;
  1918. curdef:=tfieldvarsym(sym).vardef;
  1919. result:=curdef;
  1920. end;
  1921. for i:=high(fields) downto low(fields) do
  1922. queue_subscriptn(tabstractrecorddef(parentdefs[i]),tfieldvarsym(syms[i]));
  1923. syms.free;
  1924. parentdefs.free;
  1925. end;
  1926. procedure ttai_typedconstbuilder.queue_typeconvn(fromdef, todef: tdef);
  1927. begin
  1928. { do nothing }
  1929. end;
  1930. procedure ttai_typedconstbuilder.queue_emit_proc(pd: tprocdef);
  1931. begin
  1932. if fqueue_offset<>0 then
  1933. internalerror(2014092101);
  1934. emit_procdef_const(pd);
  1935. fqueue_offset:=low(fqueue_offset);
  1936. end;
  1937. procedure ttai_typedconstbuilder.queue_emit_staticvar(vs: tstaticvarsym);
  1938. begin
  1939. { pointerdef because we are emitting a pointer to the staticvarsym
  1940. data, not the data itself }
  1941. emit_tai(Tai_const.Createname(vs.mangledname,AT_DATA,fqueue_offset),cpointerdef.getreusable(vs.vardef));
  1942. fqueue_offset:=low(fqueue_offset);
  1943. end;
  1944. procedure ttai_typedconstbuilder.queue_emit_label(l: tlabelsym);
  1945. begin
  1946. emit_tai(Tai_const.Createname(l.mangledname,fqueue_offset),voidcodepointertype);
  1947. fqueue_offset:=low(fqueue_offset);
  1948. end;
  1949. procedure ttai_typedconstbuilder.queue_emit_const(cs: tconstsym);
  1950. var
  1951. resourcestrrec: trecorddef;
  1952. begin
  1953. if not (cs.consttyp in [constresourcestring,constwresourcestring]) then
  1954. internalerror(2014062102);
  1955. if fqueue_offset<>0 then
  1956. internalerror(2014062103);
  1957. { warning: update if/when the type of resource strings changes }
  1958. case cs.consttyp of
  1959. constwresourcestring,
  1960. constresourcestring:
  1961. begin
  1962. resourcestrrec:=trecorddef(search_system_type('TRESOURCESTRINGRECORD').typedef);
  1963. queue_subscriptn_multiple_by_name(resourcestrrec,['CURRENTVALUE']);
  1964. queue_emit_asmsym(current_asmdata.RefAsmSymbol(
  1965. make_mangledname('RESSTR',cs.owner,cs.name),AT_DATA),resourcestrrec
  1966. );
  1967. end;
  1968. { can these occur? }
  1969. constord,
  1970. conststring,constreal,
  1971. constset,constpointer,constnil,
  1972. constwstring,constguid:
  1973. internalerror(2015090903);
  1974. else
  1975. internalerror(2015090904);
  1976. end;
  1977. fqueue_offset:=low(fqueue_offset);
  1978. end;
  1979. procedure ttai_typedconstbuilder.queue_emit_asmsym(sym: tasmsymbol; def: tdef);
  1980. begin
  1981. { pointerdef, because "sym" represents the address of whatever the
  1982. data is }
  1983. def:=cpointerdef.getreusable(def);
  1984. emit_tai(Tai_const.Create_sym_offset(sym,fqueue_offset),def);
  1985. fqueue_offset:=low(fqueue_offset);
  1986. end;
  1987. procedure ttai_typedconstbuilder.queue_emit_ordconst(value: int64; def: tdef);
  1988. begin
  1989. emit_ord_const(value,def);
  1990. fqueue_offset:=low(fqueue_offset);
  1991. end;
  1992. function ttai_typedconstbuilder.queue_is_active: boolean;
  1993. begin
  1994. result:=fqueue_offset<>low(fqueue_offset)
  1995. end;
  1996. {****************************************************************************
  1997. tlowleveltypedconstplaceholder
  1998. ****************************************************************************}
  1999. constructor tlowleveltypedconstplaceholder.create(l: tasmlist; pos: tai; d: tdef);
  2000. begin
  2001. inherited create(d);
  2002. list:=l;
  2003. insertpos:=pos;
  2004. end;
  2005. procedure tlowleveltypedconstplaceholder.replace(ai: tai; d: tdef);
  2006. begin
  2007. if d<>def then
  2008. internalerror(2015091007);
  2009. list.insertafter(ai,insertpos);
  2010. list.remove(insertpos);
  2011. insertpos.free;
  2012. end;
  2013. {****************************************************************************
  2014. tai_abstracttypedconst
  2015. ****************************************************************************}
  2016. class constructor ttai_lowleveltypedconstbuilder.classcreate;
  2017. begin
  2018. caggregateinformation:=tlowlevelaggregateinformation;
  2019. end;
  2020. function ttai_lowleveltypedconstbuilder.emit_placeholder(def: tdef): ttypedconstplaceholder;
  2021. var
  2022. p: tai;
  2023. begin
  2024. check_add_placeholder(def);
  2025. p:=tai_marker.Create(mark_position);
  2026. emit_tai(p,def);
  2027. result:=tlowleveltypedconstplaceholder.create(fasmlist,p,def);
  2028. end;
  2029. procedure ttai_lowleveltypedconstbuilder.mark_anon_aggregate_alignment;
  2030. var
  2031. marker: tai_marker;
  2032. begin
  2033. marker:=tai_marker.Create(mark_position);
  2034. fasmlist.concat(marker);
  2035. tlowlevelaggregateinformation(curagginfo).anonrecmarker:=marker;
  2036. end;
  2037. procedure ttai_lowleveltypedconstbuilder.insert_marked_aggregate_alignment(def: tdef);
  2038. var
  2039. info: tlowlevelaggregateinformation;
  2040. fillbytes: asizeint;
  2041. begin
  2042. info:=tlowlevelaggregateinformation(curagginfo);
  2043. if not assigned(info.anonrecmarker) then
  2044. internalerror(2014091401);
  2045. fillbytes:=info.prepare_next_field(def);
  2046. while fillbytes>0 do
  2047. begin
  2048. fasmlist.insertafter(tai_const.create_8bit(0),info.anonrecmarker);
  2049. dec(fillbytes);
  2050. end;
  2051. fasmlist.remove(info.anonrecmarker);
  2052. info.anonrecmarker.free;
  2053. info.anonrecmarker:=nil;
  2054. end;
  2055. procedure ttai_lowleveltypedconstbuilder.finalize_asmlist(asmsym: tasmsymbol; sym: tsym; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions);
  2056. begin
  2057. inherited;
  2058. { The darwin/ppc64 assembler or linker seems to have trouble }
  2059. { if a section ends with a global label without any data after it. }
  2060. { So for safety, just put a dummy value here. }
  2061. { Further, the regular linker also kills this symbol when turning }
  2062. { on smart linking in case no value appears after it, so put the }
  2063. { dummy byte there always }
  2064. { Update: the Mac OS X 10.6 linker orders data that needs to be }
  2065. { relocated before all other data, so make this data relocatable, }
  2066. { otherwise the end label won't be moved with the rest }
  2067. if (tcalo_vectorized_dead_strip_end in options) and
  2068. (target_info.system in (systems_darwin+systems_aix)) then
  2069. fasmlist.concat(Tai_const.create_sym(asmsym));
  2070. end;
  2071. begin
  2072. ctai_typedconstbuilder:=ttai_lowleveltypedconstbuilder;
  2073. end.