llvmdef.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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. 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(fieldtypes: tfplist; packrecords, recordalignmin, maxcrecordalign: 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): 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(fromsize, tosize: tdef): 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. implementation
  90. uses
  91. cutils,constexp,
  92. verbose,systems,
  93. fmodule,
  94. symtable,symconst,symsym,
  95. llvmsym,hlcgobj,
  96. defutil,cgbase,paramgr;
  97. {******************************************************************
  98. Type encoding
  99. *******************************************************************}
  100. function llvmtypeidentifier(def: tdef): TSymStr;
  101. begin
  102. if not assigned(def.typesym) then
  103. internalerror(2015041901);
  104. result:='%"typ.'+def.fullownerhierarchyname+def.typesym.realname+'"'
  105. end;
  106. function llvmaggregatetype(def: tdef): boolean;
  107. begin
  108. result:=
  109. (def.typ in [recorddef,filedef,variantdef]) or
  110. ((def.typ=arraydef) and
  111. not is_dynamic_array(def)) or
  112. ((def.typ=setdef) and
  113. not is_smallset(def)) or
  114. is_shortstring(def) or
  115. is_object(def) or
  116. ((def.typ=procvardef) and
  117. not tprocvardef(def).is_addressonly)
  118. end;
  119. function llvmconvop(fromsize, tosize: tdef): tllvmop;
  120. var
  121. fromregtyp,
  122. toregtyp: tregistertype;
  123. frombytesize,
  124. tobytesize: asizeint;
  125. begin
  126. fromregtyp:=chlcgobj.def2regtyp(fromsize);
  127. toregtyp:=chlcgobj.def2regtyp(tosize);
  128. { int to pointer or vice versa }
  129. if fromregtyp=R_ADDRESSREGISTER then
  130. begin
  131. case toregtyp of
  132. R_INTREGISTER:
  133. result:=la_ptrtoint;
  134. R_ADDRESSREGISTER:
  135. result:=la_bitcast;
  136. else
  137. result:=la_ptrtoint_to_x;
  138. end;
  139. end
  140. else if toregtyp=R_ADDRESSREGISTER then
  141. begin
  142. case fromregtyp of
  143. R_INTREGISTER:
  144. result:=la_inttoptr;
  145. R_ADDRESSREGISTER:
  146. result:=la_bitcast;
  147. else
  148. result:=la_x_to_inttoptr;
  149. end;
  150. end
  151. else
  152. begin
  153. frombytesize:=fromsize.size;
  154. tobytesize:=tosize.size;
  155. { need zero/sign extension, float truncation or plain bitcast? }
  156. if tobytesize<>frombytesize then
  157. begin
  158. case fromregtyp of
  159. R_FPUREGISTER,
  160. R_MMREGISTER:
  161. begin
  162. { todo: update once we support vectors }
  163. if not(toregtyp in [R_FPUREGISTER,R_MMREGISTER]) then
  164. internalerror(2014062203);
  165. if tobytesize<frombytesize then
  166. result:=la_fptrunc
  167. else
  168. result:=la_fpext
  169. end;
  170. else
  171. begin
  172. if tobytesize<frombytesize then
  173. result:=la_trunc
  174. else if is_signed(fromsize) then
  175. { fromsize is signed -> sign extension }
  176. result:=la_sext
  177. else
  178. result:=la_zext;
  179. end;
  180. end;
  181. end
  182. else if (fromsize=llvmbool1type) and
  183. (tosize<>llvmbool1type) then
  184. begin
  185. if is_cbool(tosize) then
  186. result:=la_sext
  187. else
  188. result:=la_zext
  189. end
  190. else if (tosize=llvmbool1type) and
  191. (fromsize<>llvmbool1type) then
  192. begin
  193. { would have to compare with 0, can't just take the lowest bit }
  194. if is_cbool(fromsize) then
  195. internalerror(2016052001)
  196. else
  197. result:=la_trunc
  198. end
  199. else
  200. result:=la_bitcast;
  201. end;
  202. end;
  203. function llvmmangledname(const s: TSymStr): TSymStr;
  204. begin
  205. if copy(s,1,length('llvm.'))<>'llvm.' then
  206. result:='@"\01'+s+'"'
  207. else
  208. result:='@'+s
  209. end;
  210. function llvmasmsymname(const sym: TAsmSymbol): TSymStr;
  211. begin
  212. { AT_ADDR and AT_LABEL represent labels in the code, which have
  213. a different type in llvm compared to (global) data labels }
  214. if sym.bind=AB_TEMP then
  215. result:='%'+sym.name
  216. else if not(sym.typ in [AT_LABEL,AT_ADDR]) then
  217. result:=llvmmangledname(sym.name)
  218. else
  219. result:='label %'+sym.name;
  220. end;
  221. function llvmbyvalparaloc(paraloc: pcgparalocation): boolean;
  222. begin
  223. { "byval" is broken for register paras on several platforms in llvm
  224. (search for "byval" in llvm's bug tracker). Additionally, it should only
  225. be used to pass aggregate parameters on the stack, because it reportedly
  226. inhibits llvm's midlevel optimizers.
  227. Exception (for now?): parameters that have special shifting
  228. requirements, because modelling those in llvm is not easy (and clang
  229. nor llvm-gcc seem to do so either) }
  230. result:=
  231. ((paraloc^.loc=LOC_REFERENCE) and
  232. llvmaggregatetype(paraloc^.def)) or
  233. ((paraloc^.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  234. (paraloc^.shiftval<>0))
  235. end;
  236. procedure llvmaddencodedabstractrecordtype(def: tabstractrecorddef; var encodedstr: TSymStr); forward;
  237. type
  238. tllvmencodeflag = (lef_inaggregate, lef_noimplicitderef, lef_typedecl);
  239. tllvmencodeflags = set of tllvmencodeflag;
  240. procedure llvmaddencodedtype_intern(def: tdef; const flags: tllvmencodeflags; var encodedstr: TSymStr);
  241. var
  242. elesize: asizeint;
  243. begin
  244. case def.typ of
  245. stringdef :
  246. begin
  247. case tstringdef(def).stringtype of
  248. st_widestring,
  249. st_unicodestring:
  250. { the variable does not point to the header, but to a
  251. null-terminated string/array with undefined bounds }
  252. encodedstr:=encodedstr+'i16*';
  253. st_ansistring:
  254. encodedstr:=encodedstr+'i8*';
  255. st_shortstring:
  256. { length byte followed by string bytes }
  257. if tstringdef(def).len>0 then
  258. encodedstr:=encodedstr+'['+tostr(tstringdef(def).len+1)+' x i8]'
  259. else
  260. encodedstr:=encodedstr+'[0 x i8]';
  261. else
  262. internalerror(2013100201);
  263. end;
  264. end;
  265. enumdef:
  266. begin
  267. encodedstr:=encodedstr+'i'+tostr(def.size*8);
  268. end;
  269. orddef :
  270. begin
  271. if is_void(def) then
  272. encodedstr:=encodedstr+'void'
  273. { mainly required because comparison operations return i1, and
  274. we need a way to represent the i1 type in Pascal. We don't
  275. reuse pasbool8type, because putting an i1 in a record or
  276. passing it as a parameter may result in unexpected behaviour }
  277. else if def=llvmbool1type then
  278. encodedstr:=encodedstr+'i1'
  279. else
  280. encodedstr:=encodedstr+'i'+tostr(def.size*8);
  281. end;
  282. pointerdef :
  283. begin
  284. if is_voidpointer(def) then
  285. encodedstr:=encodedstr+'i8*'
  286. else
  287. begin
  288. llvmaddencodedtype_intern(tpointerdef(def).pointeddef,[],encodedstr);
  289. encodedstr:=encodedstr+'*';
  290. end;
  291. end;
  292. floatdef :
  293. begin
  294. case tfloatdef(def).floattype of
  295. s32real:
  296. encodedstr:=encodedstr+'float';
  297. s64real:
  298. encodedstr:=encodedstr+'double';
  299. { necessary to be able to force our own size/alignment }
  300. s80real:
  301. { prevent llvm from allocating the standard ABI size for
  302. extended }
  303. if lef_inaggregate in flags then
  304. encodedstr:=encodedstr+'[10 x i8]'
  305. else
  306. encodedstr:=encodedstr+'x86_fp80';
  307. sc80real:
  308. encodedstr:=encodedstr+'x86_fp80';
  309. s64comp,
  310. s64currency:
  311. encodedstr:=encodedstr+'i64';
  312. s128real:
  313. {$if defined(powerpc) or defined(powerpc128)}
  314. encodedstr:=encodedstr+'ppc_fp128';
  315. {$else}
  316. encodedstr:=encodedstr+'fp128';
  317. {$endif}
  318. else
  319. internalerror(2013100202);
  320. end;
  321. end;
  322. filedef :
  323. begin
  324. case tfiledef(def).filetyp of
  325. ft_text :
  326. llvmaddencodedtype_intern(search_system_type('TEXTREC').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  327. ft_typed,
  328. ft_untyped :
  329. llvmaddencodedtype_intern(search_system_type('FILEREC').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  330. else
  331. internalerror(2013100203);
  332. end;
  333. end;
  334. recorddef :
  335. begin
  336. { avoid endlessly recursive definitions }
  337. if assigned(def.typesym) and
  338. ((lef_inaggregate in flags) or
  339. not(lef_typedecl in flags)) then
  340. encodedstr:=encodedstr+llvmtypeidentifier(def)
  341. else
  342. llvmaddencodedabstractrecordtype(trecorddef(def),encodedstr);
  343. end;
  344. variantdef :
  345. begin
  346. llvmaddencodedtype_intern(search_system_type('TVARDATA').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  347. end;
  348. classrefdef :
  349. begin
  350. if is_class(tclassrefdef(def).pointeddef) then
  351. begin
  352. llvmaddencodedtype_intern(tobjectdef(tclassrefdef(def).pointeddef).vmt_def,flags,encodedstr);
  353. encodedstr:=encodedstr+'*';
  354. end
  355. else if is_objcclass(tclassrefdef(def).pointeddef) then
  356. llvmaddencodedtype_intern(objc_idtype,flags,encodedstr)
  357. else
  358. encodedstr:=encodedstr+'i8*'
  359. end;
  360. setdef :
  361. begin
  362. { just an array as far as llvm is concerned; don't use a "packed
  363. array of i1" or so, this requires special support in backends
  364. and guarantees nothing about the internal format }
  365. if is_smallset(def) then
  366. llvmaddencodedtype_intern(cgsize_orddef(def_cgsize(def)),[lef_inaggregate],encodedstr)
  367. else
  368. encodedstr:=encodedstr+'['+tostr(tsetdef(def).size)+' x i8]';
  369. end;
  370. formaldef :
  371. begin
  372. { var/const/out x (always treated as "pass by reference" -> don't
  373. add extra "*" here) }
  374. encodedstr:=encodedstr+'i8';
  375. end;
  376. arraydef :
  377. begin
  378. if is_array_of_const(def) then
  379. begin
  380. encodedstr:=encodedstr+'[0 x ';
  381. llvmaddencodedtype_intern(search_system_type('TVARREC').typedef,[lef_inaggregate],encodedstr);
  382. encodedstr:=encodedstr+']';
  383. end
  384. else if is_open_array(def) then
  385. begin
  386. encodedstr:=encodedstr+'[0 x ';
  387. llvmaddencodedtype_intern(tarraydef(def).elementdef,[lef_inaggregate],encodedstr);
  388. encodedstr:=encodedstr+']';
  389. end
  390. else if is_dynamic_array(def) then
  391. begin
  392. llvmaddencodedtype_intern(tarraydef(def).elementdef,[],encodedstr);
  393. encodedstr:=encodedstr+'*';
  394. end
  395. else if is_packed_array(def) and
  396. (tarraydef(def).elementdef.typ in [enumdef,orddef]) then
  397. begin
  398. elesize:=packedbitsloadsize(tarraydef(def).elementdef.packedbitsize);
  399. encodedstr:=encodedstr+'['+tostr(tarraydef(def).size div elesize)+' x ';
  400. { encode as an array of integers with the size on which we
  401. perform the packedbits operations }
  402. llvmaddencodedtype_intern(cgsize_orddef(int_cgsize(elesize)),[lef_inaggregate],encodedstr);
  403. encodedstr:=encodedstr+']';
  404. end
  405. else
  406. begin
  407. encodedstr:=encodedstr+'['+tostr(tarraydef(def).elecount)+' x ';
  408. llvmaddencodedtype_intern(tarraydef(def).elementdef,[lef_inaggregate],encodedstr);
  409. encodedstr:=encodedstr+']';
  410. end;
  411. end;
  412. procdef,
  413. procvardef :
  414. begin
  415. if (def.typ=procdef) or
  416. tprocvardef(def).is_addressonly then
  417. begin
  418. llvmaddencodedproctype(tabstractprocdef(def),'',lpd_procvar,encodedstr);
  419. if def.typ=procvardef then
  420. encodedstr:=encodedstr+'*';
  421. end
  422. else if ((lef_inaggregate in flags) or
  423. not(lef_typedecl in flags)) and
  424. assigned(tprocvardef(def).typesym) then
  425. begin
  426. { in case the procvardef recursively references itself, e.g.
  427. via a pointer }
  428. encodedstr:=encodedstr+llvmtypeidentifier(def)
  429. end
  430. else
  431. begin
  432. encodedstr:=encodedstr+'<{';
  433. { code pointer }
  434. llvmaddencodedproctype(tabstractprocdef(def),'',lpd_procvar,encodedstr);
  435. { data pointer (maybe todo: generate actual layout if
  436. available) }
  437. encodedstr:=encodedstr+'*, i8*}>';
  438. end;
  439. end;
  440. objectdef :
  441. case tobjectdef(def).objecttype of
  442. odt_class,
  443. odt_objcclass,
  444. odt_object,
  445. odt_cppclass:
  446. begin
  447. if not(lef_typedecl in flags) and
  448. assigned(def.typesym) then
  449. encodedstr:=encodedstr+llvmtypeidentifier(def)
  450. else
  451. llvmaddencodedabstractrecordtype(tabstractrecorddef(def),encodedstr);
  452. if ([lef_typedecl,lef_noimplicitderef]*flags=[]) and
  453. is_implicit_pointer_object_type(def) then
  454. encodedstr:=encodedstr+'*'
  455. end;
  456. odt_interfacecom,
  457. odt_interfacecorba,
  458. odt_dispinterface:
  459. begin
  460. { type is a pointer to a pointer to the vmt }
  461. llvmaddencodedtype_intern(tobjectdef(def).vmt_def,flags,encodedstr);
  462. if ([lef_typedecl,lef_noimplicitderef]*flags=[]) then
  463. encodedstr:=encodedstr+'**';
  464. end;
  465. odt_interfacecom_function,
  466. odt_interfacecom_property,
  467. odt_objcprotocol:
  468. begin
  469. { opaque for now }
  470. encodedstr:=encodedstr+'i8*'
  471. end;
  472. else
  473. internalerror(2013100601);
  474. end;
  475. undefineddef,
  476. errordef :
  477. internalerror(2013100604);
  478. else
  479. internalerror(2013100603);
  480. end;
  481. end;
  482. function llvmencodetypename(def: tdef): TSymStr;
  483. begin
  484. result:='';
  485. llvmaddencodedtype_intern(def,[],result);
  486. end;
  487. procedure llvmaddencodedtype(def: tdef; inaggregate: boolean; var encodedstr: TSymStr);
  488. var
  489. flags: tllvmencodeflags;
  490. begin
  491. if inaggregate then
  492. flags:=[lef_inaggregate]
  493. else
  494. flags:=[];
  495. llvmaddencodedtype_intern(def,flags,encodedstr);
  496. end;
  497. procedure llvmaddencodedabstractrecordtype(def: tabstractrecorddef; var encodedstr: TSymStr);
  498. var
  499. st: tllvmshadowsymtable;
  500. symdeflist: tfpobjectlist;
  501. i: longint;
  502. nopacked: boolean;
  503. begin
  504. st:=tabstractrecordsymtable(def.symtable).llvmst;
  505. symdeflist:=st.symdeflist;
  506. nopacked:=df_llvm_no_struct_packing in def.defoptions;
  507. if nopacked then
  508. encodedstr:=encodedstr+'{ '
  509. else
  510. encodedstr:=encodedstr+'<{ ';
  511. if symdeflist.count>0 then
  512. begin
  513. i:=0;
  514. if (def.typ=objectdef) and
  515. assigned(tobjectdef(def).childof) and
  516. is_class_or_interface_or_dispinterface(tllvmshadowsymtableentry(symdeflist[0]).def) then
  517. begin
  518. { insert the struct for the class rather than a pointer to the struct }
  519. if (tllvmshadowsymtableentry(symdeflist[0]).def.typ<>objectdef) then
  520. internalerror(2008070601);
  521. llvmaddencodedtype_intern(tllvmshadowsymtableentry(symdeflist[0]).def,[lef_inaggregate,lef_noimplicitderef],encodedstr);
  522. inc(i);
  523. end;
  524. while i<symdeflist.count do
  525. begin
  526. if i<>0 then
  527. encodedstr:=encodedstr+', ';
  528. llvmaddencodedtype_intern(tllvmshadowsymtableentry(symdeflist[i]).def,[lef_inaggregate],encodedstr);
  529. inc(i);
  530. end;
  531. end;
  532. if nopacked then
  533. encodedstr:=encodedstr+' }'
  534. else
  535. encodedstr:=encodedstr+' }>';
  536. end;
  537. procedure llvmextractvalueextinfo(paradef: tdef; var paralocdef: tdef; out signext: tllvmvalueextension);
  538. begin
  539. { implicit zero/sign extension for ABI compliance? (yes, if the size
  540. of a paraloc is larger than the size of the entire parameter) }
  541. if is_ordinal(paradef) and
  542. is_ordinal(paralocdef) and
  543. (paradef.size<paralocdef.size) then
  544. begin
  545. paralocdef:=paradef;
  546. if is_signed(paradef) then
  547. signext:=lve_signext
  548. else
  549. signext:=lve_zeroext
  550. end
  551. else
  552. signext:=lve_none;
  553. end;
  554. procedure llvmaddencodedparaloctype(hp: tparavarsym; proccalloption: tproccalloption; withparaname, withattributes: boolean; var first: boolean; var encodedstr: TSymStr);
  555. var
  556. paraloc: PCGParaLocation;
  557. signext: tllvmvalueextension;
  558. usedef: tdef;
  559. begin
  560. if (proccalloption in cdecl_pocalls) and
  561. is_array_of_const(hp.vardef) then
  562. begin
  563. if not first then
  564. encodedstr:=encodedstr+', '
  565. else
  566. first:=false;
  567. encodedstr:=encodedstr+'...';
  568. exit
  569. end;
  570. if withparaname then
  571. paraloc:=hp.paraloc[calleeside].location
  572. else
  573. paraloc:=hp.paraloc[callerside].location;
  574. repeat
  575. usedef:=paraloc^.def;
  576. llvmextractvalueextinfo(hp.vardef,usedef,signext);
  577. { implicit zero/sign extension for ABI compliance? }
  578. if not first then
  579. encodedstr:=encodedstr+', '
  580. else
  581. first:=false;
  582. llvmaddencodedtype_intern(usedef,[],encodedstr);
  583. { in case signextstr<>'', there should be only one paraloc -> no need
  584. to clear (reason: it means that the paraloc is larger than the
  585. original parameter) }
  586. if withattributes then
  587. encodedstr:=encodedstr+llvmvalueextension2str[signext];
  588. { sret: hidden pointer for structured function result }
  589. if vo_is_funcret in hp.varoptions then
  590. begin
  591. { "sret" is only valid for the firstparameter, while in FPC this
  592. can sometimes be second one (self comes before). In general,
  593. this is not a problem: we can just leave out sret, which means
  594. the result will be a bit less well optimised), but it is for
  595. AArch64: there, the sret parameter must be passed in a different
  596. register (-> paranr_result is smaller than paranr_self for that
  597. platform in symconst) }
  598. {$ifdef aarch64}
  599. if not first then
  600. internalerror(2015101404);
  601. {$endif aarch64}
  602. if withattributes then
  603. if first then
  604. encodedstr:=encodedstr+' sret'
  605. else { we can add some other attributes to optimise things,}
  606. encodedstr:=encodedstr+' noalias nocapture';
  607. end
  608. else if not paramanager.push_addr_param(hp.varspez,hp.vardef,proccalloption) and
  609. llvmbyvalparaloc(paraloc) then
  610. begin
  611. if withattributes then
  612. encodedstr:=encodedstr+'* byval'
  613. else
  614. encodedstr:=encodedstr+'*';
  615. end;
  616. if withparaname then
  617. begin
  618. if paraloc^.llvmloc.loc<>LOC_REFERENCE then
  619. internalerror(2014010803);
  620. encodedstr:=encodedstr+' '+llvmasmsymname(paraloc^.llvmloc.sym);
  621. end;
  622. paraloc:=paraloc^.next;
  623. until not assigned(paraloc);
  624. end;
  625. function llvmencodeproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype): TSymStr;
  626. begin
  627. result:='';
  628. llvmaddencodedproctype(def,customname,pddecltype,result);
  629. end;
  630. procedure llvmaddencodedproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype; var encodedstr: TSymStr);
  631. var
  632. usedef: tdef;
  633. paranr: longint;
  634. hp: tparavarsym;
  635. signext: tllvmvalueextension;
  636. useside: tcallercallee;
  637. first: boolean;
  638. begin
  639. { when writing a definition, we have to write the parameter names, and
  640. those are only available on the callee side. In all other cases,
  641. we are at the callerside }
  642. if pddecltype=lpd_def then
  643. useside:=calleeside
  644. else
  645. useside:=callerside;
  646. def.init_paraloc_info(useside);
  647. first:=true;
  648. { function result (return-by-ref is handled explicitly) }
  649. if not paramanager.ret_in_param(def.returndef,def) then
  650. begin
  651. usedef:=llvmgetcgparadef(def.funcretloc[useside],false);
  652. llvmextractvalueextinfo(def.returndef,usedef,signext);
  653. { specifying result sign extention information for an alias causes
  654. an error for some reason }
  655. if pddecltype in [lpd_decl,lpd_def] then
  656. encodedstr:=encodedstr+llvmvalueextension2str[signext];
  657. encodedstr:=encodedstr+' ';
  658. llvmaddencodedtype_intern(usedef,[],encodedstr);
  659. end
  660. else
  661. begin
  662. encodedstr:=encodedstr+' ';
  663. llvmaddencodedtype(voidtype,false,encodedstr);
  664. end;
  665. encodedstr:=encodedstr+' ';
  666. { add procname? }
  667. if (pddecltype in [lpd_decl,lpd_def]) and
  668. (def.typ=procdef) then
  669. if customname='' then
  670. encodedstr:=encodedstr+llvmmangledname(tprocdef(def).mangledname)
  671. else
  672. encodedstr:=encodedstr+llvmmangledname(customname);
  673. encodedstr:=encodedstr+'(';
  674. { parameters }
  675. first:=true;
  676. for paranr:=0 to def.paras.count-1 do
  677. begin
  678. hp:=tparavarsym(def.paras[paranr]);
  679. llvmaddencodedparaloctype(hp,def.proccalloption,pddecltype in [lpd_def],not(pddecltype in [lpd_procvar,lpd_alias]),first,encodedstr);
  680. end;
  681. if po_varargs in def.procoptions then
  682. begin
  683. if not first then
  684. encodedstr:=encodedstr+', ';
  685. encodedstr:=encodedstr+'...';
  686. end;
  687. encodedstr:=encodedstr+')'
  688. end;
  689. function llvmgettemprecorddef(fieldtypes: tfplist; packrecords, recordalignmin, maxcrecordalign: shortint): trecorddef;
  690. var
  691. i: longint;
  692. res: PHashSetItem;
  693. oldsymtablestack: tsymtablestack;
  694. hrecst: trecordsymtable;
  695. hdef: tdef;
  696. hrecdef: trecorddef;
  697. sym: tfieldvarsym;
  698. typename: string;
  699. begin
  700. typename:=internaltypeprefixName[itp_llvmstruct];
  701. for i:=0 to fieldtypes.count-1 do
  702. begin
  703. hdef:=tdef(fieldtypes[i]);
  704. case hdef.typ of
  705. orddef:
  706. case torddef(hdef).ordtype of
  707. s8bit,
  708. u8bit:
  709. typename:=typename+'i8';
  710. s16bit,
  711. u16bit:
  712. typename:=typename+'i16';
  713. s32bit,
  714. u32bit:
  715. typename:=typename+'i32';
  716. s64bit,
  717. u64bit:
  718. typename:=typename+'i64';
  719. else
  720. { other types should not appear currently, add as needed }
  721. internalerror(2014012001);
  722. end;
  723. floatdef:
  724. case tfloatdef(hdef).floattype of
  725. s32real:
  726. typename:=typename+'f32';
  727. s64real:
  728. typename:=typename+'f64';
  729. else
  730. { other types should not appear currently, add as needed }
  731. internalerror(2014012008);
  732. end;
  733. pointerdef:
  734. typename:='p'+tostr(hdef.deflist_index);
  735. else
  736. { other types should not appear currently, add as needed }
  737. internalerror(2014012009);
  738. end;
  739. end;
  740. if not assigned(current_module) then
  741. internalerror(2014012002);
  742. res:=current_module.llvmdefs.FindOrAdd(@typename[1],length(typename));
  743. if not assigned(res^.Data) then
  744. begin
  745. res^.Data:=crecorddef.create_global_internal(typename,packrecords,
  746. recordalignmin,maxcrecordalign);
  747. for i:=0 to fieldtypes.count-1 do
  748. trecorddef(res^.Data).add_field_by_def('F'+tostr(i),tdef(fieldtypes[i]));
  749. end;
  750. trecordsymtable(trecorddef(res^.Data).symtable).addalignmentpadding;
  751. result:=trecorddef(res^.Data);
  752. end;
  753. function llvmgetcgparadef(const cgpara: tcgpara; beforevalueext: boolean): tdef;
  754. var
  755. retdeflist: tfplist;
  756. retloc: pcgparalocation;
  757. usedef: tdef;
  758. valueext: tllvmvalueextension;
  759. begin
  760. { single location }
  761. if not assigned(cgpara.location^.next) then
  762. begin
  763. { def of the location, except in case of zero/sign-extension }
  764. usedef:=cgpara.location^.def;
  765. if beforevalueext then
  766. llvmextractvalueextinfo(cgpara.def,usedef,valueext);
  767. { comp and currency are handled by the x87 in this case. They cannot
  768. be represented directly in llvm, and llvmdef translates them into
  769. i64 (since that's their storage size and internally they also are
  770. int64). Solve this by changing the type to s80real in the
  771. returndef/parameter declaration. }
  772. if (usedef.typ=floatdef) and
  773. (tfloatdef(usedef).floattype in [s64comp,s64currency]) then
  774. usedef:=s80floattype;
  775. result:=usedef;
  776. exit
  777. end;
  778. { multiple locations -> create temp record }
  779. retdeflist:=tfplist.create;
  780. retloc:=cgpara.location;
  781. repeat
  782. retdeflist.add(retloc^.def);
  783. retloc:=retloc^.next;
  784. until not assigned(retloc);
  785. result:=llvmgettemprecorddef(retdeflist,C_alignment,
  786. targetinfos[target_info.system]^.alignment.recordalignmin,
  787. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  788. include(result.defoptions,df_llvm_no_struct_packing);
  789. end;
  790. function llvmencodetypedecl(def: tdef): TSymStr;
  791. begin
  792. result:='';
  793. llvmaddencodedtype_intern(def,[lef_typedecl],result);
  794. end;
  795. end.