aasmcnst.pas 55 KB

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