llvmdef.pas 30 KB

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