2
0

export.pas 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. procedure exportallprocdefnames(sym: tprocsym; pd: tprocdef; options: word);
  62. procedure exportallprocsymnames(ps: tprocsym; options: word);
  63. var
  64. CExportLib : array[tsystem] of TExportLibClass;
  65. ExportLib : TExportLib;
  66. procedure RegisterExport(t:tsystem;c:TExportLibClass);
  67. procedure InitExport;
  68. procedure DoneExport;
  69. implementation
  70. uses
  71. verbose,globals;
  72. {****************************************************************************
  73. TExported_procedure
  74. ****************************************************************************}
  75. procedure exportprocsym(sym: tsym; const s : string; index: longint; options: word);
  76. var
  77. hp : texported_item;
  78. begin
  79. hp:=texported_item.create;
  80. hp.name:=stringdup(s);
  81. hp.sym:=sym;
  82. hp.options:=options or eo_name;
  83. hp.index:=index;
  84. exportlib.exportprocedure(hp);
  85. end;
  86. procedure exportvarsym(sym: tsym; const s : string; index: longint; options: word);
  87. var
  88. hp : texported_item;
  89. begin
  90. hp:=texported_item.create;
  91. hp.name:=stringdup(s);
  92. hp.sym:=sym;
  93. hp.is_var:=true;
  94. hp.options:=options or eo_name;
  95. hp.index:=index;
  96. exportlib.exportvar(hp);
  97. end;
  98. procedure exportallprocdefnames(sym: tprocsym; pd: tprocdef; options: word);
  99. var
  100. item: TCmdStrListItem;
  101. begin
  102. exportprocsym(sym,pd.mangledname,0,options);
  103. { walk through all aliases }
  104. item:=TCmdStrListItem(pd.aliasnames.first);
  105. while assigned(item) do
  106. begin
  107. { avoid duplicate entries, sometimes aliasnames contains the mangledname }
  108. if item.str<>pd.mangledname then
  109. exportprocsym(sym,item.str,0,options);
  110. item:=TCmdStrListItem(item.next);
  111. end;
  112. end;
  113. procedure exportallprocsymnames(ps: tprocsym; options: word);
  114. var
  115. i: longint;
  116. begin
  117. for i:= 0 to ps.ProcdefList.Count-1 do
  118. exportallprocdefnames(ps,tprocdef(ps.ProcdefList[i]),options);
  119. end;
  120. {****************************************************************************
  121. TExported_procedure
  122. ****************************************************************************}
  123. constructor texported_item.Create;
  124. begin
  125. inherited Create;
  126. sym:=nil;
  127. index:=-1;
  128. name:=nil;
  129. options:=0;
  130. is_var:=false;
  131. end;
  132. destructor texported_item.destroy;
  133. begin
  134. stringdispose(name);
  135. inherited destroy;
  136. end;
  137. {****************************************************************************
  138. TExportLib
  139. ****************************************************************************}
  140. constructor texportlib.Create;
  141. begin
  142. notsupmsg:=false;
  143. end;
  144. destructor texportlib.Destroy;
  145. begin
  146. end;
  147. procedure texportlib.NotSupported;
  148. begin
  149. { show the message only once }
  150. if not notsupmsg then
  151. begin
  152. Message(exec_e_dll_not_supported);
  153. notsupmsg:=true;
  154. end;
  155. end;
  156. procedure texportlib.preparelib(const s:string);
  157. begin
  158. NotSupported;
  159. end;
  160. procedure texportlib.exportprocedure(hp : texported_item);
  161. begin
  162. NotSupported;
  163. end;
  164. procedure texportlib.exportvar(hp : texported_item);
  165. begin
  166. NotSupported;
  167. end;
  168. procedure texportlib.generatelib;
  169. begin
  170. NotSupported;
  171. end;
  172. procedure texportlib.setinitname(list: TAsmList; const s: string);
  173. begin
  174. finitname:=s;
  175. end;
  176. procedure texportlib.setfininame(list: TAsmList; const s: string);
  177. begin
  178. ffininame:=s;
  179. end;
  180. {*****************************************************************************
  181. Init/Done
  182. *****************************************************************************}
  183. procedure RegisterExport(t:tsystem;c:TExportLibClass);
  184. begin
  185. CExportLib[t]:=c;
  186. end;
  187. procedure InitExport;
  188. begin
  189. if assigned(CExportLib[target_info.system]) then
  190. exportlib:=CExportLib[target_info.system].Create
  191. else
  192. exportlib:=TExportLib.Create;
  193. end;
  194. procedure DoneExport;
  195. begin
  196. if assigned(Exportlib) then
  197. Exportlib.free;
  198. end;
  199. end.