aasmcnst.pas 46 KB

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