aasmcnst.pas 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  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,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. procedure finish;
  81. destructor destroy; override;
  82. end;
  83. tasmlabofs = record
  84. lab: tasmlabel;
  85. ofs: asizeint;
  86. end;
  87. { flags for the finalisation of the typed const builder asmlist }
  88. ttcasmlistoption = (
  89. { the tasmsymbol is a tasmlabel }
  90. tcalo_is_lab,
  91. { start a new section (e.g., because we don't know the current section
  92. type) }
  93. tcalo_new_section,
  94. { this symbol is the start of a block of data that should be
  95. dead-stripable/smartlinkable; may imply starting a new section, but
  96. not necessarily (depends on what the platform requirements are) }
  97. tcalo_make_dead_strippable
  98. );
  99. ttcasmlistoptions = set of ttcasmlistoption;
  100. { information about aggregates we are parsing }
  101. taggregateinformation = class
  102. private
  103. function getcuroffset: asizeint;
  104. function getfieldoffset(l: longint): asizeint;
  105. protected
  106. { type of the aggregate }
  107. fdef: tdef;
  108. { type of the aggregate }
  109. ftyp: ttypedconstkind;
  110. { symtable entry of the previously emitted field in case of a
  111. record/object (nil if none emitted yet), used to insert alignment bytes
  112. if necessary for variant records and objects }
  113. fcurfield,
  114. { field corresponding to the data that will be emitted next in case of a
  115. record/object (nil if not set), used to handle variant records and
  116. objects }
  117. fnextfield: tfieldvarsym;
  118. { similar as the fcurfield/fnextfield above, but instead of fieldvarsyms
  119. these are indices in the symlist of a recorddef that correspond to
  120. fieldvarsyms. These are used only for non-variant records, simply
  121. traversing the fields in order. We could use the above method here as
  122. well, but to find the next field we'd always have to use
  123. symlist.indexof(fcurfield), which would be quite slow. These have -1 as
  124. value if they're not set }
  125. fcurindex,
  126. fnextindex: longint;
  127. { anonymous record that is being built as we add constant data }
  128. fanonrecord: boolean;
  129. property curindex: longint read fcurindex write fcurindex;
  130. property nextindex: longint read fnextindex write fnextindex;
  131. public
  132. constructor create(_def: tdef; _typ: ttypedconstkind); virtual;
  133. { calculated padding bytes for alignment if needed, and add the def of the
  134. next field in case we are constructing an anonymous record }
  135. function prepare_next_field(nextfielddef: tdef): asizeint;
  136. property def: tdef read fdef;
  137. property typ: ttypedconstkind read ftyp;
  138. property curfield: tfieldvarsym read fcurfield write fcurfield;
  139. property nextfield: tfieldvarsym read fnextfield write fnextfield;
  140. property fieldoffset[l: longint]: asizeint read getfieldoffset;
  141. property curoffset: asizeint read getcuroffset;
  142. property anonrecord: boolean read fanonrecord write fanonrecord;
  143. end;
  144. taggregateinformationclass = class of taggregateinformation;
  145. { Warning: never directly create a ttai_typedconstbuilder instance,
  146. instead create a cai_typedconstbuilder (this class can be overridden) }
  147. ttai_typedconstbuilder = class abstract
  148. { class type to use when creating new aggregate information instances }
  149. protected class var
  150. caggregateinformation: taggregateinformationclass;
  151. private
  152. function getcurragginfo: taggregateinformation;
  153. procedure set_next_field(AValue: tfieldvarsym);
  154. protected
  155. { temporary list in which all data is collected }
  156. fasmlist: tasmlist;
  157. { options for the final asmlist }
  158. foptions: ttcasmlistoptions;
  159. { while queueing elements of a compound expression, this is the current
  160. offset in the top-level array/record }
  161. fqueue_offset: asizeint;
  162. { array of caggregateinformation instances }
  163. faggregateinformation: tfpobjectlist;
  164. { Support for generating data that is only referenced from the typed
  165. constant data that we are currently generated. Such data can all be put
  166. in the same dead-strippable unit, as it's either all included or none of
  167. it is included. This data can be spread over multiple kinds of sections
  168. though (e.g. rodata and rodata_no_rel), so per section keep track whether
  169. we already started a dead-strippable unit and if so, what the section
  170. name was (so that on platforms that perform the dead stripping based on
  171. sections, we put all data for one typed constant into a single section
  172. with the same name) }
  173. protected type
  174. tinternal_data_section_info = record
  175. secname: TSymStr;
  176. sectype: TAsmSectiontype;
  177. end;
  178. protected var
  179. { all internally generated data must be stored in the same list, as it must
  180. be consecutive (if it's spread over multiple lists, we don't know in
  181. which order they'll be concatenated) -> keep track of this list }
  182. finternal_data_asmlist: tasmlist;
  183. { kind of the last section we started in the finternal_data_asmlist, to
  184. avoid creating unnecessary section statements }
  185. finternal_data_current_section: TAsmSectiontype;
  186. { info about in which kinds of sections we have already emitted internal
  187. data, and what their names were }
  188. finternal_data_section_info: array of tinternal_data_section_info;
  189. { ensure that finalize_asmlist is called only once }
  190. fasmlist_finalized: boolean;
  191. { returns whether def must be handled as an aggregate on the current
  192. platform }
  193. function aggregate_kind(def: tdef): ttypedconstkind; virtual;
  194. { finalize the asmlist: add the necessary symbols etc }
  195. procedure finalize_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions); virtual;
  196. { called by the public emit_tai() routines to actually add the typed
  197. constant data; the public ones also take care of adding extra padding
  198. bytes etc (by calling this one) }
  199. procedure do_emit_tai(p: tai; def: tdef); virtual;
  200. { calls prepare_next_field() and adds the padding bytes in the current
  201. location }
  202. procedure pad_next_field(nextfielddef: tdef);
  203. { returns the index in finternal_data_section_info of the info for the
  204. section of type typ. Returns -1 if there is no such info yet }
  205. function get_internal_data_section_index(typ: TAsmSectiontype): longint;
  206. { get a start label for an internal data section (at the start of a
  207. potentially dead-strippable part) }
  208. function get_internal_data_section_start_label: tasmlabel; virtual;
  209. { get a label in the middle of an internal data section (no dead
  210. stripping) }
  211. function get_internal_data_section_internal_label: tasmlabel; virtual;
  212. { easy access to the top level aggregate information instance }
  213. property curagginfo: taggregateinformation read getcurragginfo;
  214. public
  215. constructor create(const options: ttcasmlistoptions); virtual;
  216. destructor destroy; override;
  217. public
  218. { returns a builder for generating data that is only referrenced by the
  219. typed constant date we are currently generating (e.g. string data for a
  220. pchar constant). Also returns the label that will be placed at the start
  221. of that data. list is the tasmlist to which the data will be added.
  222. secname can be empty to use a default }
  223. procedure start_internal_data_builder(list: tasmlist; sectype: TAsmSectiontype; const secname: TSymStr; out tcb: ttai_typedconstbuilder; out l: tasmlabel);
  224. { finish a previously started internal data builder, including
  225. concatenating all generated data to the provided list and freeing the
  226. builder }
  227. procedure finish_internal_data_builder(var tcb: ttai_typedconstbuilder; l: tasmlabel; def: tdef; alignment: longint);
  228. { add a simple constant data element (p) to the typed constant.
  229. def is the type of the added value }
  230. procedure emit_tai(p: tai; def: tdef); virtual;
  231. { same as above, for a special case: when the def is a procvardef and we
  232. want to use it explicitly as a procdef (i.e., not as a record with a
  233. code and data pointer in case of a complex procvardef) }
  234. procedure emit_tai_procvar2procdef(p: tai; pvdef: tprocvardef); virtual;
  235. protected
  236. function emit_string_const_common(stringtype: tstringtype; len: asizeint; encoding: tstringencoding; var startlab: tasmlabel):tasmlabofs;
  237. procedure begin_aggregate_internal(def: tdef; anonymous: boolean); virtual;
  238. procedure end_aggregate_internal(def: tdef; anonymous: boolean); virtual;
  239. { when building an anonymous record, we cannot immediately insert the
  240. alignment before it in case it's nested, since we only know the required
  241. alignment once all fields have been inserted -> mark the location before
  242. the anonymous record, and insert the alignment once it's finished }
  243. procedure mark_anon_aggregate_alignment; virtual; abstract;
  244. procedure insert_marked_aggregate_alignment(def: tdef); virtual; abstract;
  245. public
  246. class function get_dynstring_rec_name(typ: tstringtype; winlike: boolean; len: asizeint): string;
  247. { the datalist parameter specifies where the data for the string constant
  248. will be emitted (via an internal data builder) }
  249. function emit_ansistring_const(datalist: TAsmList; data: pchar; len: asizeint; encoding: tstringencoding): tasmlabofs;
  250. function emit_unicodestring_const(datalist: TAsmList; data: pointer; encoding: tstringencoding; winlike: boolean):tasmlabofs;
  251. { emits a tasmlabofs as returned by emit_*string_const }
  252. procedure emit_string_offset(const ll: tasmlabofs; const strlength: longint; const st: tstringtype; const winlikewidestring: boolean; const charptrdef: tdef);virtual;
  253. { emit a shortstring constant, and return its def }
  254. function emit_shortstring_const(const str: shortstring): tdef;
  255. { emit a guid constant }
  256. procedure emit_guid_const(const guid: tguid);
  257. { emit a procdef constant }
  258. procedure emit_procdef_const(pd: tprocdef);
  259. { emit an ordinal constant }
  260. procedure emit_ord_const(value: int64; def: tdef);
  261. { begin a potential aggregate type. Must be called for any type
  262. that consists of multiple tai constant data entries, or that
  263. represents an aggregate at the Pascal level (a record, a non-dynamic
  264. array, ... }
  265. procedure maybe_begin_aggregate(def: tdef);
  266. { end a potential aggregate type. Must be paired with every
  267. maybe_begin_aggregate }
  268. procedure maybe_end_aggregate(def: tdef);
  269. { similar as above, but in case
  270. a) it's definitely a record
  271. b) the def of the record should be automatically constructed based on
  272. the types of the emitted fields
  273. }
  274. function begin_anonymous_record(const optionalname: string; packrecords, recordalignmin, maxcrecordalign: shortint): trecorddef; virtual;
  275. function end_anonymous_record: trecorddef; virtual;
  276. { The next group of routines are for constructing complex expressions.
  277. While parsing a typed constant these operators are encountered from
  278. outer to inner, so that is also the order in which they should be
  279. added to the queue. Only one queue can be active at a time. }
  280. { Init the queue. Gives an internalerror if a queue was already active }
  281. procedure queue_init(todef: tdef); virtual;
  282. { queue an array/string indexing operation (performs all range checking,
  283. so it doesn't have to be duplicated in all descendents). }
  284. procedure queue_vecn(def: tdef; const index: tconstexprint); virtual;
  285. { queue a subscripting operation }
  286. procedure queue_subscriptn(def: tabstractrecorddef; vs: tfieldvarsym); virtual;
  287. { queue a type conversion operation }
  288. procedure queue_typeconvn(fromdef, todef: tdef); virtual;
  289. { queue an address taking operation }
  290. procedure queue_addrn(fromdef, todef: tdef); virtual;
  291. { finalise the queue (so a new one can be created) and flush the
  292. previously queued operations, applying them in reverse order on a...}
  293. { ... procdef }
  294. procedure queue_emit_proc(pd: tprocdef); virtual;
  295. { ... staticvarsym }
  296. procedure queue_emit_staticvar(vs: tstaticvarsym); virtual;
  297. { ... labelsym }
  298. procedure queue_emit_label(l: tlabelsym); virtual;
  299. { ... constsym }
  300. procedure queue_emit_const(cs: tconstsym); virtual;
  301. { ... asmsym/asmlabel }
  302. procedure queue_emit_asmsym(sym: tasmsymbol; def: tdef); virtual;
  303. { ... an ordinal constant }
  304. procedure queue_emit_ordconst(value: int64; def: tdef); virtual;
  305. protected
  306. { returns whether queue_init has been called without a corresponding
  307. queue_emit_* to finish it }
  308. function queue_is_active: boolean;
  309. public
  310. { finalize the internal asmlist (if necessary) and return it.
  311. This asmlist will be freed when the builder is destroyed, so add its
  312. contents to another list first. This property should only be accessed
  313. once all data has been added. }
  314. function get_final_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: longint): tasmlist;
  315. { returns the offset of the string data relative to ansi/unicode/widestring
  316. constant labels. On most platforms, this is 0 (with the header at a
  317. negative offset), but on some platforms such negative offsets are not
  318. supported this is equal to the header size }
  319. class function get_string_symofs(typ: tstringtype; winlikewidestring: boolean): pint; virtual;
  320. { set the fieldvarsym whose data we will emit next; needed
  321. in case of variant records, so we know which part of the variant gets
  322. initialised. Also in case of objects, because the fieldvarsyms are spread
  323. over the symtables of the entire inheritance tree }
  324. property next_field: tfieldvarsym write set_next_field;
  325. protected
  326. { this one always return the actual offset, called by the above (and
  327. overridden versions) }
  328. class function get_string_header_size(typ: tstringtype; winlikewidestring: boolean): pint;
  329. end;
  330. ttai_typedconstbuilderclass = class of ttai_typedconstbuilder;
  331. tlowlevelaggregateinformation = class(taggregateinformation)
  332. protected
  333. fanonrecmarker: tai;
  334. public
  335. property anonrecmarker: tai read fanonrecmarker write fanonrecmarker;
  336. end;
  337. ttai_lowleveltypedconstbuilder = class(ttai_typedconstbuilder)
  338. protected
  339. procedure mark_anon_aggregate_alignment; override;
  340. procedure insert_marked_aggregate_alignment(def: tdef); override;
  341. public
  342. { set the default value for caggregateinformation (= tlowlevelaggregateinformation) }
  343. class constructor classcreate;
  344. end;
  345. var
  346. ctai_typedconstbuilder: ttai_typedconstbuilderclass;
  347. implementation
  348. uses
  349. verbose,globals,systems,widestr,
  350. fmodule,
  351. symbase,symtable,defutil;
  352. {****************************************************************************
  353. taggregateinformation
  354. ****************************************************************************}
  355. function taggregateinformation.getcuroffset: asizeint;
  356. var
  357. field: tfieldvarsym;
  358. begin
  359. if assigned(curfield) then
  360. result:=curfield.fieldoffset+curfield.vardef.size
  361. else if curindex<>-1 then
  362. begin
  363. field:=tfieldvarsym(tabstractrecorddef(def).symtable.symlist[curindex]);
  364. result:=field.fieldoffset+field.vardef.size
  365. end
  366. else
  367. result:=0
  368. end;
  369. function taggregateinformation.getfieldoffset(l: longint): asizeint;
  370. var
  371. field: tfieldvarsym;
  372. begin
  373. field:=tfieldvarsym(tabstractrecorddef(def).symtable.symlist[l]);
  374. result:=field.fieldoffset;
  375. end;
  376. constructor taggregateinformation.create(_def: tdef; _typ: ttypedconstkind);
  377. begin
  378. fdef:=_def;
  379. ftyp:=_typ;
  380. fcurindex:=-1;
  381. fnextindex:=-1;
  382. end;
  383. function taggregateinformation.prepare_next_field(nextfielddef: tdef): asizeint;
  384. var
  385. currentoffset,nextoffset: asizeint;
  386. i: longint;
  387. begin
  388. { get the next field and its offset, and make that next field the current
  389. one }
  390. if assigned(nextfield) then
  391. begin
  392. nextoffset:=nextfield.fieldoffset;
  393. currentoffset:=curoffset;
  394. curfield:=nextfield;
  395. end
  396. else
  397. begin
  398. { must set nextfield for unions and objects, as we cannot
  399. automatically detect the "next" field in that case }
  400. if ((def.typ=recorddef) and
  401. trecorddef(def).isunion) or
  402. is_object(def) then
  403. internalerror(2014091202);
  404. { if we are constructing this record as data gets emitted, add a field
  405. for this data }
  406. if anonrecord then
  407. trecorddef(def).add_field_by_def(nextfielddef);
  408. { find next field }
  409. i:=curindex;
  410. repeat
  411. inc(i);
  412. until (tsym(tabstractrecorddef(def).symtable.symlist[i]).typ=fieldvarsym) and
  413. not(sp_static in tsym(tabstractrecorddef(def).symtable.symlist[i]).symoptions);
  414. nextoffset:=fieldoffset[i];
  415. currentoffset:=curoffset;
  416. curindex:=i;
  417. end;
  418. { need padding? }
  419. result:=nextoffset-currentoffset;
  420. end;
  421. {****************************************************************************
  422. tai_abstracttypedconst
  423. ****************************************************************************}
  424. procedure tai_abstracttypedconst.setdef(def: tdef);
  425. begin
  426. { should not be changed, rewrite the calling code if this happens }
  427. if assigned(fdef) then
  428. Internalerror(2014080203);
  429. fdef:=def;
  430. end;
  431. constructor tai_abstracttypedconst.create(_adetyp: ttypedconstkind; _def: tdef);
  432. begin
  433. inherited create;
  434. typ:=ait_typedconst;
  435. fadetyp:=_adetyp;
  436. fdef:=_def;
  437. end;
  438. {****************************************************************************
  439. tai_simpletypedconst
  440. ****************************************************************************}
  441. procedure tai_simpletypedconst.setval(AValue: tai);
  442. begin
  443. fval:=AValue;
  444. end;
  445. constructor tai_simpletypedconst.create(_adetyp: ttypedconstkind; _def: tdef; _val: tai);
  446. begin
  447. inherited create(_adetyp,_def);
  448. fval:=_val;
  449. end;
  450. {****************************************************************************
  451. tai_aggregatetypedconst.tadeenumerator
  452. ****************************************************************************}
  453. constructor tai_aggregatetypedconst.tadeenumerator.create(data: tai_aggregatetypedconst);
  454. begin
  455. fvalues:=data.fvalues;
  456. fvaluespos:=-1;
  457. end;
  458. function tai_aggregatetypedconst.tadeenumerator.getcurrent: tai_abstracttypedconst;
  459. begin
  460. result:=tai_abstracttypedconst(fvalues[fvaluespos]);
  461. end;
  462. function tai_aggregatetypedconst.tadeenumerator.movenext: boolean;
  463. begin
  464. if fvaluespos<pred(fvalues.count) then
  465. begin
  466. inc(fvaluespos);
  467. result:=true
  468. end
  469. else
  470. result:=false;
  471. end;
  472. procedure tai_aggregatetypedconst.tadeenumerator.reset;
  473. begin
  474. fvaluespos:=0
  475. end;
  476. {****************************************************************************
  477. tai_aggregatetypedconst
  478. ****************************************************************************}
  479. procedure tai_aggregatetypedconst.convert_to_string;
  480. var
  481. ai: tai_abstracttypedconst;
  482. newstr: tai_string;
  483. begin
  484. newstr:=tai_string.Create('');
  485. for ai in self do
  486. begin
  487. if ai.adetyp<>tck_simple then
  488. internalerror(2014070103);
  489. add_to_string(newstr,tai_simpletypedconst(ai).val);
  490. ai.free;
  491. end;
  492. fvalues.count:=0;
  493. { the "nil" def will be replaced with an array def of the appropriate
  494. size once we're finished adding data, so we don't create intermediate
  495. arraydefs all the time }
  496. fvalues.add(tai_simpletypedconst.create(tck_simple,nil,newstr));
  497. end;
  498. procedure tai_aggregatetypedconst.add_to_string(strtai: tai_string; othertai: tai);
  499. begin
  500. case othertai.typ of
  501. ait_string:
  502. begin
  503. strtai.str:=reallocmem(strtai.str,strtai.len+tai_string(othertai).len+1);
  504. { also copy null terminator }
  505. move(tai_string(othertai).str[0],strtai.str[strtai.len],tai_string(othertai).len+1);
  506. { the null terminator is not part of the string data }
  507. strtai.len:=strtai.len+tai_string(othertai).len;
  508. end;
  509. ait_const:
  510. begin
  511. if tai_const(othertai).size<>1 then
  512. internalerror(2014070101);
  513. strtai.str:=reallocmem(strtai.str,strtai.len+1);
  514. strtai.str[strtai.len]:=ansichar(tai_const(othertai).value);
  515. strtai.str[strtai.len+1]:=#0;
  516. inc(strtai.len);
  517. end;
  518. else
  519. internalerror(2014070102);
  520. end;
  521. end;
  522. constructor tai_aggregatetypedconst.create(_adetyp: ttypedconstkind; _fdef: tdef);
  523. begin
  524. inherited;
  525. fisstring:=false;
  526. fvalues:=tfplist.create;
  527. end;
  528. function tai_aggregatetypedconst.getenumerator: tadeenumerator;
  529. begin
  530. result:=tadeenumerator.create(self);
  531. end;
  532. procedure tai_aggregatetypedconst.addvalue(val: tai_abstracttypedconst);
  533. begin
  534. { merge string constants and ordinal constants added in an array of
  535. char, to unify the length and the string data }
  536. if fisstring or
  537. ((val.adetyp=tck_simple) and
  538. (tai_simpletypedconst(val).val.typ=ait_string)) then
  539. begin
  540. if not fisstring and
  541. (fvalues.count>0) then
  542. convert_to_string;
  543. fisstring:=true;
  544. case fvalues.count of
  545. 0: fvalues.add(val);
  546. 1:
  547. begin
  548. add_to_string(tai_string(tai_simpletypedconst(fvalues[0]).val),tai_simpletypedconst(val).val);
  549. val.free
  550. end
  551. else
  552. internalerror(2014070104);
  553. end;
  554. end
  555. else
  556. fvalues.add(val);
  557. end;
  558. function tai_aggregatetypedconst.valuecount: longint;
  559. begin
  560. result:=fvalues.count;
  561. end;
  562. procedure tai_aggregatetypedconst.insertvaluebeforepos(val: tai_abstracttypedconst; pos: longint);
  563. begin
  564. fvalues.insert(pos,val);
  565. end;
  566. procedure tai_aggregatetypedconst.finish;
  567. begin
  568. if fisstring then
  569. begin
  570. { set the def: an array of char with the same length as the string
  571. data }
  572. if fvalues.count<>1 then
  573. internalerror(2014070105);
  574. tai_simpletypedconst(fvalues[0]).fdef:=
  575. getarraydef(cansichartype,
  576. tai_string(tai_simpletypedconst(fvalues[0]).val).len);
  577. end;
  578. end;
  579. destructor tai_aggregatetypedconst.destroy;
  580. begin
  581. fvalues.free;
  582. inherited destroy;
  583. end;
  584. {*****************************************************************************
  585. ttai_typedconstbuilder
  586. *****************************************************************************}
  587. function ttai_typedconstbuilder.getcurragginfo: taggregateinformation;
  588. begin
  589. if assigned(faggregateinformation) and
  590. (faggregateinformation.count>0) then
  591. result:=taggregateinformation(faggregateinformation[faggregateinformation.count-1])
  592. else
  593. result:=nil;
  594. end;
  595. procedure ttai_typedconstbuilder.set_next_field(AValue: tfieldvarsym);
  596. var
  597. info: taggregateinformation;
  598. begin
  599. info:=curagginfo;
  600. if not assigned(info) then
  601. internalerror(2014091206);
  602. info.nextfield:=AValue;
  603. end;
  604. procedure ttai_typedconstbuilder.pad_next_field(nextfielddef: tdef);
  605. var
  606. fillbytes: asizeint;
  607. begin
  608. fillbytes:=curagginfo.prepare_next_field(nextfielddef);
  609. while fillbytes>0 do
  610. begin
  611. do_emit_tai(tai_const.create_8bit(0),u8inttype);
  612. dec(fillbytes);
  613. end;
  614. end;
  615. function ttai_typedconstbuilder.get_internal_data_section_index(typ: TAsmSectiontype): longint;
  616. begin
  617. { avoid wrong warning by -Oodfa }
  618. result:=-1;
  619. for result:=low(finternal_data_section_info) to high(finternal_data_section_info) do
  620. if finternal_data_section_info[result].sectype=typ then
  621. exit;
  622. result:=-1;
  623. end;
  624. function ttai_typedconstbuilder.get_internal_data_section_start_label: tasmlabel;
  625. begin
  626. { on Darwin, dead code/data stripping happens based on non-temporary
  627. labels (any label that doesn't start with "L" -- it doesn't have
  628. to be global) }
  629. if target_info.system in systems_darwin then
  630. current_asmdata.getstaticdatalabel(result)
  631. else if create_smartlink_library then
  632. current_asmdata.getglobaldatalabel(result)
  633. else
  634. current_asmdata.getlocaldatalabel(result);
  635. end;
  636. function ttai_typedconstbuilder.get_internal_data_section_internal_label: tasmlabel;
  637. begin
  638. if create_smartlink_library then
  639. { all labels need to be global in case they're in another object }
  640. current_asmdata.getglobaldatalabel(result)
  641. else
  642. { no special requirement for the label -> just get a local one }
  643. current_asmdata.getlocaldatalabel(result);
  644. end;
  645. function ttai_typedconstbuilder.aggregate_kind(def: tdef): ttypedconstkind;
  646. begin
  647. if (def.typ in [recorddef,filedef,variantdef]) or
  648. is_object(def) or
  649. ((def.typ=procvardef) and
  650. not tprocvardef(def).is_addressonly) then
  651. result:=tck_record
  652. else if ((def.typ=arraydef) and
  653. not is_dynamic_array(def)) or
  654. ((def.typ=setdef) and
  655. not is_smallset(def)) or
  656. is_shortstring(def) then
  657. result:=tck_array
  658. else
  659. result:=tck_simple;
  660. end;
  661. procedure ttai_typedconstbuilder.finalize_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions);
  662. var
  663. prelist: tasmlist;
  664. begin
  665. prelist:=tasmlist.create;
  666. { only now add items based on the symbolname, because it may be
  667. modified by the "section" specifier in case of a typed constant }
  668. if tcalo_make_dead_strippable in options then
  669. begin
  670. maybe_new_object_file(prelist);
  671. { we always need a new section here, since if we started a new
  672. object file then we have to say what the section is, and otherwise
  673. we need a new section because that's how the dead stripping works
  674. (except on Darwin, but that will be addressed in a future commit) }
  675. new_section(prelist,section,secname,const_align(alignment));
  676. end
  677. else if tcalo_new_section in options then
  678. new_section(prelist,section,secname,const_align(alignment))
  679. else
  680. prelist.concat(cai_align.Create(const_align(alignment)));
  681. if not(tcalo_is_lab in options) then
  682. if sym.bind=AB_GLOBAL then
  683. prelist.concat(tai_symbol.Create_Global(sym,0))
  684. else
  685. prelist.concat(tai_symbol.Create(sym,0))
  686. else
  687. prelist.concat(tai_label.Create(tasmlabel(sym)));
  688. { insert the symbol information before the data }
  689. fasmlist.insertlist(prelist);
  690. { end of the symbol }
  691. fasmlist.concat(tai_symbol_end.Createname(sym.name));
  692. { free the temporary list }
  693. prelist.free;
  694. end;
  695. procedure ttai_typedconstbuilder.do_emit_tai(p: tai; def: tdef);
  696. begin
  697. { by default we don't care about the type }
  698. fasmlist.concat(p);
  699. end;
  700. function ttai_typedconstbuilder.get_final_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: longint): tasmlist;
  701. begin
  702. if not fasmlist_finalized then
  703. begin
  704. finalize_asmlist(sym,def,section,secname,alignment,foptions);
  705. fasmlist_finalized:=true;
  706. end;
  707. result:=fasmlist;
  708. end;
  709. class function ttai_typedconstbuilder.get_string_symofs(typ: tstringtype; winlikewidestring: boolean): pint;
  710. begin
  711. { darwin's linker does not support negative offsets }
  712. if not(target_info.system in systems_darwin) then
  713. result:=0
  714. else
  715. result:=get_string_header_size(typ,winlikewidestring);
  716. end;
  717. class function ttai_typedconstbuilder.get_string_header_size(typ: tstringtype; winlikewidestring: boolean): pint;
  718. const
  719. ansistring_header_size =
  720. { encoding }
  721. 2 +
  722. { elesize }
  723. 2 +
  724. {$ifdef cpu64bitaddr}
  725. { alignment }
  726. 4 +
  727. {$endif cpu64bitaddr}
  728. { reference count }
  729. sizeof(pint) +
  730. { length }
  731. sizeof(pint);
  732. unicodestring_header_size = ansistring_header_size;
  733. begin
  734. case typ of
  735. st_ansistring:
  736. result:=ansistring_header_size;
  737. st_unicodestring:
  738. result:=unicodestring_header_size;
  739. st_widestring:
  740. if winlikewidestring then
  741. result:=0
  742. else
  743. result:=unicodestring_header_size;
  744. else
  745. result:=0;
  746. end;
  747. end;
  748. constructor ttai_typedconstbuilder.create(const options: ttcasmlistoptions);
  749. begin
  750. inherited create;
  751. fasmlist:=tasmlist.create;
  752. foptions:=options;
  753. { queue is empty }
  754. fqueue_offset:=low(fqueue_offset);
  755. finternal_data_current_section:=sec_none;
  756. end;
  757. destructor ttai_typedconstbuilder.destroy;
  758. begin
  759. { the queue should have been flushed if it was used }
  760. if fqueue_offset<>low(fqueue_offset) then
  761. internalerror(2014062901);
  762. faggregateinformation.free;
  763. fasmlist.free;
  764. inherited destroy;
  765. end;
  766. procedure ttai_typedconstbuilder.start_internal_data_builder(list: tasmlist; sectype: TAsmSectiontype; const secname: TSymStr; out tcb: ttai_typedconstbuilder; out l: tasmlabel);
  767. var
  768. options: ttcasmlistoptions;
  769. foundsec: longint;
  770. begin
  771. options:=[tcalo_is_lab];
  772. { Add a section header if the previous one was different. We'll use the
  773. same section name in case multiple items are added to the same kind of
  774. section (rodata, rodata_no_rel, ...), so that everything will still
  775. end up in the same section even if there are multiple section headers }
  776. if finternal_data_current_section<>sectype then
  777. include(options,tcalo_new_section);
  778. finternal_data_current_section:=sectype;
  779. l:=nil;
  780. { did we already create a section of this type for the internal data of
  781. this builder? }
  782. foundsec:=get_internal_data_section_index(sectype);
  783. if foundsec=-1 then
  784. begin
  785. { we only need to start a dead-strippable section of data at the
  786. start of the first subsection of this kind for this block.
  787. exception: if dead stripping happens based on objects/libraries,
  788. then we only have to create a new object file for the first
  789. internal data section of any kind (all the rest will simply be put
  790. in the same object file) }
  791. if create_smartlink then
  792. begin
  793. if not create_smartlink_library or
  794. (length(finternal_data_section_info)=0) then
  795. include(options,tcalo_make_dead_strippable);
  796. { on Darwin, dead code/data stripping happens based on non-
  797. temporary labels (any label that doesn't start with "L" -- it
  798. doesn't have to be global) -> add a non-temporary lobel at the
  799. start of every kind of subsection created in this builder }
  800. if target_info.system in systems_darwin then
  801. l:=get_internal_data_section_start_label;
  802. end;
  803. foundsec:=length(finternal_data_section_info);
  804. setlength(finternal_data_section_info,foundsec+1);
  805. finternal_data_section_info[foundsec].sectype:=sectype;
  806. end;
  807. if not assigned(finternal_data_asmlist) and
  808. (cs_create_smart in current_settings.moduleswitches) then
  809. begin
  810. l:=get_internal_data_section_start_label;
  811. { the internal data list should only be assigned by this routine,
  812. the first time that an internal data block is started }
  813. if not assigned(list) or
  814. assigned(finternal_data_asmlist) then
  815. internalerror(2015032101);
  816. finternal_data_asmlist:=list;
  817. end
  818. { all internal data for this tcb must go to the same list (otherwise all
  819. data we want to add to the dead-strippable block is not guaranteed to
  820. be sequential and e.g. in the same object file in case of library-based
  821. dead stripping) }
  822. else if (assigned(finternal_data_asmlist) and
  823. (list<>finternal_data_asmlist)) or
  824. not assigned(list) then
  825. internalerror(2015032101);
  826. finternal_data_asmlist:=list;
  827. if not assigned(l) then
  828. l:=get_internal_data_section_internal_label;
  829. { first section of this kind -> set name }
  830. if finternal_data_section_info[foundsec].secname='' then
  831. if secname='' then
  832. finternal_data_section_info[foundsec].secname:=l.Name
  833. else
  834. finternal_data_section_info[foundsec].secname:=secname
  835. { if the name is specified multiple times, it must match }
  836. else if (secname<>'') and
  837. (finternal_data_section_info[foundsec].secname<>secname) then
  838. internalerror(2015032401);
  839. tcb:=ttai_typedconstbuilderclass(classtype).create(options);
  840. end;
  841. procedure ttai_typedconstbuilder.finish_internal_data_builder(var tcb: ttai_typedconstbuilder; l: tasmlabel; def: tdef; alignment: longint);
  842. begin
  843. finternal_data_asmlist.concatList(tcb.get_final_asmlist(l,def,
  844. finternal_data_current_section,
  845. finternal_data_section_info[get_internal_data_section_index(finternal_data_current_section)].secname,
  846. alignment));
  847. tcb.free;
  848. tcb:=nil;
  849. end;
  850. procedure ttai_typedconstbuilder.emit_tai(p: tai; def: tdef);
  851. var
  852. kind: ttypedconstkind;
  853. info: taggregateinformation;
  854. begin
  855. { these elements can be aggregates themselves, e.g. a shortstring can
  856. be emitted as a series of bytes and char arrays }
  857. kind:=aggregate_kind(def);
  858. info:=curagginfo;
  859. if (kind<>tck_simple) and
  860. (not assigned(info) or
  861. (info.typ<>kind)) then
  862. internalerror(2014091001);
  863. { if we're emitting a record, handle the padding bytes, and in case of
  864. an anonymous record also add the next field }
  865. if assigned(info) then
  866. begin
  867. if ((info.def.typ=recorddef) or
  868. is_object(info.def)) and
  869. { may add support for these later }
  870. not is_packed_record_or_object(info.def) then
  871. pad_next_field(def);
  872. end;
  873. { emit the data }
  874. do_emit_tai(p,def);
  875. end;
  876. procedure ttai_typedconstbuilder.emit_tai_procvar2procdef(p: tai; pvdef: tprocvardef);
  877. begin
  878. { nothing special by default, since we don't care about the type }
  879. emit_tai(p,pvdef);
  880. end;
  881. function ttai_typedconstbuilder.emit_string_const_common(stringtype: tstringtype; len: asizeint; encoding: tstringencoding; var startlab: tasmlabel): tasmlabofs;
  882. var
  883. string_symofs: asizeint;
  884. charptrdef: tdef;
  885. elesize: word;
  886. begin
  887. result.lab:=startlab;
  888. result.ofs:=0;
  889. { pack the data, so that we don't add unnecessary null bytes after the
  890. constant string }
  891. begin_anonymous_record('$'+get_dynstring_rec_name(stringtype,false,len),1,1,1);
  892. string_symofs:=get_string_symofs(stringtype,false);
  893. { encoding }
  894. emit_tai(tai_const.create_16bit(encoding),u16inttype);
  895. inc(result.ofs,2);
  896. { element size }
  897. case stringtype of
  898. st_ansistring:
  899. begin
  900. elesize:=1;
  901. charptrdef:=charpointertype;
  902. end;
  903. st_unicodestring:
  904. begin
  905. elesize:=2;
  906. charptrdef:=widecharpointertype;
  907. end
  908. else
  909. internalerror(2014080401);
  910. end;
  911. emit_tai(tai_const.create_16bit(elesize),u16inttype);
  912. inc(result.ofs,2);
  913. {$ifdef cpu64bitaddr}
  914. { dummy for alignment }
  915. emit_tai(tai_const.create_32bit(0),u32inttype);
  916. inc(result.ofs,4);
  917. {$endif cpu64bitaddr}
  918. emit_tai(tai_const.create_pint(-1),ptrsinttype);
  919. inc(result.ofs,sizeof(pint));
  920. emit_tai(tai_const.create_pint(len),ptrsinttype);
  921. inc(result.ofs,sizeof(pint));
  922. if string_symofs=0 then
  923. begin
  924. { results in slightly more efficient code }
  925. emit_tai(tai_label.create(result.lab),charptrdef);
  926. result.ofs:=0;
  927. { create new label of the same kind (including whether or not the
  928. name starts with target_asm.labelprefix in case it's AB_LOCAL,
  929. so we keep the difference depending on whether the original was
  930. allocated via getstatic/getlocal/getglobal datalabel) }
  931. startlab:=tasmlabel.create(current_asmdata.AsmSymbolDict,startlab.name+'$strlab',startlab.bind,startlab.typ);
  932. end;
  933. { sanity check }
  934. if result.ofs<>string_symofs then
  935. internalerror(2012051701);
  936. end;
  937. procedure ttai_typedconstbuilder.begin_aggregate_internal(def: tdef; anonymous: boolean);
  938. var
  939. info: taggregateinformation;
  940. tck: ttypedconstkind;
  941. begin
  942. tck:=aggregate_kind(def);
  943. if tck=tck_simple then
  944. exit;
  945. if not assigned(faggregateinformation) then
  946. faggregateinformation:=tfpobjectlist.create
  947. { if we're starting an anonymous record, we can't align it yet because
  948. the alignment depends on the fields that will be added -> we'll do
  949. it at the end }
  950. else if not anonymous then
  951. begin
  952. { add padding if necessary, and update the current field/offset }
  953. info:=curagginfo;
  954. if is_record(curagginfo.def) or
  955. is_object(curagginfo.def) then
  956. pad_next_field(def);
  957. end
  958. { if this is the outer record, no padding is required; the alignment
  959. has to be specified explicitly in that case via get_final_asmlist() }
  960. else if assigned(curagginfo) and
  961. (curagginfo.def.typ=recorddef) then
  962. { mark where we'll have to insert the padding bytes at the end }
  963. mark_anon_aggregate_alignment;
  964. info:=caggregateinformation.create(def,aggregate_kind(def));
  965. faggregateinformation.add(info);
  966. end;
  967. procedure ttai_typedconstbuilder.end_aggregate_internal(def: tdef; anonymous: boolean);
  968. var
  969. info: taggregateinformation;
  970. fillbytes: asizeint;
  971. tck: ttypedconstkind;
  972. begin
  973. tck:=aggregate_kind(def);
  974. if tck=tck_simple then
  975. exit;
  976. info:=curagginfo;
  977. if not assigned(info) then
  978. internalerror(2014091002);
  979. if def<>info.def then
  980. internalerror(2014091205);
  981. { add tail padding if necessary }
  982. if (is_record(def) or
  983. is_object(def)) and
  984. not is_packed_record_or_object(def) then
  985. begin
  986. fillbytes:=def.size-info.curoffset;
  987. while fillbytes>0 do
  988. begin
  989. do_emit_tai(Tai_const.Create_8bit(0),u8inttype);
  990. dec(fillbytes)
  991. end;
  992. end;
  993. { pop and free the information }
  994. faggregateinformation.count:=faggregateinformation.count-1;
  995. info.free;
  996. end;
  997. class function ttai_typedconstbuilder.get_dynstring_rec_name(typ: tstringtype; winlike: boolean; len: asizeint): string;
  998. begin
  999. case typ of
  1000. st_ansistring:
  1001. result:='ansistrrec';
  1002. st_unicodestring,
  1003. st_widestring:
  1004. if (typ=st_unicodestring) or
  1005. not winlike then
  1006. result:='unicodestrrec'
  1007. else
  1008. result:='widestrrec';
  1009. else
  1010. internalerror(2014080402);
  1011. end;
  1012. result:=result+tostr(len);
  1013. end;
  1014. function ttai_typedconstbuilder.emit_ansistring_const(datalist: TAsmList; data: pchar; len: asizeint; encoding: tstringencoding): tasmlabofs;
  1015. var
  1016. s: PChar;
  1017. startlab: tasmlabel;
  1018. ansistrrecdef: trecorddef;
  1019. datadef: tdef;
  1020. datatcb: ttai_typedconstbuilder;
  1021. begin
  1022. start_internal_data_builder(datalist,sec_rodata_norel,'',datatcb,startlab);
  1023. result:=datatcb.emit_string_const_common(st_ansistring,len,encoding,startlab);
  1024. getmem(s,len+1);
  1025. move(data^,s^,len);
  1026. s[len]:=#0;
  1027. { terminating zero included }
  1028. datadef:=getarraydef(cansichartype,len+1);
  1029. datatcb.maybe_begin_aggregate(datadef);
  1030. datatcb.emit_tai(tai_string.create_pchar(s,len+1),datadef);
  1031. datatcb.maybe_end_aggregate(datadef);
  1032. ansistrrecdef:=datatcb.end_anonymous_record;
  1033. finish_internal_data_builder(datatcb,startlab,ansistrrecdef,const_align(sizeof(pointer)));
  1034. end;
  1035. function ttai_typedconstbuilder.emit_unicodestring_const(datalist: TAsmList; data: pointer; encoding: tstringencoding; winlike: boolean):tasmlabofs;
  1036. var
  1037. i, strlength: longint;
  1038. string_symofs: asizeint;
  1039. startlab: tasmlabel;
  1040. datadef: tdef;
  1041. uniwidestrrecdef: trecorddef;
  1042. datatcb: ttai_typedconstbuilder;
  1043. begin
  1044. start_internal_data_builder(datalist,sec_rodata_norel,'',datatcb,startlab);
  1045. strlength:=getlengthwidestring(pcompilerwidestring(data));
  1046. if winlike then
  1047. begin
  1048. result.lab:=startlab;
  1049. datatcb.begin_anonymous_record('$'+get_dynstring_rec_name(st_widestring,true,strlength),
  1050. 4,
  1051. targetinfos[target_info.system]^.alignment.recordalignmin,
  1052. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1053. datatcb.emit_tai(Tai_const.Create_32bit(strlength*cwidechartype.size),s32inttype);
  1054. { can we optimise by placing the string constant label at the
  1055. required offset? }
  1056. string_symofs:=get_string_symofs(st_widestring,true);
  1057. if string_symofs=0 then
  1058. begin
  1059. { yes }
  1060. datatcb.emit_tai(Tai_label.Create(result.lab),widecharpointertype);
  1061. { allocate a separate label for the start of the data (see
  1062. emit_string_const_common() for explanation) }
  1063. startlab:=tasmlabel.create(current_asmdata.AsmSymbolDict,startlab.name+'$strlab',startlab.bind,startlab.typ);
  1064. end
  1065. else
  1066. internalerror(2015031502);
  1067. result.ofs:=string_symofs;
  1068. end
  1069. else
  1070. begin
  1071. result:=datatcb.emit_string_const_common(st_unicodestring,strlength,encoding,startlab);
  1072. end;
  1073. if cwidechartype.size = 2 then
  1074. begin
  1075. datadef:=getarraydef(cwidechartype,strlength+1);
  1076. datatcb.maybe_begin_aggregate(datadef);
  1077. for i:=0 to strlength-1 do
  1078. datatcb.emit_tai(Tai_const.Create_16bit(pcompilerwidestring(data)^.data[i]),cwidechartype);
  1079. { ending #0 }
  1080. datatcb.emit_tai(Tai_const.Create_16bit(0),cwidechartype);
  1081. datatcb.maybe_end_aggregate(datadef);
  1082. uniwidestrrecdef:=datatcb.end_anonymous_record;
  1083. end
  1084. else
  1085. { code generation for other sizes must be written }
  1086. internalerror(200904271);
  1087. finish_internal_data_builder(datatcb,startlab,datadef,const_align(sizeof(pint)));
  1088. end;
  1089. procedure ttai_typedconstbuilder.emit_string_offset(const ll: tasmlabofs; const strlength: longint; const st: tstringtype; const winlikewidestring: boolean; const charptrdef: tdef);
  1090. begin
  1091. emit_tai(Tai_const.Create_sym_offset(ll.lab,ll.ofs),charptrdef);
  1092. end;
  1093. function ttai_typedconstbuilder.emit_shortstring_const(const str: shortstring): tdef;
  1094. begin
  1095. { we use an arraydef instead of a shortstringdef, because we don't have
  1096. functionality in place yet to reuse shortstringdefs of the same length
  1097. and neither the lowlevel nor the llvm typedconst builder cares about
  1098. this difference }
  1099. result:=getarraydef(cansichartype,length(str)+1);
  1100. maybe_begin_aggregate(result);
  1101. emit_tai(Tai_const.Create_8bit(length(str)),u8inttype);
  1102. if str<>'' then
  1103. emit_tai(Tai_string.Create(str),getarraydef(cansichartype,length(str)));
  1104. maybe_end_aggregate(result);
  1105. end;
  1106. procedure ttai_typedconstbuilder.emit_guid_const(const guid: tguid);
  1107. var
  1108. i: longint;
  1109. begin
  1110. maybe_begin_aggregate(rec_tguid);
  1111. { variant record -> must specify which fields get initialised }
  1112. next_field:=tfieldvarsym(rec_tguid.symtable.symlist[0]);
  1113. emit_tai(Tai_const.Create_32bit(longint(guid.D1)),u32inttype);
  1114. next_field:=tfieldvarsym(rec_tguid.symtable.symlist[1]);
  1115. emit_tai(Tai_const.Create_16bit(guid.D2),u16inttype);
  1116. next_field:=tfieldvarsym(rec_tguid.symtable.symlist[2]);
  1117. emit_tai(Tai_const.Create_16bit(guid.D3),u16inttype);
  1118. next_field:=tfieldvarsym(rec_tguid.symtable.symlist[3]);
  1119. { the array }
  1120. maybe_begin_aggregate(tfieldvarsym(rec_tguid.symtable.symlist[3]).vardef);
  1121. for i:=Low(guid.D4) to High(guid.D4) do
  1122. emit_tai(Tai_const.Create_8bit(guid.D4[i]),u8inttype);
  1123. maybe_end_aggregate(tfieldvarsym(rec_tguid.symtable.symlist[3]).vardef);
  1124. maybe_end_aggregate(rec_tguid);
  1125. end;
  1126. procedure ttai_typedconstbuilder.emit_procdef_const(pd: tprocdef);
  1127. begin
  1128. emit_tai(Tai_const.Createname(pd.mangledname,AT_FUNCTION,0),getprocaddressprocvar(pd));
  1129. end;
  1130. procedure ttai_typedconstbuilder.emit_ord_const(value: int64; def: tdef);
  1131. begin
  1132. case def.size of
  1133. 1:
  1134. emit_tai(Tai_const.Create_8bit(byte(value)),def);
  1135. 2:
  1136. emit_tai(Tai_const.Create_16bit(word(value)),def);
  1137. 4:
  1138. emit_tai(Tai_const.Create_32bit(longint(value)),def);
  1139. 8:
  1140. emit_tai(Tai_const.Create_64bit(value),def);
  1141. else
  1142. internalerror(2014100501);
  1143. end;
  1144. end;
  1145. procedure ttai_typedconstbuilder.maybe_begin_aggregate(def: tdef);
  1146. begin
  1147. begin_aggregate_internal(def,false);
  1148. end;
  1149. procedure ttai_typedconstbuilder.maybe_end_aggregate(def: tdef);
  1150. begin
  1151. end_aggregate_internal(def,false);
  1152. end;
  1153. function ttai_typedconstbuilder.begin_anonymous_record(const optionalname: string; packrecords, recordalignmin, maxcrecordalign: shortint): trecorddef;
  1154. var
  1155. anonrecorddef: trecorddef;
  1156. srsym: tsym;
  1157. srsymtable: tsymtable;
  1158. found: boolean;
  1159. begin
  1160. { if the name is specified, we create a typesym with that name in order
  1161. to ensure we can find it again later with that name -> reuse here as
  1162. well if possible (and that also avoids duplicate type name issues) }
  1163. if optionalname<>'' then
  1164. begin
  1165. if optionalname[1]='$' then
  1166. found:=searchsym_in_module(current_module,copy(optionalname,2,length(optionalname)),srsym,srsymtable)
  1167. else
  1168. found:=searchsym_in_module(current_module,optionalname,srsym,srsymtable);
  1169. if found then
  1170. begin
  1171. if (srsym.typ<>typesym) or
  1172. (ttypesym(srsym).typedef.typ<>recorddef) then
  1173. internalerror(2014091207);
  1174. result:=trecorddef(ttypesym(srsym).typedef);
  1175. maybe_begin_aggregate(result);
  1176. exit;
  1177. end;
  1178. end;
  1179. { create skeleton def }
  1180. anonrecorddef:=crecorddef.create_global_internal(optionalname,packrecords,recordalignmin,maxcrecordalign);
  1181. { generic aggregate housekeeping }
  1182. begin_aggregate_internal(anonrecorddef,true);
  1183. { mark as anonymous record }
  1184. curagginfo.anonrecord:=true;
  1185. { in case a descendent wants to do something with the anonrecorddef too }
  1186. result:=anonrecorddef;
  1187. end;
  1188. function ttai_typedconstbuilder.end_anonymous_record: trecorddef;
  1189. var
  1190. info: taggregateinformation;
  1191. anonrecord: boolean;
  1192. begin
  1193. info:=curagginfo;
  1194. if not assigned(info) or
  1195. (info.def.typ<>recorddef) then
  1196. internalerror(2014080201);
  1197. result:=trecorddef(info.def);
  1198. { make a copy, as we need it after info has been freed by
  1199. maybe_end_aggregate(result) }
  1200. anonrecord:=info.anonrecord;
  1201. { finalise the record skeleton (all fields have been added already by
  1202. emit_tai()) -- anonrecord may not be set in case we reused an earlier
  1203. constructed def }
  1204. if anonrecord then
  1205. trecordsymtable(result.symtable).addalignmentpadding;
  1206. end_aggregate_internal(result,true);
  1207. if anonrecord and
  1208. assigned(curagginfo) and
  1209. (curagginfo.def.typ=recorddef) then
  1210. insert_marked_aggregate_alignment(result);
  1211. end;
  1212. procedure ttai_typedconstbuilder.queue_init(todef: tdef);
  1213. begin
  1214. { nested call to init? }
  1215. if fqueue_offset<>low(fqueue_offset) then
  1216. internalerror(2014062101);
  1217. fqueue_offset:=0;
  1218. end;
  1219. procedure ttai_typedconstbuilder.queue_vecn(def: tdef; const index: tconstexprint);
  1220. var
  1221. elelen,
  1222. vecbase: asizeint;
  1223. v: tconstexprint;
  1224. begin
  1225. elelen:=1;
  1226. vecbase:=0;
  1227. case def.typ of
  1228. stringdef :
  1229. ;
  1230. arraydef :
  1231. begin
  1232. if not is_packed_array(def) then
  1233. begin
  1234. elelen:=tarraydef(def).elesize;
  1235. vecbase:=tarraydef(def).lowrange;
  1236. end
  1237. else
  1238. Message(parser_e_packed_dynamic_open_array);
  1239. end;
  1240. else
  1241. Message(parser_e_illegal_expression);
  1242. end;
  1243. { Prevent overflow }
  1244. v:=index-vecbase;
  1245. if (v<int64(low(fqueue_offset))) or (v>int64(high(fqueue_offset))) then
  1246. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(fqueue_offset)),tostr(high(fqueue_offset)));
  1247. if high(fqueue_offset)-fqueue_offset div elelen>v then
  1248. inc(fqueue_offset,elelen*v.svalue)
  1249. else
  1250. message3(type_e_range_check_error_bounds,tostr(index),tostr(vecbase),tostr(high(fqueue_offset)-fqueue_offset div elelen+vecbase))
  1251. end;
  1252. procedure ttai_typedconstbuilder.queue_subscriptn(def: tabstractrecorddef; vs: tfieldvarsym);
  1253. begin
  1254. inc(fqueue_offset,vs.fieldoffset);
  1255. end;
  1256. procedure ttai_typedconstbuilder.queue_typeconvn(fromdef, todef: tdef);
  1257. begin
  1258. { do nothing }
  1259. end;
  1260. procedure ttai_typedconstbuilder.queue_addrn(fromdef, todef: tdef);
  1261. begin
  1262. { do nothing }
  1263. end;
  1264. procedure ttai_typedconstbuilder.queue_emit_proc(pd: tprocdef);
  1265. begin
  1266. if fqueue_offset<>0 then
  1267. internalerror(2014092101);
  1268. emit_procdef_const(pd);
  1269. fqueue_offset:=low(fqueue_offset);
  1270. end;
  1271. procedure ttai_typedconstbuilder.queue_emit_staticvar(vs: tstaticvarsym);
  1272. begin
  1273. { pointerdef because we are emitting a pointer to the staticvarsym
  1274. data, not the data itself }
  1275. emit_tai(Tai_const.Createname(vs.mangledname,fqueue_offset),cpointerdef.getreusable(vs.vardef));
  1276. fqueue_offset:=low(fqueue_offset);
  1277. end;
  1278. procedure ttai_typedconstbuilder.queue_emit_label(l: tlabelsym);
  1279. begin
  1280. emit_tai(Tai_const.Createname(l.mangledname,fqueue_offset),voidcodepointertype);
  1281. fqueue_offset:=low(fqueue_offset);
  1282. end;
  1283. procedure ttai_typedconstbuilder.queue_emit_const(cs: tconstsym);
  1284. begin
  1285. if cs.consttyp<>constresourcestring then
  1286. internalerror(2014062102);
  1287. if fqueue_offset<>0 then
  1288. internalerror(2014062103);
  1289. { warning: update if/when the type of resource strings changes }
  1290. emit_tai(Tai_const.Createname(make_mangledname('RESSTR',cs.owner,cs.name),AT_DATA,sizeof(pint)),cansistringtype);
  1291. fqueue_offset:=low(fqueue_offset);
  1292. end;
  1293. procedure ttai_typedconstbuilder.queue_emit_asmsym(sym: tasmsymbol; def: tdef);
  1294. begin
  1295. { pointerdef, because "sym" represents the address of whatever the
  1296. data is }
  1297. def:=cpointerdef.getreusable(def);
  1298. emit_tai(Tai_const.Create_sym_offset(sym,fqueue_offset),def);
  1299. fqueue_offset:=low(fqueue_offset);
  1300. end;
  1301. procedure ttai_typedconstbuilder.queue_emit_ordconst(value: int64; def: tdef);
  1302. begin
  1303. emit_ord_const(value,def);
  1304. fqueue_offset:=low(fqueue_offset);
  1305. end;
  1306. function ttai_typedconstbuilder.queue_is_active: boolean;
  1307. begin
  1308. result:=fqueue_offset<>low(fqueue_offset)
  1309. end;
  1310. {****************************************************************************
  1311. tai_abstracttypedconst
  1312. ****************************************************************************}
  1313. class constructor ttai_lowleveltypedconstbuilder.classcreate;
  1314. begin
  1315. caggregateinformation:=tlowlevelaggregateinformation;
  1316. end;
  1317. procedure ttai_lowleveltypedconstbuilder.mark_anon_aggregate_alignment;
  1318. var
  1319. marker: tai_marker;
  1320. begin
  1321. marker:=tai_marker.Create(mark_position);
  1322. fasmlist.concat(marker);
  1323. tlowlevelaggregateinformation(curagginfo).anonrecmarker:=marker;
  1324. end;
  1325. procedure ttai_lowleveltypedconstbuilder.insert_marked_aggregate_alignment(def: tdef);
  1326. var
  1327. info: tlowlevelaggregateinformation;
  1328. fillbytes: asizeint;
  1329. begin
  1330. info:=tlowlevelaggregateinformation(curagginfo);
  1331. if not assigned(info.anonrecmarker) then
  1332. internalerror(2014091401);
  1333. fillbytes:=info.prepare_next_field(def);
  1334. while fillbytes>0 do
  1335. begin
  1336. fasmlist.insertafter(tai_const.create_8bit(0),info.anonrecmarker);
  1337. dec(fillbytes);
  1338. end;
  1339. fasmlist.remove(info.anonrecmarker);
  1340. info.anonrecmarker.free;
  1341. info.anonrecmarker:=nil;
  1342. end;
  1343. begin
  1344. ctai_typedconstbuilder:=ttai_lowleveltypedconstbuilder;
  1345. end.