export.pas 6.3 KB

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