aasmcnst.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. symtype,symdef,symsym;
  25. type
  26. { Warning: never directly create a ttai_typedconstbuilder instance,
  27. instead create a cai_typedconstbuilder (this class can be overridden) }
  28. ttai_lowleveltypedconstbuilder = class abstract
  29. protected
  30. { temporary list in which all data is collected }
  31. fasmlist: tasmlist;
  32. { while queueing elements of a compound expression, this is the current
  33. offset in the top-level array/record }
  34. fqueue_offset: asizeint;
  35. { ensure that finalize_asmlist is called only once }
  36. fasmlist_finalized: boolean;
  37. { returns whether def must be handled as an aggregate on the current
  38. platform }
  39. function aggregate_kind(def: tdef): ttypedconstkind; virtual;
  40. { finalize the asmlist: add the necessary symbols etc }
  41. procedure finalize_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; lab: boolean); virtual;
  42. public
  43. { sym is the symbol for which this typed constant data is emitted. }
  44. constructor create; virtual;
  45. destructor destroy; override;
  46. { add a simple constant data element (p) to the typed constant.
  47. def is the type of the added value }
  48. procedure emit_tai(p: tai; def: tdef); virtual;
  49. { same as above, for a special case: when the def is a procvardef and we
  50. want to use it explicitly as a procvdef (i.e., not as a record with a
  51. code and data pointer in case of a complex procvardef) }
  52. procedure emit_tai_procvar2procdef(p: tai; pvdef: tprocvardef); virtual;
  53. { begin a potential aggregate type. Must be called for any type
  54. that consists of multiple tai constant data entries, or that
  55. represents an aggregate at the Pascal level (a record, a non-dynamic
  56. array, ... }
  57. procedure maybe_begin_aggregate(def: tdef); virtual;
  58. { end a potential aggregate type. Must be paired with every
  59. maybe_begin_aggregate }
  60. procedure maybe_end_aggregate(def: tdef); virtual;
  61. { The next group of routines are for constructing complex expressions.
  62. While parsing a typed constant these operators are encountered from
  63. outer to inner, so that is also the order in which they should be
  64. added to the queue. Only one queue can be active at a time. There is
  65. no default implementation. }
  66. { Init the queue. Gives an internalerror if a queu was already active }
  67. procedure queue_init(todef: tdef); virtual;
  68. { queue an array/string indexing operation (performs all range checking,
  69. so it doesn't have to be duplicated in all descendents). Returns the
  70. length of the elements (in bytes) and the index in the vector
  71. (0-based) }
  72. procedure queue_vecn(def: tdef; const index: tconstexprint); virtual;
  73. { queue a subscripting operation }
  74. procedure queue_subscriptn(def: tabstractrecorddef; vs: tfieldvarsym); virtual;
  75. { queue a type conversion operation }
  76. procedure queue_typeconvn(fromdef, todef: tdef); virtual;
  77. { queue an address taking operation }
  78. procedure queue_addrn(fromdef, todef: tdef); virtual;
  79. { (these default implementations don't do anything but indicate the
  80. queue has been flushed by resetting fqueueu_offset)
  81. finalise the queue (so a new one can be created) and flush the
  82. previously queued operations, applying them in reverse order on a...}
  83. { ... procdef }
  84. procedure queue_emit_proc(pd: tprocdef); virtual;
  85. { ... staticvarsym }
  86. procedure queue_emit_staticvar(vs: tstaticvarsym); virtual;
  87. { ... labelsym }
  88. procedure queue_emit_label(l: tlabelsym); virtual;
  89. { ... constsym }
  90. procedure queue_emit_const(cs: tconstsym); virtual;
  91. { ... asmsym }
  92. procedure queue_emit_asmsym(sym: tasmsymbol; def: tdef); virtual;
  93. { finalize the asmlist (if necessary) and return it.
  94. At the end, the generated data can be found in the asmlist. It will
  95. be freed when the builder is destroyed, so add its contents to
  96. another list first. This property should only be accessed once all
  97. data has been added. }
  98. function get_final_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: longint; lab: boolean): tasmlist;
  99. end;
  100. ttai_lowleveltypedconstbuilderclass = class of ttai_lowleveltypedconstbuilder;
  101. var
  102. ctai_typedconstbuilder: ttai_lowleveltypedconstbuilderclass;
  103. implementation
  104. uses
  105. verbose,globals,
  106. symconst,defutil;
  107. {*****************************************************************************
  108. ttai_lowleveltypedconstbuilder
  109. *****************************************************************************}
  110. function ttai_lowleveltypedconstbuilder.aggregate_kind(def: tdef): ttypedconstkind;
  111. begin
  112. if (def.typ in [recorddef,filedef,variantdef]) or
  113. is_object(def) or
  114. ((def.typ=procvardef) and
  115. not tprocvardef(def).is_addressonly) then
  116. result:=tck_record
  117. else if ((def.typ=arraydef) and
  118. not is_dynamic_array(def)) or
  119. ((def.typ=setdef) and
  120. not is_smallset(def)) or
  121. is_shortstring(def) then
  122. result:=tck_array
  123. else
  124. result:=tck_simple;
  125. end;
  126. procedure ttai_lowleveltypedconstbuilder.finalize_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; lab: boolean);
  127. var
  128. prelist: tasmlist;
  129. begin
  130. prelist:=tasmlist.create_without_marker;
  131. maybe_new_object_file(prelist);
  132. { only now add items based on the symbolname, because it may be
  133. modified by the "section" specifier in case of a typed constant }
  134. new_section(prelist,section,secname,const_align(alignment));
  135. if not lab then
  136. if sym.bind=AB_GLOBAL then
  137. prelist.concat(tai_symbol.Create_Global(sym,0))
  138. else
  139. prelist.concat(tai_symbol.Create(sym,0))
  140. else
  141. prelist.concat(tai_label.Create(tasmlabel(sym)));
  142. { insert the symbol information before the data }
  143. fasmlist.insertlist(prelist);
  144. { end of the symbol }
  145. fasmlist.concat(tai_symbol_end.Createname(sym.name));
  146. { free the temporary list }
  147. prelist.free;
  148. end;
  149. function ttai_lowleveltypedconstbuilder.get_final_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: longint; lab: boolean): tasmlist;
  150. begin
  151. if not fasmlist_finalized then
  152. begin
  153. finalize_asmlist(sym,def,section,secname,alignment,lab);
  154. fasmlist_finalized:=true;
  155. end;
  156. result:=fasmlist;
  157. end;
  158. constructor ttai_lowleveltypedconstbuilder.create;
  159. begin
  160. inherited create;
  161. fasmlist:=tasmlist.create_without_marker;
  162. { queue is empty }
  163. fqueue_offset:=low(fqueue_offset);
  164. end;
  165. destructor ttai_lowleveltypedconstbuilder.destroy;
  166. begin
  167. { the queue should have been flushed if it was used }
  168. if fqueue_offset<>low(fqueue_offset) then
  169. internalerror(2014062901);
  170. fasmlist.free;
  171. inherited destroy;
  172. end;
  173. procedure ttai_lowleveltypedconstbuilder.emit_tai(p: tai; def: tdef);
  174. begin
  175. { by default, we ignore the def info since we don't care about it at the
  176. the assembler level }
  177. fasmlist.concat(p);
  178. end;
  179. procedure ttai_lowleveltypedconstbuilder.emit_tai_procvar2procdef(p: tai; pvdef: tprocvardef);
  180. begin
  181. { nothing special by default, since we don't care about the type }
  182. emit_tai(p,pvdef);
  183. end;
  184. procedure ttai_lowleveltypedconstbuilder.maybe_begin_aggregate(def: tdef);
  185. begin
  186. { do nothing }
  187. end;
  188. procedure ttai_lowleveltypedconstbuilder.maybe_end_aggregate(def: tdef);
  189. begin
  190. { do nothing }
  191. end;
  192. procedure ttai_lowleveltypedconstbuilder.queue_init(todef: tdef);
  193. begin
  194. { nested call to init? }
  195. if fqueue_offset<>low(fqueue_offset) then
  196. internalerror(2014062101);
  197. fqueue_offset:=0;
  198. end;
  199. procedure ttai_lowleveltypedconstbuilder.queue_vecn(def: tdef; const index: tconstexprint);
  200. var
  201. elelen,
  202. vecbase: asizeint;
  203. v: tconstexprint;
  204. begin
  205. elelen:=1;
  206. vecbase:=0;
  207. case def.typ of
  208. stringdef :
  209. ;
  210. arraydef :
  211. begin
  212. if not is_packed_array(def) then
  213. begin
  214. elelen:=tarraydef(def).elesize;
  215. vecbase:=tarraydef(def).lowrange;
  216. end
  217. else
  218. Message(parser_e_packed_dynamic_open_array);
  219. end;
  220. else
  221. Message(parser_e_illegal_expression);
  222. end;
  223. { Prevent overflow }
  224. v:=index-vecbase;
  225. if (v<int64(low(fqueue_offset))) or (v>int64(high(fqueue_offset))) then
  226. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(fqueue_offset)),tostr(high(fqueue_offset)));
  227. if high(fqueue_offset)-fqueue_offset div elelen>v then
  228. inc(fqueue_offset,elelen*v.svalue)
  229. else
  230. message3(type_e_range_check_error_bounds,tostr(index),tostr(vecbase),tostr(high(fqueue_offset)-fqueue_offset div elelen+vecbase))
  231. end;
  232. procedure ttai_lowleveltypedconstbuilder.queue_subscriptn(def: tabstractrecorddef; vs: tfieldvarsym);
  233. begin
  234. inc(fqueue_offset,vs.fieldoffset);
  235. end;
  236. procedure ttai_lowleveltypedconstbuilder.queue_typeconvn(fromdef, todef: tdef);
  237. begin
  238. { do nothing }
  239. end;
  240. procedure ttai_lowleveltypedconstbuilder.queue_addrn(fromdef, todef: tdef);
  241. begin
  242. { do nothing }
  243. end;
  244. procedure ttai_lowleveltypedconstbuilder.queue_emit_proc(pd: tprocdef);
  245. begin
  246. emit_tai(Tai_const.Createname(pd.mangledname,fqueue_offset),pd);
  247. fqueue_offset:=low(fqueue_offset);
  248. end;
  249. procedure ttai_lowleveltypedconstbuilder.queue_emit_staticvar(vs: tstaticvarsym);
  250. begin
  251. { getpointerdef because we are emitting a pointer to the staticvarsym
  252. data, not the data itself }
  253. emit_tai(Tai_const.Createname(vs.mangledname,fqueue_offset),getpointerdef(vs.vardef));
  254. fqueue_offset:=low(fqueue_offset);
  255. end;
  256. procedure ttai_lowleveltypedconstbuilder.queue_emit_label(l: tlabelsym);
  257. begin
  258. emit_tai(Tai_const.Createname(l.mangledname,fqueue_offset),voidcodepointertype);
  259. fqueue_offset:=low(fqueue_offset);
  260. end;
  261. procedure ttai_lowleveltypedconstbuilder.queue_emit_const(cs: tconstsym);
  262. begin
  263. if cs.consttyp<>constresourcestring then
  264. internalerror(2014062102);
  265. if fqueue_offset<>0 then
  266. internalerror(2014062103);
  267. { warning: update if/when the type of resource strings changes }
  268. emit_tai(Tai_const.Createname(make_mangledname('RESSTR',cs.owner,cs.name),AT_DATA,sizeof(pint)),cansistringtype);
  269. fqueue_offset:=low(fqueue_offset);
  270. end;
  271. procedure ttai_lowleveltypedconstbuilder.queue_emit_asmsym(sym: tasmsymbol; def: tdef);
  272. begin
  273. { getpointerdef, because "sym" represents the address of whatever the
  274. data is }
  275. def:=getpointerdef(def);
  276. emit_tai(Tai_const.Create_sym(sym),def);
  277. fqueue_offset:=low(fqueue_offset);
  278. end;
  279. begin
  280. ctai_typedconstbuilder:=ttai_lowleveltypedconstbuilder;
  281. end.