expunix.pas 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. {
  2. Copyright (c) 2008 by the Free Pascal Compiler team
  3. This unit implements common support for import,export,link routines
  4. for unix target
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit expunix;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cutils,cclasses,
  23. systems,
  24. export,
  25. symtype,symdef,symsym,
  26. aasmbase;
  27. type
  28. texportlibunix=class(texportlib)
  29. private
  30. fexportedsymnames: TCmdStrList;
  31. public
  32. constructor Create; override;
  33. destructor destroy; override;
  34. procedure preparelib(const s : string);override;
  35. procedure exportprocedure(hp : texported_item);override;
  36. procedure exportvar(hp : texported_item);override;
  37. procedure generatelib;override;
  38. property exportedsymnames: TCmdStrList read fexportedsymnames;
  39. end;
  40. implementation
  41. {****************************************************************************
  42. TExportLibUnix
  43. ****************************************************************************}
  44. uses
  45. symconst,
  46. globtype,globals,
  47. aasmdata,aasmtai,aasmcpu,
  48. fmodule,
  49. cgbase,cgutils,cpubase,cgobj,
  50. cgcpu,hlcgobj,hlcgcpu,
  51. {$ifdef llvm}
  52. hlcgllvm,
  53. {$endif llvm}
  54. ncgutil,
  55. verbose;
  56. constructor texportlibunix.create;
  57. begin
  58. inherited create;
  59. fexportedsymnames:=tcmdstrlist.create_no_double;
  60. end;
  61. destructor texportlibunix.destroy;
  62. begin
  63. fexportedsymnames.free;
  64. inherited destroy;
  65. end;
  66. procedure texportlibunix.preparelib(const s:string);
  67. begin
  68. end;
  69. procedure texportlibunix.exportprocedure(hp : texported_item);
  70. var
  71. hp2 : texported_item;
  72. begin
  73. { first test the index value }
  74. if eo_index in hp.options then
  75. begin
  76. Message1(parser_e_no_export_with_index_for_target,target_info.shortname);
  77. exit;
  78. end;
  79. { now place in correct order }
  80. hp2:=texported_item(current_module._exports.first);
  81. while assigned(hp2) and
  82. (hp.name^>hp2.name^) do
  83. hp2:=texported_item(hp2.next);
  84. { insert hp there !! }
  85. if assigned(hp2) and (hp2.name^=hp.name^) then
  86. begin
  87. { this is not allowed !! }
  88. duplicatesymbol(hp.name^);
  89. exit;
  90. end;
  91. if hp2=texported_item(current_module._exports.first) then
  92. current_module._exports.concat(hp)
  93. else if assigned(hp2) then
  94. begin
  95. hp.next:=hp2;
  96. hp.previous:=hp2.previous;
  97. if assigned(hp2.previous) then
  98. hp2.previous.next:=hp;
  99. hp2.previous:=hp;
  100. end
  101. else
  102. current_module._exports.concat(hp);
  103. end;
  104. procedure texportlibunix.exportvar(hp : texported_item);
  105. begin
  106. hp.is_var:=true;
  107. exportprocedure(hp);
  108. end;
  109. procedure texportlibunix.generatelib; // straight t_linux copy for now.
  110. var
  111. hp2 : texported_item;
  112. pd : tprocdef;
  113. anyhasalias : boolean;
  114. i : longint;
  115. begin
  116. pd:=nil;
  117. create_hlcodegen;
  118. new_section(current_asmdata.asmlists[al_procedures],sec_code,'',0);
  119. hp2:=texported_item(current_module._exports.first);
  120. while assigned(hp2) do
  121. begin
  122. if (not hp2.is_var) and
  123. assigned(hp2.sym) and
  124. (hp2.sym.typ=procsym) then
  125. begin
  126. { the manglednames can already be the same when the procedure
  127. is declared with cdecl }
  128. { note: for "exports" sections we only allow non overloaded procsyms,
  129. so checking all symbols only matters for packages }
  130. anyhasalias:=false;
  131. for i:=0 to tprocsym(hp2.sym).procdeflist.count-1 do
  132. begin
  133. pd:=tprocdef(tprocsym(hp2.sym).procdeflist[i]);
  134. anyhasalias:=pd.has_alias_name(hp2.name^);
  135. if anyhasalias then
  136. break;
  137. end;
  138. if not anyhasalias then
  139. hlcg.g_external_wrapper(current_asmdata.asmlists[al_procedures],pd,hp2.name^,pd.mangledname,true);
  140. exportedsymnames.insert(hp2.name^);
  141. end
  142. else
  143. begin
  144. if assigned(hp2.sym) and
  145. (hp2.name^<>hp2.sym.mangledname) then
  146. Message2(parser_e_cant_export_var_different_name,hp2.sym.realname,hp2.sym.mangledname)
  147. else
  148. exportedsymnames.insert(hp2.name^);
  149. end;
  150. hp2:=texported_item(hp2.next);
  151. end;
  152. destroy_hlcodegen;
  153. end;
  154. end.