aasmcnst.pas 73 KB

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