2
0

llvmdef.pas 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. {
  2. Copyright (c) 2013 by Jonas Maebe
  3. This unit implements some LLVM type helper routines.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {$i fpcdefs.inc}
  18. unit llvmdef;
  19. interface
  20. uses
  21. cclasses,globtype,
  22. aasmbase,
  23. parabase,
  24. symconst,symbase,symtype,symdef,
  25. llvmbase;
  26. type
  27. { there are three different circumstances in which procdefs are used:
  28. a) definition of a procdef that's implemented in the current module
  29. b) declaration of an external routine that's called in the current one
  30. c) alias declaration of a procdef implemented in the current module
  31. d) defining a procvar type
  32. The main differences between the contexts are:
  33. a) information about sign extension of result type, proc name, parameter names & sign-extension info & types
  34. b) information about sign extension of result type, proc name, no parameter names, with parameter sign-extension info & types
  35. c) no information about sign extension of result type, proc name, no parameter names, no information about sign extension of parameters, parameter types
  36. d) no information about sign extension of result type, no proc name, no parameter names, no information about sign extension of parameters, parameter types
  37. }
  38. tllvmprocdefdecltype = (lpd_def,lpd_decl,lpd_alias,lpd_procvar);
  39. { returns the identifier to use as typename for a def in llvm (llvm only
  40. allows naming struct types) -- only supported for tabstractrecorddef
  41. descendantds and complex procvars }
  42. function llvmtypeidentifier(def: tdef): TSymStr;
  43. { encode a type into the internal format used by LLVM (for a type
  44. declaration) }
  45. function llvmencodetypedecl(def: tdef): TSymStr;
  46. { same as above, but use a type name if possible (for any use) }
  47. function llvmencodetypename(def: tdef; pointedtype: boolean = false): TSymStr;
  48. { encode a procdef/procvardef into the internal format used by LLVM }
  49. function llvmencodeproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype): TSymStr;
  50. { incremental version of the above }
  51. procedure llvmaddencodedproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype; var encodedstr: TSymStr);
  52. { function result types may have to be represented differently, e.g. a
  53. record consisting of 4 longints must be returned as a record consisting of
  54. two int64's on x86-64. This function is used to create (and reuse)
  55. temporary recorddefs for such purposes.}
  56. function llvmgettemprecorddef(const fieldtypes: array of tdef; packrecords, recordalignmin: shortint): trecorddef;
  57. { get the llvm type corresponding to a parameter, e.g. a record containing
  58. two integer int64 for an arbitrary record split over two individual int64
  59. parameters, or an int32 for an int16 parameter on a platform that requires
  60. such parameters to be zero/sign extended. The second parameter can be used
  61. to get the type before zero/sign extension, as e.g. required to generate
  62. function declarations. }
  63. function llvmgetcgparadef(const cgpara: tcgpara; beforevalueext: boolean; callercallee: tcallercallee): tdef;
  64. { can be used to extract the value extension info from acgpara. Pass in
  65. the def of the cgpara as first parameter and a local variable holding
  66. a copy of the def of the location (value extension only makes sense for
  67. ordinal parameters that are smaller than a single location). The routine
  68. will return the def of the location without sign extension (if applicable)
  69. and the kind of sign extension that was originally performed in the
  70. signext parameter }
  71. procedure llvmextractvalueextinfo(paradef: tdef; var paralocdef: tdef; out signext: tllvmvalueextension);
  72. { returns whether a paraloc should be translated into an llvm "byval"
  73. parameter. These are declared as pointers to a particular type, but
  74. usually turned into copies onto the stack. The exact behaviour for
  75. parameters that should be passed in registers is undefined and depends on
  76. the platform, and furthermore this modifier sometimes inhibits
  77. optimizations. As a result,we only use it for aggregate parameters of
  78. which we know that they should be passed on the stack }
  79. function llvmbyvalparaloc(paraloc: pcgparalocation): boolean;
  80. { returns whether a def is representated by an aggregate type in llvm
  81. (struct, array) }
  82. function llvmaggregatetype(def: tdef): boolean;
  83. function llvmconvop(var fromsize, tosize: tdef; inregs: boolean): tllvmop;
  84. { mangle a global identifier so that it's recognised by LLVM as a global
  85. (in the sense of module-global) label and so that it won't mangle the
  86. name further according to platform conventions (we already did that) }
  87. function llvmmangledname(const s: TSymStr): TSymStr;
  88. { convert a parameter attribute to a string. Depending on the target
  89. LLVM version, we may have to add the dereferenced parameter type as well }
  90. function llvmparatypeattr(const attr: TSymStr; paradef: tdef; strippointer: boolean): TSymStr;
  91. function llvmasmsymname(const sym: TAsmSymbol): TSymStr;
  92. function llvmfloatintrinsicsuffix(def: tfloatdef): TIDString;
  93. implementation
  94. uses
  95. globals,cutils,constexp,
  96. verbose,systems,
  97. fmodule,
  98. symtable,symsym,
  99. llvmsym,hlcgobj,
  100. defutil,blockutl,cgbase,paramgr,
  101. llvminfo,cpubase;
  102. {******************************************************************
  103. Type encoding
  104. *******************************************************************}
  105. function llvmtypeidentifier(def: tdef): TSymStr;
  106. begin
  107. if assigned(def.typesym) then
  108. result:='%"typ.'+def.fullownerhierarchyname(false)+def.typesym.realname+'"'
  109. else
  110. result:='%"typ.'+def.fullownerhierarchyname(false)+def.unique_id_str+'"';
  111. end;
  112. function llvmaggregatetype(def: tdef): boolean;
  113. begin
  114. result:=
  115. (def.typ in [recorddef,filedef,variantdef]) or
  116. ((def.typ=arraydef) and
  117. not is_dynamic_array(def)) or
  118. ((def.typ=setdef) and
  119. not is_smallset(def)) or
  120. is_shortstring(def) or
  121. is_object(def) or
  122. ((def.typ=procvardef) and
  123. not tprocvardef(def).is_addressonly)
  124. end;
  125. function llvmconvop(var fromsize, tosize: tdef; inregs: boolean): tllvmop;
  126. var
  127. fromregtyp,
  128. toregtyp: tregistertype;
  129. frombytesize,
  130. tobytesize: asizeint;
  131. begin
  132. fromregtyp:=chlcgobj.def2regtyp(fromsize);
  133. toregtyp:=chlcgobj.def2regtyp(tosize);
  134. { int to pointer or vice versa }
  135. if fromregtyp=R_ADDRESSREGISTER then
  136. begin
  137. case toregtyp of
  138. R_INTREGISTER:
  139. result:=la_ptrtoint;
  140. R_ADDRESSREGISTER:
  141. result:=la_bitcast;
  142. else
  143. result:=la_ptrtoint_to_x;
  144. end;
  145. end
  146. else if toregtyp=R_ADDRESSREGISTER then
  147. begin
  148. case fromregtyp of
  149. R_INTREGISTER:
  150. result:=la_inttoptr;
  151. R_ADDRESSREGISTER:
  152. result:=la_bitcast;
  153. else
  154. result:=la_x_to_inttoptr;
  155. end;
  156. end
  157. else
  158. begin
  159. { treat comp and currency as extended in registers (see comment at start
  160. of thlgcobj.a_loadfpu_ref_reg) }
  161. if inregs and
  162. (fromsize.typ=floatdef) then
  163. begin
  164. if tfloatdef(fromsize).floattype in [s64comp,s64currency] then
  165. fromsize:=sc80floattype;
  166. { at the value level, s80real and sc80real are the same }
  167. if tfloatdef(fromsize).floattype<>s80real then
  168. frombytesize:=fromsize.size
  169. else
  170. frombytesize:=sc80floattype.size;
  171. end
  172. else
  173. frombytesize:=fromsize.size;
  174. if inregs and
  175. (tosize.typ=floatdef) then
  176. begin
  177. if tfloatdef(tosize).floattype in [s64comp,s64currency] then
  178. tosize:=sc80floattype;
  179. if tfloatdef(tosize).floattype<>s80real then
  180. tobytesize:=tosize.size
  181. else
  182. tobytesize:=sc80floattype.size;
  183. end
  184. else
  185. tobytesize:=tosize.size;
  186. { need zero/sign extension, float truncation or plain bitcast? }
  187. if tobytesize<>frombytesize then
  188. begin
  189. case fromregtyp of
  190. R_FPUREGISTER,
  191. R_MMREGISTER:
  192. begin
  193. { todo: update once we support vectors }
  194. if not(toregtyp in [R_FPUREGISTER,R_MMREGISTER]) then
  195. internalerror(2014062202);
  196. if tobytesize<frombytesize then
  197. result:=la_fptrunc
  198. else
  199. result:=la_fpext
  200. end;
  201. else
  202. begin
  203. if tobytesize<frombytesize then
  204. result:=la_trunc
  205. else if is_signed(fromsize) then
  206. { fromsize is signed -> sign extension }
  207. result:=la_sext
  208. else
  209. result:=la_zext;
  210. end;
  211. end;
  212. end
  213. else if (fromsize=llvmbool1type) and
  214. (tosize<>llvmbool1type) then
  215. begin
  216. if is_cbool(tosize) then
  217. result:=la_sext
  218. else
  219. result:=la_zext
  220. end
  221. else if (tosize=llvmbool1type) and
  222. (fromsize<>llvmbool1type) then
  223. begin
  224. { would have to compare with 0, can't just take the lowest bit }
  225. if is_cbool(fromsize) then
  226. internalerror(2016052001)
  227. else
  228. result:=la_trunc
  229. end
  230. else
  231. result:=la_bitcast;
  232. end;
  233. end;
  234. function llvmmangledname(const s: TSymStr): TSymStr;
  235. begin
  236. if copy(s,1,length('llvm.'))<>'llvm.' then
  237. if s[1]<>'"' then
  238. result:='@"\01'+s+'"'
  239. else
  240. begin
  241. { already quoted -> insert \01 and prepend @ }
  242. result:='@'+s;
  243. insert('\01',result,3);
  244. end
  245. else
  246. result:='@'+s
  247. end;
  248. function llvmparatypeattr(const attr: TSymStr; paradef: tdef; strippointer: boolean): TSymStr;
  249. begin
  250. result:=attr;
  251. if llvmflag_para_attr_type in llvmversion_properties[current_settings.llvmversion] then
  252. begin
  253. if not strippointer then
  254. result:=result+'('+llvmencodetypename(paradef)+')'
  255. else
  256. begin
  257. if paradef.typ<>pointerdef then
  258. internalerror(2022060310);
  259. if not is_void(tpointerdef(paradef).pointeddef) then
  260. result:=result+'('+llvmencodetypename(tpointerdef(paradef).pointeddef)+')'
  261. else
  262. result:=result+'(i8)'
  263. end;
  264. end;
  265. end;
  266. function llvmasmsymname(const sym: TAsmSymbol): TSymStr;
  267. begin
  268. { AT_ADDR and AT_LABEL represent labels in the code, which have
  269. a different type in llvm compared to (global) data labels }
  270. if sym.bind=AB_TEMP then
  271. result:='%'+sym.name
  272. else if not(sym.typ in [AT_LABEL,AT_ADDR]) then
  273. result:=llvmmangledname(sym.name)
  274. else
  275. result:='label %'+sym.name;
  276. end;
  277. function llvmfloatintrinsicsuffix(def: tfloatdef): TIDString;
  278. begin
  279. case def.floattype of
  280. s32real:
  281. result:='_f32';
  282. s64real:
  283. result:='_f64';
  284. s80real,sc80real:
  285. result:='_f80';
  286. s128real:
  287. result:='_f128';
  288. else
  289. { comp/currency need to be converted to s(c)80real first }
  290. internalerror(2019122902);
  291. end;
  292. end;
  293. function llvmbyvalparaloc(paraloc: pcgparalocation): boolean;
  294. begin
  295. { "byval" is broken for register paras on several platforms in llvm
  296. (search for "byval" in llvm's bug tracker). Additionally, it should only
  297. be used to pass aggregate parameters on the stack, because it reportedly
  298. inhibits llvm's midlevel optimizers.
  299. Exception (for now?): parameters that have special shifting
  300. requirements, because modelling those in llvm is not easy (and clang
  301. nor llvm-gcc seem to do so either) }
  302. result:=
  303. ((paraloc^.loc=LOC_REFERENCE) and
  304. llvmaggregatetype(paraloc^.def)) or
  305. ((paraloc^.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  306. (paraloc^.shiftval<>0))
  307. end;
  308. procedure llvmaddencodedabstractrecordtype(def: tabstractrecorddef; var encodedstr: TSymStr); forward;
  309. type
  310. tllvmencodeflag = (lef_inaggregate, lef_noimplicitderef, lef_typedecl, lef_removeouterpointer);
  311. tllvmencodeflags = set of tllvmencodeflag;
  312. procedure llvmaddencodedtype_intern(def: tdef; const flags: tllvmencodeflags; var encodedstr: TSymStr);
  313. var
  314. def_is_address: boolean;
  315. begin
  316. def_is_address:=false;
  317. if ((lef_removeouterpointer in flags) or
  318. (llvmflag_opaque_ptr in llvmversion_properties[current_settings.llvmversion])) and
  319. is_address(def) and
  320. (def<>llvm_metadatatype) then
  321. def_is_address:=true
  322. else if lef_removeouterpointer in flags then
  323. internalerror(2022060813);
  324. if (llvmflag_opaque_ptr in llvmversion_properties[current_settings.llvmversion]) and
  325. not(lef_removeouterpointer in flags) and
  326. def_is_address then
  327. begin
  328. if not(([lef_typedecl,lef_noimplicitderef]*flags<>[]) and
  329. is_implicit_pointer_object_type(def)) and
  330. not((def.typ=procdef) and
  331. not(lef_typedecl in flags)) then
  332. begin
  333. encodedstr:=encodedstr+'ptr';
  334. exit;
  335. end;
  336. end;
  337. case def.typ of
  338. stringdef :
  339. begin
  340. case tstringdef(def).stringtype of
  341. st_widestring,
  342. st_unicodestring:
  343. { the variable does not point to the header, but to a
  344. null-terminated string/array with undefined bounds }
  345. if not(lef_removeouterpointer in flags) then
  346. encodedstr:=encodedstr+'i16*'
  347. else
  348. encodedstr:=encodedstr+'i16';
  349. st_ansistring:
  350. if not(lef_removeouterpointer in flags) then
  351. encodedstr:=encodedstr+'i8*'
  352. else
  353. encodedstr:=encodedstr+'i8';
  354. st_shortstring:
  355. begin
  356. { length byte followed by string bytes }
  357. if tstringdef(def).len>0 then
  358. encodedstr:=encodedstr+'['+tostr(tstringdef(def).len+1)+' x i8]'
  359. else
  360. encodedstr:=encodedstr+'[0 x i8]';
  361. end
  362. else
  363. internalerror(2013100201);
  364. end;
  365. end;
  366. enumdef:
  367. begin
  368. encodedstr:=encodedstr+'i'+tostr(def.size*8);
  369. end;
  370. orddef :
  371. begin
  372. if is_void(def) then
  373. encodedstr:=encodedstr+'void'
  374. { mainly required because comparison operations return i1, and
  375. we need a way to represent the i1 type in Pascal. We don't
  376. reuse pasbool1type, because putting an i1 in a record or
  377. passing it as a parameter may result in unexpected behaviour }
  378. else if def=llvmbool1type then
  379. encodedstr:=encodedstr+'i1'
  380. else if torddef(def).ordtype<>customint then
  381. encodedstr:=encodedstr+'i'+tostr(def.size*8)
  382. else
  383. encodedstr:=encodedstr+'i'+tostr(def.packedbitsize);
  384. end;
  385. pointerdef :
  386. begin
  387. if is_voidpointer(def) then
  388. begin
  389. if not(lef_removeouterpointer in flags) then
  390. encodedstr:=encodedstr+'i8*'
  391. else
  392. encodedstr:=encodedstr+'i8';
  393. end
  394. else
  395. begin
  396. llvmaddencodedtype_intern(tpointerdef(def).pointeddef,[],encodedstr);
  397. if not(lef_removeouterpointer in flags) then
  398. encodedstr:=encodedstr+'*';
  399. end;
  400. end;
  401. floatdef :
  402. begin
  403. case tfloatdef(def).floattype of
  404. s32real:
  405. encodedstr:=encodedstr+'float';
  406. s64real:
  407. encodedstr:=encodedstr+'double';
  408. { necessary to be able to force our own size/alignment }
  409. s80real:
  410. { prevent llvm from allocating the standard ABI size for
  411. extended }
  412. if lef_inaggregate in flags then
  413. encodedstr:=encodedstr+'[10 x i8]'
  414. else
  415. encodedstr:=encodedstr+'x86_fp80';
  416. sc80real:
  417. encodedstr:=encodedstr+'x86_fp80';
  418. s64comp,
  419. s64currency:
  420. encodedstr:=encodedstr+'i64';
  421. s128real:
  422. {$if defined(powerpc) or defined(powerpc128)}
  423. encodedstr:=encodedstr+'ppc_fp128';
  424. {$else}
  425. encodedstr:=encodedstr+'fp128';
  426. {$endif}
  427. end;
  428. end;
  429. filedef :
  430. begin
  431. case tfiledef(def).filetyp of
  432. ft_text :
  433. llvmaddencodedtype_intern(search_system_type('TEXTREC').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  434. ft_typed :
  435. begin
  436. { in case of ISO-like I/O, the typed file def includes a
  437. get/put buffer of the size of the file's elements }
  438. if (m_isolike_io in current_settings.modeswitches) and
  439. not is_void(tfiledef(def).typedfiledef) then
  440. encodedstr:=encodedstr+'<{';
  441. llvmaddencodedtype_intern(search_system_type('FILEREC').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  442. if (m_isolike_io in current_settings.modeswitches) and
  443. not is_void(tfiledef(def).typedfiledef) then
  444. begin
  445. encodedstr:=encodedstr+',[';
  446. encodedstr:=encodedstr+tostr(tfiledef(def).typedfiledef.size);
  447. encodedstr:=encodedstr+' x i8]}>'
  448. end;
  449. end;
  450. ft_untyped :
  451. llvmaddencodedtype_intern(search_system_type('FILEREC').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  452. end;
  453. end;
  454. recorddef :
  455. begin
  456. { avoid endlessly recursive definitions }
  457. if not(lef_typedecl in flags) and
  458. not(df_llvm_no_typename in def.defoptions) then
  459. encodedstr:=encodedstr+llvmtypeidentifier(def)
  460. else
  461. llvmaddencodedabstractrecordtype(trecorddef(def),encodedstr);
  462. end;
  463. variantdef :
  464. begin
  465. llvmaddencodedtype_intern(search_system_type('TVARDATA').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  466. end;
  467. classrefdef :
  468. begin
  469. if is_class(tclassrefdef(def).pointeddef) then
  470. begin
  471. llvmaddencodedtype_intern(tobjectdef(tclassrefdef(def).pointeddef).vmt_def,flags-[lef_removeouterpointer],encodedstr);
  472. if not(lef_removeouterpointer in flags) then
  473. encodedstr:=encodedstr+'*';
  474. end
  475. else if is_objcclass(tclassrefdef(def).pointeddef) then
  476. llvmaddencodedtype_intern(objc_idtype,flags-[lef_removeouterpointer],encodedstr)
  477. else if not(lef_removeouterpointer in flags) then
  478. encodedstr:=encodedstr+'i8*'
  479. else
  480. encodedstr:=encodedstr+'i8'
  481. end;
  482. setdef :
  483. begin
  484. { just an array as far as llvm is concerned; don't use a "packed
  485. array of i1" or so, this requires special support in backends
  486. and guarantees nothing about the internal format }
  487. if is_smallset(def) then
  488. llvmaddencodedtype_intern(cgsize_orddef(def_cgsize(def)),[lef_inaggregate],encodedstr)
  489. else
  490. encodedstr:=encodedstr+'['+tostr(tsetdef(def).size)+' x i8]';
  491. end;
  492. formaldef :
  493. begin
  494. if def<>llvm_metadatatype then
  495. { var/const/out x (always treated as "pass by reference" -> don't
  496. add extra "*" here) }
  497. encodedstr:=encodedstr+'i8'
  498. else
  499. encodedstr:=encodedstr+'metadata'
  500. end;
  501. arraydef :
  502. begin
  503. if tarraydef(def).is_hwvector then
  504. begin
  505. encodedstr:=encodedstr+'<'+tostr(tarraydef(def).elecount)+' x ';
  506. llvmaddencodedtype_intern(tarraydef(def).elementdef,[lef_inaggregate],encodedstr);
  507. encodedstr:=encodedstr+'>';
  508. end
  509. else if is_array_of_const(def) then
  510. begin
  511. encodedstr:=encodedstr+'[0 x ';
  512. llvmaddencodedtype_intern(search_system_type('TVARREC').typedef,[lef_inaggregate],encodedstr);
  513. encodedstr:=encodedstr+']';
  514. end
  515. else if is_open_array(def) then
  516. begin
  517. encodedstr:=encodedstr+'[0 x ';
  518. llvmaddencodedtype_intern(tarraydef(def).elementdef,[lef_inaggregate],encodedstr);
  519. encodedstr:=encodedstr+']';
  520. end
  521. else if is_dynamic_array(def) then
  522. begin
  523. llvmaddencodedtype_intern(tarraydef(def).elementdef,[lef_inaggregate],encodedstr);
  524. if not(lef_removeouterpointer in flags) then
  525. encodedstr:=encodedstr+'*';
  526. end
  527. else if is_packed_array(def) and
  528. (tarraydef(def).elementdef.typ in [enumdef,orddef]) then
  529. begin
  530. { encode as an array of bytes rather than as an array of
  531. packedbitsloadsize(elesize), because even if the load size
  532. is e.g. 2 bytes, the array may only be 1 or 3 bytes long
  533. (and if this array is inside a record, it must not be
  534. encoded as a type that is too long) }
  535. encodedstr:=encodedstr+'['+tostr(tarraydef(def).size)+' x ';
  536. llvmaddencodedtype_intern(u8inttype,[lef_inaggregate],encodedstr);
  537. encodedstr:=encodedstr+']';
  538. end
  539. else
  540. begin
  541. encodedstr:=encodedstr+'['+tostr(tarraydef(def).elecount)+' x ';
  542. llvmaddencodedtype_intern(tarraydef(def).elementdef,[lef_inaggregate],encodedstr);
  543. encodedstr:=encodedstr+']';
  544. end;
  545. end;
  546. procdef,
  547. procvardef :
  548. begin
  549. if (def.typ=procdef) or
  550. tprocvardef(def).is_addressonly then
  551. begin
  552. llvmaddencodedproctype(tabstractprocdef(def),'',lpd_procvar,encodedstr);
  553. if not(lef_removeouterpointer in flags) then
  554. begin
  555. if def.typ=procvardef then
  556. encodedstr:=encodedstr+'*'
  557. end
  558. end
  559. else if not(lef_typedecl in flags) then
  560. begin
  561. { in case the procvardef recursively references itself, e.g.
  562. via a pointer }
  563. encodedstr:=encodedstr+llvmtypeidentifier(def);
  564. { blocks are implicit pointers }
  565. if not(lef_removeouterpointer in flags) and
  566. is_block(def) then
  567. encodedstr:=encodedstr+'*'
  568. end
  569. else if is_block(def) then
  570. begin
  571. llvmaddencodedtype_intern(get_block_literal_type_for_proc(tabstractprocdef(def)),flags,encodedstr);
  572. end
  573. else
  574. begin
  575. encodedstr:=encodedstr+'<{';
  576. { code pointer }
  577. llvmaddencodedproctype(tabstractprocdef(def),'',lpd_procvar,encodedstr);
  578. { data pointer (maybe todo: generate actual layout if
  579. available) }
  580. encodedstr:=encodedstr+'*, i8*}>';
  581. end;
  582. end;
  583. objectdef :
  584. case tobjectdef(def).objecttype of
  585. odt_class,
  586. odt_objcclass,
  587. odt_object,
  588. odt_cppclass:
  589. begin
  590. if not(lef_typedecl in flags) then
  591. encodedstr:=encodedstr+llvmtypeidentifier(def)
  592. else
  593. llvmaddencodedabstractrecordtype(tabstractrecorddef(def),encodedstr);
  594. if ([lef_typedecl,lef_noimplicitderef,lef_removeouterpointer]*flags=[]) and
  595. is_implicit_pointer_object_type(def) then
  596. encodedstr:=encodedstr+'*'
  597. end;
  598. odt_interfacecom,
  599. odt_interfacecorba,
  600. odt_dispinterface:
  601. begin
  602. { type is a pointer to a pointer to the vmt }
  603. if ([lef_typedecl,lef_noimplicitderef]*flags=[]) and
  604. (llvmflag_opaque_ptr in llvmversion_properties[current_settings.llvmversion]) then
  605. encodedstr:=encodedstr+'ptr'
  606. else
  607. begin
  608. llvmaddencodedtype_intern(tobjectdef(def).vmt_def,flags,encodedstr);
  609. if ([lef_typedecl,lef_noimplicitderef]*flags=[]) then
  610. if not(lef_removeouterpointer in flags) then
  611. encodedstr:=encodedstr+'**'
  612. else
  613. encodedstr:=encodedstr+'*'
  614. end;
  615. end;
  616. odt_interfacecom_function,
  617. odt_interfacecom_property,
  618. odt_objcprotocol:
  619. begin
  620. { opaque for now }
  621. if not(lef_removeouterpointer in flags) then
  622. if (llvmflag_opaque_ptr in llvmversion_properties[current_settings.llvmversion]) then
  623. encodedstr:=encodedstr+'ptr'
  624. else
  625. encodedstr:=encodedstr+'i8*'
  626. else
  627. encodedstr:=encodedstr+'i8'
  628. end;
  629. odt_helper:
  630. llvmaddencodedtype_intern(tobjectdef(def).extendeddef,flags,encodedstr);
  631. else
  632. internalerror(2013100601);
  633. end;
  634. undefineddef:
  635. begin
  636. internalerror(2022052301);
  637. end;
  638. errordef :
  639. internalerror(2013100604);
  640. else
  641. internalerror(2013100603);
  642. end;
  643. end;
  644. function llvmencodetypename(def: tdef; pointedtype: boolean = false): TSymStr;
  645. var
  646. flags: tllvmencodeflags;
  647. begin
  648. result:='';
  649. if not pointedtype then
  650. flags:=[]
  651. else
  652. flags:=[lef_removeouterpointer];
  653. llvmaddencodedtype_intern(def,flags,result);
  654. end;
  655. procedure llvmaddencodedtype(def: tdef; inaggregate: boolean; var encodedstr: TSymStr);
  656. var
  657. flags: tllvmencodeflags;
  658. begin
  659. if inaggregate then
  660. flags:=[lef_inaggregate]
  661. else
  662. flags:=[];
  663. llvmaddencodedtype_intern(def,flags,encodedstr);
  664. end;
  665. procedure llvmaddencodedabstractrecordtype(def: tabstractrecorddef; var encodedstr: TSymStr);
  666. var
  667. st: tllvmshadowsymtable;
  668. symdeflist: tfpobjectlist;
  669. i: longint;
  670. nopacked: boolean;
  671. begin
  672. st:=tabstractrecordsymtable(def.symtable).llvmst;
  673. symdeflist:=st.symdeflist;
  674. nopacked:=df_llvm_no_struct_packing in def.defoptions;
  675. if nopacked then
  676. encodedstr:=encodedstr+'{ '
  677. else
  678. encodedstr:=encodedstr+'<{ ';
  679. if symdeflist.count>0 then
  680. begin
  681. i:=0;
  682. if (def.typ=objectdef) and
  683. assigned(tobjectdef(def).childof) and
  684. is_class_or_interface_or_dispinterface(tllvmshadowsymtableentry(symdeflist[0]).def) then
  685. begin
  686. { insert the struct for the class rather than a pointer to the struct }
  687. if (tllvmshadowsymtableentry(symdeflist[0]).def.typ<>objectdef) then
  688. internalerror(2008070601);
  689. llvmaddencodedtype_intern(tllvmshadowsymtableentry(symdeflist[0]).def,[lef_inaggregate,lef_noimplicitderef],encodedstr);
  690. inc(i);
  691. end;
  692. while i<symdeflist.count do
  693. begin
  694. if i<>0 then
  695. encodedstr:=encodedstr+', ';
  696. llvmaddencodedtype_intern(tllvmshadowsymtableentry(symdeflist[i]).def,[lef_inaggregate],encodedstr);
  697. inc(i);
  698. end;
  699. end;
  700. if nopacked then
  701. encodedstr:=encodedstr+' }'
  702. else
  703. encodedstr:=encodedstr+' }>';
  704. end;
  705. procedure llvmextractvalueextinfo(paradef: tdef; var paralocdef: tdef; out signext: tllvmvalueextension);
  706. begin
  707. { implicit zero/sign extension for ABI compliance? (yes, if the size
  708. of a paraloc is larger than the size of the entire parameter) }
  709. if is_ordinal(paradef) and
  710. is_ordinal(paralocdef) and
  711. (paradef.size<paralocdef.size) then
  712. begin
  713. paralocdef:=paradef;
  714. if is_signed(paradef) then
  715. signext:=lve_signext
  716. else
  717. signext:=lve_zeroext
  718. end
  719. else
  720. signext:=lve_none;
  721. end;
  722. procedure llvmaddencodedparaloctype(hp: tparavarsym; proccalloption: tproccalloption; withparaname, withattributes: boolean; var first: boolean; var encodedstr: TSymStr);
  723. var
  724. para: PCGPara;
  725. paraloc: PCGParaLocation;
  726. side: tcallercallee;
  727. signext: tllvmvalueextension;
  728. usedef: tdef;
  729. firstloc: boolean;
  730. begin
  731. if (proccalloption in cdecl_pocalls) and
  732. is_array_of_const(hp.vardef) then
  733. begin
  734. if not first then
  735. encodedstr:=encodedstr+', '
  736. else
  737. first:=false;
  738. encodedstr:=encodedstr+'...';
  739. exit
  740. end;
  741. if not withparaname then
  742. side:=callerside
  743. else
  744. side:=calleeside;
  745. { don't add parameters that don't take up registers or stack space;
  746. clang doesn't either and some LLVM backends don't support them }
  747. if hp.paraloc[side].isempty then
  748. exit;
  749. para:[email protected][side];
  750. paraloc:=para^.location;
  751. firstloc:=true;
  752. repeat
  753. usedef:=paraloc^.def;
  754. llvmextractvalueextinfo(hp.vardef,usedef,signext);
  755. { implicit zero/sign extension for ABI compliance? }
  756. if not first then
  757. encodedstr:=encodedstr+', ';
  758. if (hp.vardef=llvm_metadatatype) or
  759. not((llvmflag_opaque_ptr in llvmversion_properties[current_settings.llvmversion]) and
  760. ((vo_is_funcret in hp.varoptions) or
  761. paramanager.push_addr_param(hp.varspez,hp.vardef,proccalloption) or
  762. llvmbyvalparaloc(paraloc))) then
  763. llvmaddencodedtype_intern(usedef,[],encodedstr)
  764. else
  765. encodedstr:=encodedstr+'ptr';
  766. { in case signextstr<>'', there should be only one paraloc -> no need
  767. to clear (reason: it means that the paraloc is larger than the
  768. original parameter) }
  769. if withattributes then
  770. encodedstr:=encodedstr+llvmvalueextension2str[signext];
  771. { sret: hidden pointer for structured function result }
  772. if vo_is_funcret in hp.varoptions then
  773. begin
  774. { "sret" is only valid for the first parameter, while in FPC this
  775. can sometimes be second one (self comes before). In general,
  776. this is not a problem: we can just leave out sret, which means
  777. the result will be a bit less well optimised), but it is for
  778. AArch64: there, the sret parameter must be passed in a different
  779. register (-> paranr_result is smaller than paranr_self for that
  780. platform in symconst) }
  781. {$ifdef aarch64}
  782. if not first and
  783. not is_managed_type(hp.vardef) then
  784. internalerror(2015101404);
  785. {$endif aarch64}
  786. if withattributes then
  787. begin
  788. if first
  789. {$ifdef aarch64}
  790. and not is_managed_type(hp.vardef)
  791. {$endif aarch64}
  792. then
  793. encodedstr:=encodedstr+llvmparatypeattr(' sret',hp.vardef,false)+' noalias nocapture'
  794. else
  795. encodedstr:=encodedstr+' noalias nocapture';
  796. end;
  797. end
  798. else if not paramanager.push_addr_param(hp.varspez,hp.vardef,proccalloption) and
  799. llvmbyvalparaloc(paraloc) then
  800. begin
  801. if not (llvmflag_opaque_ptr in llvmversion_properties[current_settings.llvmversion]) then
  802. encodedstr:=encodedstr+'*';
  803. if withattributes then
  804. begin
  805. encodedstr:=encodedstr+llvmparatypeattr(' byval',paraloc^.def,false);
  806. if firstloc and
  807. (para^.alignment<>std_param_align) then
  808. begin
  809. encodedstr:=encodedstr+' align '+tostr(para^.alignment);
  810. end;
  811. end
  812. end
  813. else if withattributes and
  814. paramanager.push_addr_param(hp.varspez,hp.vardef,proccalloption) then
  815. begin
  816. { it's not valid to take the address of a parameter and store it for
  817. use past the end of the function call (since the address can always
  818. be on the stack and become invalid later) }
  819. encodedstr:=encodedstr+' nocapture';
  820. { open array/array of const/variant array may be a valid pointer but empty }
  821. if not is_special_array(hp.vardef) and
  822. { e.g. empty records }
  823. (hp.vardef.size<>0) then
  824. begin
  825. case hp.varspez of
  826. vs_value,
  827. vs_const:
  828. begin
  829. encodedstr:=encodedstr+' readonly dereferenceable('
  830. end;
  831. vs_var,
  832. vs_out:
  833. begin
  834. { while normally these are not nil, it is technically possible
  835. to pass nil via ptrtype(nil)^ }
  836. encodedstr:=encodedstr+' dereferenceable_or_null(';
  837. end;
  838. vs_constref:
  839. begin
  840. encodedstr:=encodedstr+' readonly dereferenceable_or_null(';
  841. end;
  842. else
  843. internalerror(2018120801);
  844. end;
  845. if hp.vardef.typ<>formaldef then
  846. encodedstr:=encodedstr+tostr(hp.vardef.size)+')'
  847. else
  848. encodedstr:=encodedstr+'1)';
  849. end;
  850. end;
  851. if withparaname then
  852. begin
  853. if paraloc^.llvmloc.loc<>LOC_REFERENCE then
  854. internalerror(2014010803);
  855. encodedstr:=encodedstr+' '+llvmasmsymname(paraloc^.llvmloc.sym);
  856. end;
  857. paraloc:=paraloc^.next;
  858. firstloc:=false;
  859. first:=false;
  860. until not assigned(paraloc);
  861. end;
  862. function llvmencodeproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype): TSymStr;
  863. begin
  864. result:='';
  865. llvmaddencodedproctype(def,customname,pddecltype,result);
  866. end;
  867. procedure llvmaddencodedproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype; var encodedstr: TSymStr);
  868. var
  869. callingconv: ansistring;
  870. usedef: tdef;
  871. paranr: longint;
  872. hp: tparavarsym;
  873. signext: tllvmvalueextension;
  874. useside: tcallercallee;
  875. first: boolean;
  876. begin
  877. if not(pddecltype in [lpd_alias,lpd_procvar]) then
  878. begin
  879. callingconv:=llvm_callingconvention_name(def.proccalloption);
  880. if callingconv<>'' then
  881. encodedstr:=encodedstr+' '+callingconv;
  882. end;
  883. { when writing a definition, we have to write the parameter names, and
  884. those are only available on the callee side. In all other cases,
  885. we are at the callerside }
  886. if pddecltype=lpd_def then
  887. useside:=calleeside
  888. else
  889. useside:=callerside;
  890. def.init_paraloc_info(useside);
  891. first:=true;
  892. { function result (return-by-ref is handled explicitly) }
  893. if not paramanager.ret_in_param(def.returndef,def) or
  894. def.generate_safecall_wrapper then
  895. begin
  896. if not def.generate_safecall_wrapper then
  897. usedef:=llvmgetcgparadef(def.funcretloc[useside],false,useside)
  898. else
  899. usedef:=ossinttype;
  900. llvmextractvalueextinfo(def.returndef,usedef,signext);
  901. { specifying result sign extention information for an alias causes
  902. an error for some reason }
  903. if pddecltype in [lpd_decl,lpd_def] then
  904. encodedstr:=encodedstr+llvmvalueextension2str[signext];
  905. encodedstr:=encodedstr+' ';
  906. llvmaddencodedtype_intern(usedef,[],encodedstr);
  907. end
  908. else
  909. begin
  910. encodedstr:=encodedstr+' ';
  911. llvmaddencodedtype(voidtype,false,encodedstr);
  912. end;
  913. encodedstr:=encodedstr+' ';
  914. { add procname? }
  915. if (pddecltype in [lpd_decl,lpd_def]) and
  916. (def.typ=procdef) then
  917. if customname='' then
  918. encodedstr:=encodedstr+llvmmangledname(tprocdef(def).mangledname)
  919. else
  920. encodedstr:=encodedstr+llvmmangledname(customname);
  921. encodedstr:=encodedstr+'(';
  922. { parameters }
  923. first:=true;
  924. for paranr:=0 to def.paras.count-1 do
  925. begin
  926. hp:=tparavarsym(def.paras[paranr]);
  927. llvmaddencodedparaloctype(hp,def.proccalloption,pddecltype in [lpd_def],not(pddecltype in [lpd_procvar,lpd_alias]),first,encodedstr);
  928. end;
  929. if po_varargs in def.procoptions then
  930. begin
  931. if not first then
  932. encodedstr:=encodedstr+', ';
  933. encodedstr:=encodedstr+'...';
  934. end;
  935. encodedstr:=encodedstr+')'
  936. end;
  937. function llvmgettemprecorddef(const fieldtypes: array of tdef; packrecords, recordalignmin: shortint): trecorddef;
  938. procedure addtypename(var typename: TSymStr; hdef: tdef);
  939. begin
  940. case hdef.typ of
  941. orddef:
  942. case torddef(hdef).ordtype of
  943. s8bit,
  944. u8bit,
  945. pasbool1,
  946. pasbool8:
  947. typename:=typename+'i8';
  948. s16bit,
  949. u16bit:
  950. typename:=typename+'i16';
  951. s32bit,
  952. u32bit:
  953. typename:=typename+'i32';
  954. s64bit,
  955. u64bit:
  956. typename:=typename+'i64';
  957. customint:
  958. typename:=typename+'i'+tostr(torddef(hdef).packedbitsize);
  959. else
  960. { other types should not appear currently, add as needed }
  961. internalerror(2014012001);
  962. end;
  963. floatdef:
  964. case tfloatdef(hdef).floattype of
  965. s32real:
  966. typename:=typename+'f32';
  967. s64real:
  968. typename:=typename+'f64';
  969. else
  970. { other types should not appear currently, add as needed }
  971. internalerror(2014012008);
  972. end;
  973. arraydef:
  974. begin
  975. if not is_special_array(hdef) and
  976. not is_packed_array(hdef) then
  977. begin
  978. typename:=typename+'['+tostr(tarraydef(hdef).elecount)+'x';
  979. addtypename(typename,tarraydef(hdef).elementdef);
  980. typename:=typename+']';
  981. end
  982. else
  983. typename:=typename+'d'+hdef.unique_id_str;
  984. end
  985. else
  986. typename:=typename+'d'+hdef.unique_id_str;
  987. end;
  988. end;
  989. var
  990. i: longint;
  991. res: PHashSetItem;
  992. oldsymtablestack: tsymtablestack;
  993. hrecst: trecordsymtable;
  994. hrecdef: trecorddef;
  995. sym: tfieldvarsym;
  996. typename: TSymStr;
  997. begin
  998. typename:=internaltypeprefixName[itp_llvmstruct];
  999. for i:=low(fieldtypes) to high(fieldtypes) do
  1000. begin
  1001. addtypename(typename,fieldtypes[i]);
  1002. end;
  1003. if not assigned(current_module) then
  1004. internalerror(2014012002);
  1005. res:=current_module.llvmdefs.FindOrAdd(@typename[1],length(typename));
  1006. if not assigned(res^.Data) then
  1007. begin
  1008. res^.Data:=crecorddef.create_global_internal(typename,packrecords,
  1009. recordalignmin);
  1010. for i:=low(fieldtypes) to high(fieldtypes) do
  1011. trecorddef(res^.Data).add_field_by_def('F'+tostr(i),fieldtypes[i]);
  1012. end;
  1013. trecordsymtable(trecorddef(res^.Data).symtable).addalignmentpadding;
  1014. result:=trecorddef(res^.Data);
  1015. end;
  1016. function llvmgetcgparadef(const cgpara: tcgpara; beforevalueext: boolean; callercallee: tcallercallee): tdef;
  1017. var
  1018. retdeflist: array[0..9] of tdef;
  1019. retloc: pcgparalocation;
  1020. usedef: tdef;
  1021. valueext: tllvmvalueextension;
  1022. paraslots,
  1023. i: longint;
  1024. sizeleft: asizeint;
  1025. begin
  1026. { single location }
  1027. if not assigned(cgpara.location^.next) then
  1028. begin
  1029. { def of the location, except in case of zero/sign-extension and
  1030. zero-sized records }
  1031. if not is_special_array(cgpara.def) and
  1032. (cgpara.def.size=0) then
  1033. usedef:=cgpara.def
  1034. else
  1035. usedef:=cgpara.location^.def;
  1036. if beforevalueext then
  1037. llvmextractvalueextinfo(cgpara.def,usedef,valueext);
  1038. { comp and currency are handled by the x87 in this case. They cannot
  1039. be represented directly in llvm, and llvmdef translates them into
  1040. i64 (since that's their storage size and internally they also are
  1041. int64). Solve this by changing the type to s80real in the
  1042. returndef/parameter declaration. }
  1043. if (usedef.typ=floatdef) and
  1044. (tfloatdef(usedef).floattype in [s64comp,s64currency]) then
  1045. usedef:=s80floattype;
  1046. result:=usedef;
  1047. exit
  1048. end;
  1049. { multiple locations -> create temp record }
  1050. retloc:=cgpara.location;
  1051. i:=0;
  1052. sizeleft:=cgpara.Def.size;
  1053. repeat
  1054. if i>high(retdeflist) then
  1055. internalerror(2016121801);
  1056. if assigned(retloc^.next) then
  1057. begin
  1058. retdeflist[i]:=retloc^.def;
  1059. dec(sizeleft,retloc^.def.size);
  1060. end
  1061. { on the callerside, "byval" parameter locations have the implicit
  1062. pointer in their type -> remove if we wish to create a record
  1063. containing all actual parameter data }
  1064. else if (callercallee=callerside) and
  1065. not retloc^.llvmvalueloc then
  1066. begin
  1067. if retloc^.def.typ<>pointerdef then
  1068. internalerror(2019020201);
  1069. retdeflist[i]:=tpointerdef(retloc^.def).pointeddef
  1070. end
  1071. else if retloc^.def.size<>sizeleft then
  1072. begin
  1073. case sizeleft of
  1074. 1:
  1075. retdeflist[i]:=u8inttype;
  1076. 2:
  1077. retdeflist[i]:=u16inttype;
  1078. 3:
  1079. retdeflist[i]:=u24inttype;
  1080. 4:
  1081. retdeflist[i]:=u32inttype;
  1082. 5:
  1083. retdeflist[i]:=u40inttype;
  1084. 6:
  1085. retdeflist[i]:=u48inttype;
  1086. 7:
  1087. retdeflist[i]:=u56inttype;
  1088. else
  1089. retdeflist[i]:=retloc^.def;
  1090. end
  1091. end
  1092. else
  1093. begin
  1094. if retloc^.def.typ<>floatdef then
  1095. begin
  1096. paraslots:=sizeleft div cgpara.Alignment;
  1097. if (paraslots>1) and
  1098. ((paraslots*cgpara.Alignment)=sizeleft) then
  1099. retdeflist[i]:=carraydef.getreusable(cgsize_orddef(int_cgsize(cgpara.Alignment)),paraslots)
  1100. else
  1101. retdeflist[i]:=retloc^.def;
  1102. end
  1103. else
  1104. retdeflist[i]:=retloc^.def;
  1105. end;
  1106. inc(i);
  1107. retloc:=retloc^.next;
  1108. until not assigned(retloc);
  1109. result:=llvmgettemprecorddef(slice(retdeflist,i),C_alignment,
  1110. targetinfos[target_info.system]^.alignment.recordalignmin);
  1111. include(result.defoptions,df_llvm_no_struct_packing);
  1112. end;
  1113. function llvmencodetypedecl(def: tdef): TSymStr;
  1114. begin
  1115. result:='';
  1116. llvmaddencodedtype_intern(def,[lef_typedecl],result);
  1117. end;
  1118. end.