llvmdef.pas 41 KB

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