aasmcnst.pas 79 KB

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