export.pas 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements an uniform export object
  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. unit export;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cutils,cclasses,
  22. systems,
  23. symtype,symdef,symsym,
  24. aasmbase,aasmdata;
  25. type
  26. { export options }
  27. texportoption=(eo_none,
  28. eo_resident,
  29. eo_index,
  30. eo_name
  31. );
  32. texportoptions=set of texportoption;
  33. texported_item = class(TLinkedListItem)
  34. sym : tsym;
  35. index : longint;
  36. name : pshortstring;
  37. options : texportoptions;
  38. is_var : boolean;
  39. constructor create;
  40. destructor destroy;override;
  41. end;
  42. texportlib=class
  43. private
  44. notsupmsg : boolean;
  45. finitname,
  46. ffininame : string;
  47. procedure NotSupported;
  48. public
  49. constructor Create;virtual;
  50. destructor Destroy;override;
  51. procedure preparelib(const s : string);virtual;
  52. procedure exportprocedure(hp : texported_item);virtual;
  53. procedure exportvar(hp : texported_item);virtual;
  54. procedure generatelib;virtual;
  55. procedure setinitname(list: TAsmList; const s: string); virtual;
  56. procedure setfininame(list: TAsmList; const s: string); virtual;
  57. property initname: string read finitname;
  58. property fininame: string read ffininame;
  59. end;
  60. TExportLibClass=class of TExportLib;
  61. procedure exportprocsym(sym: tsym; const s : string; index: longint; options: texportoptions);
  62. procedure exportvarsym(sym: tsym; const s : string; index: longint; options: texportoptions);
  63. { to export symbols not directly related to a tsym (e.g., the Objective-C
  64. rtti) }
  65. procedure exportname(const s : string; options: texportoptions);
  66. procedure exportallprocdefnames(sym: tprocsym; pd: tprocdef; options: texportoptions);
  67. procedure exportallprocsymnames(ps: tprocsym; options: texportoptions);
  68. var
  69. CExportLib : array[tsystem] of TExportLibClass;
  70. ExportLib : TExportLib;
  71. procedure RegisterExport(t:tsystem;c:TExportLibClass);
  72. procedure InitExport;
  73. procedure DoneExport;
  74. implementation
  75. uses
  76. verbose,globals;
  77. {****************************************************************************
  78. TExported_procedure
  79. ****************************************************************************}
  80. procedure exportprocsym(sym: tsym; const s : string; index: longint; options: texportoptions);
  81. var
  82. hp : texported_item;
  83. begin
  84. hp:=texported_item.create;
  85. hp.name:=stringdup(s);
  86. hp.sym:=sym;
  87. hp.options:=options+[eo_name];
  88. hp.index:=index;
  89. exportlib.exportprocedure(hp);
  90. end;
  91. procedure exportvarsym(sym: tsym; const s : string; index: longint; options: texportoptions);
  92. var
  93. hp : texported_item;
  94. begin
  95. hp:=texported_item.create;
  96. hp.name:=stringdup(s);
  97. hp.sym:=sym;
  98. hp.is_var:=true;
  99. hp.options:=options+[eo_name];
  100. hp.index:=index;
  101. exportlib.exportvar(hp);
  102. end;
  103. procedure exportname(const s : string; options: texportoptions);
  104. begin
  105. exportvarsym(nil,s,0,options);
  106. end;
  107. procedure exportallprocdefnames(sym: tprocsym; pd: tprocdef; options: texportoptions);
  108. var
  109. item: TCmdStrListItem;
  110. begin
  111. exportprocsym(sym,pd.mangledname,0,options);
  112. { walk through all aliases }
  113. item:=TCmdStrListItem(pd.aliasnames.first);
  114. while assigned(item) do
  115. begin
  116. { avoid duplicate entries, sometimes aliasnames contains the mangledname }
  117. if item.str<>pd.mangledname then
  118. exportprocsym(sym,item.str,0,options);
  119. item:=TCmdStrListItem(item.next);
  120. end;
  121. end;
  122. procedure exportallprocsymnames(ps: tprocsym; options: texportoptions);
  123. var
  124. i: longint;
  125. begin
  126. for i:= 0 to ps.ProcdefList.Count-1 do
  127. exportallprocdefnames(ps,tprocdef(ps.ProcdefList[i]),options);
  128. end;
  129. {****************************************************************************
  130. TExported_procedure
  131. ****************************************************************************}
  132. constructor texported_item.Create;
  133. begin
  134. inherited Create;
  135. sym:=nil;
  136. index:=-1;
  137. name:=nil;
  138. options:=[];
  139. is_var:=false;
  140. end;
  141. destructor texported_item.destroy;
  142. begin
  143. stringdispose(name);
  144. inherited destroy;
  145. end;
  146. {****************************************************************************
  147. TExportLib
  148. ****************************************************************************}
  149. constructor texportlib.Create;
  150. begin
  151. notsupmsg:=false;
  152. end;
  153. destructor texportlib.Destroy;
  154. begin
  155. end;
  156. procedure texportlib.NotSupported;
  157. begin
  158. { show the message only once }
  159. if not notsupmsg then
  160. begin
  161. Message(exec_e_dll_not_supported);
  162. notsupmsg:=true;
  163. end;
  164. end;
  165. procedure texportlib.preparelib(const s:string);
  166. begin
  167. NotSupported;
  168. end;
  169. procedure texportlib.exportprocedure(hp : texported_item);
  170. begin
  171. NotSupported;
  172. end;
  173. procedure texportlib.exportvar(hp : texported_item);
  174. begin
  175. NotSupported;
  176. end;
  177. procedure texportlib.generatelib;
  178. begin
  179. NotSupported;
  180. end;
  181. procedure texportlib.setinitname(list: TAsmList; const s: string);
  182. begin
  183. finitname:=s;
  184. end;
  185. procedure texportlib.setfininame(list: TAsmList; const s: string);
  186. begin
  187. ffininame:=s;
  188. end;
  189. {*****************************************************************************
  190. Init/Done
  191. *****************************************************************************}
  192. procedure RegisterExport(t:tsystem;c:TExportLibClass);
  193. begin
  194. CExportLib[t]:=c;
  195. end;
  196. procedure InitExport;
  197. begin
  198. if assigned(CExportLib[target_info.system]) then
  199. exportlib:=CExportLib[target_info.system].Create
  200. else
  201. exportlib:=TExportLib.Create;
  202. end;
  203. procedure DoneExport;
  204. begin
  205. if assigned(Exportlib) then
  206. Exportlib.free;
  207. end;
  208. end.