ptconst.pas 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Reads typed constants
  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 ptconst;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses symtype,symsym,aasmdata;
  21. procedure read_typed_const(list:tasmlist;sym:tstaticvarsym;in_structure:boolean);
  22. implementation
  23. uses
  24. globtype,systems,globals,verbose,cutils,tokens,
  25. aasmbase,aasmtai,
  26. procinfo,fmodule,
  27. scanner,pbase,pdecvar,
  28. node,nbas,ngtcon,ngenutil,
  29. symconst,symbase,symdef
  30. ;
  31. procedure read_typed_const(list:tasmlist;sym:tstaticvarsym;in_structure:boolean);
  32. var
  33. storefilepos : tfileposinfo;
  34. section : ansistring;
  35. tcbuilder : ttypedconstbuilder;
  36. reslist,
  37. datalist : tasmlist;
  38. restree,
  39. previnit : tnode;
  40. symind : tasmsymbol;
  41. begin
  42. { mark the staticvarsym as typedconst }
  43. include(sym.varoptions,vo_is_typed_const);
  44. { The variable has a value assigned }
  45. sym.varstate:=vs_initialised;
  46. { the variable can't be placed in a register }
  47. sym.varregable:=vr_none;
  48. { generate data for typed const }
  49. storefilepos:=current_filepos;
  50. current_filepos:=sym.fileinfo;
  51. if not(target_info.system in systems_typed_constants_node_init) then
  52. begin
  53. maybe_new_object_file(list);
  54. tcbuilder:=tasmlisttypedconstbuilderclass(ctypedconstbuilder).create(sym);
  55. tasmlisttypedconstbuilder(tcbuilder).parse_into_asmlist;
  56. end
  57. else
  58. begin
  59. if assigned(current_structdef) then
  60. previnit:=current_structdef.tcinitcode
  61. else
  62. previnit:=tnode(current_module.tcinitcode);
  63. tcbuilder:=tnodetreetypedconstbuilderclass(ctypedconstbuilder).create(sym,previnit);
  64. restree:=tnodetreetypedconstbuilder(tcbuilder).parse_into_nodetree;
  65. if assigned(current_structdef) then
  66. current_structdef.tcinitcode:=restree
  67. else
  68. current_module.tcinitcode:=restree;
  69. end;
  70. { Parse hints }
  71. try_consume_hintdirective(sym.symoptions,sym.deprecatedmsg);
  72. consume(_SEMICOLON);
  73. { parse public/external/export/... }
  74. if not in_structure and
  75. (
  76. (
  77. (token = _ID) and
  78. (idtoken in [_EXPORT,_EXTERNAL,_WEAKEXTERNAL,_PUBLIC,_CVAR]) and
  79. (m_cvar_support in current_settings.modeswitches)
  80. ) or
  81. (
  82. (m_mac in current_settings.modeswitches) and
  83. (
  84. (cs_external_var in current_settings.localswitches) or
  85. (cs_externally_visible in current_settings.localswitches)
  86. )
  87. )
  88. ) then
  89. read_public_and_external(sym);
  90. { try to parse a section directive }
  91. if not in_structure and (target_info.system in systems_allow_section) and
  92. (symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) and
  93. (idtoken=_SECTION) then
  94. begin
  95. try_consume_sectiondirective(section);
  96. if section<>'' then
  97. begin
  98. if (sym.varoptions *[vo_is_external,vo_is_weak_external])<>[] then
  99. Message(parser_e_externals_no_section);
  100. if sym.typ<>staticvarsym then
  101. Message(parser_e_section_no_locals);
  102. tstaticvarsym(sym).section:=section;
  103. include(sym.varoptions, vo_has_section);
  104. end;
  105. end;
  106. if not(target_info.system in systems_typed_constants_node_init) then
  107. begin
  108. { only now get the final asmlist, because inserting the symbol
  109. information depends on potential section information set above }
  110. tasmlisttypedconstbuilder(tcbuilder).get_final_asmlists(reslist,datalist);
  111. { add the parsed value }
  112. list.concatlist(reslist);
  113. { and pointed data, if any }
  114. current_asmdata.asmlists[al_const].concatlist(datalist);
  115. { the (empty) lists themselves are freed by tcbuilder }
  116. if (tf_supports_packages in target_info.flags) then
  117. begin
  118. { add indirect symbol }
  119. { ToDo: do we also need this for the else part? }
  120. new_section(list,sec_rodata,lower(sym.mangledname),const_align(sym.vardef.alignment));
  121. symind:=current_asmdata.DefineAsmSymbol(sym.mangledname,AB_INDIRECT,AT_DATA);
  122. list.concat(Tai_symbol.Create_Global(symind,0));
  123. list.concat(Tai_const.Createname(sym.mangledname,AT_DATA,0));
  124. list.concat(tai_symbol_end.Create(symind));
  125. end;
  126. end
  127. else
  128. begin
  129. { nothing to do }
  130. end;
  131. tcbuilder.free;
  132. current_filepos:=storefilepos;
  133. end;
  134. end.