aasmcnst.pas 47 KB

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