cresstr.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. {
  2. Copyright (c) 1998-2002 by Michael van Canneyt
  3. Handles resourcestrings
  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 cresstr;
  18. {$i fpcdefs.inc}
  19. interface
  20. Procedure GenerateResourceStrings;
  21. implementation
  22. uses
  23. SysUtils,
  24. cclasses,widestr,
  25. cutils,globtype,globals,systems,
  26. symconst,symtype,symdef,symsym,
  27. verbose,fmodule,ppu,
  28. aasmbase,aasmtai,aasmdata,
  29. aasmcpu,
  30. {$if FPC_FULLVERSION<20700}
  31. ccharset,
  32. {$endif}
  33. asmutils;
  34. Type
  35. { These are used to form a singly-linked list, ordered by hash value }
  36. TResourceStringItem = class(TLinkedListItem)
  37. Sym : TConstSym;
  38. Name : String;
  39. Value : Pchar;
  40. Len : Longint;
  41. hash : Cardinal;
  42. constructor Create(asym:TConstsym);
  43. destructor Destroy;override;
  44. procedure CalcHash;
  45. end;
  46. Tresourcestrings=class
  47. private
  48. List : TLinkedList;
  49. procedure ConstSym_Register(p:TObject;arg:pointer);
  50. public
  51. constructor Create;
  52. destructor Destroy;override;
  53. procedure CreateResourceStringData;
  54. procedure WriteRSJFile;
  55. procedure RegisterResourceStrings;
  56. end;
  57. { ---------------------------------------------------------------------
  58. TRESOURCESTRING_ITEM
  59. ---------------------------------------------------------------------}
  60. constructor TResourceStringItem.Create(asym:TConstsym);
  61. begin
  62. inherited Create;
  63. Sym:=Asym;
  64. Name:=lower(asym.owner.name^+'.'+asym.Name);
  65. Len:=asym.value.len;
  66. GetMem(Value,Len);
  67. Move(asym.value.valueptr^,Value^,Len);
  68. CalcHash;
  69. end;
  70. destructor TResourceStringItem.Destroy;
  71. begin
  72. FreeMem(Value);
  73. end;
  74. procedure TResourceStringItem.CalcHash;
  75. Var
  76. g : Cardinal;
  77. I : longint;
  78. begin
  79. hash:=0;
  80. For I:=0 to Len-1 do { 0 terminated }
  81. begin
  82. hash:=hash shl 4;
  83. inc(Hash,Ord(Value[i]));
  84. g:=hash and ($f shl 28);
  85. if g<>0 then
  86. begin
  87. hash:=hash xor (g shr 24);
  88. hash:=hash xor g;
  89. end;
  90. end;
  91. If Hash=0 then
  92. Hash:=$ffffffff;
  93. end;
  94. { ---------------------------------------------------------------------
  95. Tresourcestrings
  96. ---------------------------------------------------------------------}
  97. Constructor Tresourcestrings.Create;
  98. begin
  99. List:=TLinkedList.Create;
  100. end;
  101. Destructor Tresourcestrings.Destroy;
  102. begin
  103. List.Free;
  104. end;
  105. procedure Tresourcestrings.CreateResourceStringData;
  106. Var
  107. namelab,
  108. valuelab : tasmlabofs;
  109. s : tsymstr;
  110. labind,
  111. resstrlab : tasmsymbol;
  112. endsymlab : tasmsymbol;
  113. R : TResourceStringItem;
  114. listind : TAsmList;
  115. begin
  116. { Put resourcestrings in a new objectfile. Putting it in multiple files
  117. makes the linking too dependent on the linker script requiring a SORT(*) for
  118. the data sections }
  119. maybe_new_object_file(current_asmdata.asmlists[al_const]);
  120. new_section(current_asmdata.asmlists[al_const],sec_rodata_norel,make_mangledname('RESSTRTABLE',current_module.localsymtable,''),sizeof(pint));
  121. maybe_new_object_file(current_asmdata.asmlists[al_resourcestrings]);
  122. new_section(current_asmdata.asmlists[al_resourcestrings],sec_data,make_mangledname('RESSTR',current_module.localsymtable,'1_START'),sizeof(pint));
  123. s:=make_mangledname('RESSTR',current_module.localsymtable,'START');
  124. current_asmdata.AsmLists[al_resourcestrings].concat(tai_symbol.createname_global(
  125. s,AT_DATA,0));
  126. { indirect symbol }
  127. labind:=current_asmdata.DefineAsmSymbol(s+indirect_suffix,AB_GLOBAL,AT_DATA);
  128. current_asmdata.asmlists[al_resourcestrings].concat(Tai_symbol.Create_Global(labind,0));
  129. current_asmdata.asmlists[al_resourcestrings].concat(Tai_const.Createname(s,AT_DATA,0));
  130. current_asmdata.asmlists[al_resourcestrings].concat(tai_symbol_end.Create(labind));
  131. { Write unitname entry }
  132. namelab:=emit_ansistring_const(current_asmdata.asmlists[al_const],@current_module.localsymtable.name^[1],length(current_module.localsymtable.name^),getansistringcodepage,False);
  133. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.Create_sym_offset(namelab.lab,namelab.ofs));
  134. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_nil_dataptr);
  135. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_nil_dataptr);
  136. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_32bit(0));
  137. {$ifdef cpu64bitaddr}
  138. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_32bit(0));
  139. {$endif cpu64bitaddr}
  140. { Add entries }
  141. listind:=TAsmList.create_without_marker;
  142. R:=TResourceStringItem(List.First);
  143. while assigned(R) do
  144. begin
  145. new_section(current_asmdata.asmlists[al_const],sec_rodata_norel,make_mangledname('RESSTR',current_module.localsymtable,'d_'+r.name),sizeof(pint));
  146. { Write default value }
  147. if assigned(R.value) and (R.len<>0) then
  148. valuelab:=emit_ansistring_const(current_asmdata.asmlists[al_const],R.Value,R.Len,getansistringcodepage,False)
  149. else
  150. begin
  151. valuelab.lab:=nil;
  152. valuelab.ofs:=0;
  153. end;
  154. { Append the name as a ansistring. }
  155. current_asmdata.asmlists[al_const].concat(cai_align.Create(const_align(sizeof(pint))));
  156. namelab:=emit_ansistring_const(current_asmdata.asmlists[al_const],@R.Name[1],length(R.name),getansistringcodepage,False);
  157. {
  158. Resourcestring index:
  159. TResourceStringRecord = Packed Record
  160. Name,
  161. CurrentValue,
  162. DefaultValue : AnsiString;
  163. HashValue : LongWord;
  164. end;
  165. }
  166. new_section(current_asmdata.asmlists[al_resourcestrings],sec_data,make_mangledname('RESSTR',current_module.localsymtable,'2_'+r.name),sizeof(pint));
  167. resstrlab:=current_asmdata.DefineAsmSymbol(make_mangledname('RESSTR',R.Sym.owner,R.Sym.name),AB_GLOBAL,AT_DATA);
  168. current_asmdata.asmlists[al_resourcestrings].concat(tai_symbol.Create_global(resstrlab,0));
  169. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.Create_sym_offset(namelab.lab,namelab.ofs));
  170. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.Create_sym_offset(valuelab.lab,valuelab.ofs));
  171. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.Create_sym_offset(valuelab.lab,valuelab.ofs));
  172. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_32bit(longint(R.Hash)));
  173. {$ifdef cpu64bitaddr}
  174. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_32bit(0));
  175. {$endif cpu64bitaddr}
  176. current_asmdata.asmlists[al_resourcestrings].concat(tai_symbol_end.create(resstrlab));
  177. if tf_supports_packages in target_info.flags then
  178. begin
  179. { indirect symbol }
  180. labind:=current_asmdata.DefineAsmSymbol(resstrlab.name+indirect_suffix,AB_GLOBAL,AT_DATA);
  181. listind.concat(Tai_symbol.Create_Global(labind,0));
  182. listind.concat(Tai_const.Create_sym(resstrlab));
  183. listind.concat(tai_symbol_end.Create(labind));
  184. end;
  185. R:=TResourceStringItem(R.Next);
  186. end;
  187. new_section(current_asmdata.asmlists[al_resourcestrings],sec_data,make_mangledname('RESSTR',current_module.localsymtable,'3_END'),sizeof(pint));
  188. endsymlab:=current_asmdata.DefineAsmSymbol(make_mangledname('RESSTR',current_module.localsymtable,'END'),AB_GLOBAL,AT_DATA);
  189. current_asmdata.AsmLists[al_resourcestrings].concat(tai_symbol.create_global(endsymlab,0));
  190. { indirect symbol }
  191. labind:=current_asmdata.DefineAsmSymbol(endsymlab.name+indirect_suffix,AB_GLOBAL,AT_DATA);
  192. current_asmdata.asmlists[al_resourcestrings].concat(Tai_symbol.Create_Global(labind,0));
  193. current_asmdata.asmlists[al_resourcestrings].concat(Tai_const.Create_sym(endsymlab));
  194. current_asmdata.asmlists[al_resourcestrings].concat(tai_symbol_end.Create(labind));
  195. { append the list of indirect resource string symbols }
  196. if tf_supports_packages in target_info.flags then
  197. current_asmdata.asmlists[al_resourcestrings].concatList(listind);
  198. listind.free;
  199. { The darwin/ppc64 assembler or linker seems to have trouble }
  200. { if a section ends with a global label without any data after it. }
  201. { So for safety, just put a dummy value here. }
  202. { Further, the regular linker also kills this symbol when turning }
  203. { on smart linking in case no value appears after it, so put the }
  204. { dummy byte there always }
  205. { Update: the Mac OS X 10.6 linker orders data that needs to be }
  206. { relocated before all other data, so make this data relocatable, }
  207. { otherwise the end label won't be moved with the rest }
  208. if (target_info.system in (systems_darwin+systems_aix)) then
  209. current_asmdata.asmlists[al_resourcestrings].concat(Tai_const.create_sym(endsymlab));
  210. end;
  211. procedure Tresourcestrings.WriteRSJFile;
  212. Var
  213. F: Text;
  214. R: TResourceStringItem;
  215. ResFileName: string;
  216. I: Integer;
  217. C: tcompilerwidechar;
  218. W: pcompilerwidestring;
  219. begin
  220. ResFileName:=ChangeFileExt(current_module.ppufilename,'.rsj');
  221. message1 (general_i_writingresourcefile,ExtractFileName(ResFileName));
  222. Assign(F,ResFileName);
  223. {$push}{$i-}
  224. Rewrite(f);
  225. {$pop}
  226. if IOresult<>0 then
  227. begin
  228. message1(general_e_errorwritingresourcefile,ResFileName);
  229. exit;
  230. end;
  231. writeln(f,'{"version":1,"strings":[');
  232. R:=TResourceStringItem(List.First);
  233. while assigned(R) do
  234. begin
  235. write(f, '{"hash":',R.Hash,',"name":"',R.Name,'","value":"');
  236. initwidestring(W);
  237. ascii2unicode(R.Value,R.Len,current_settings.sourcecodepage,W);
  238. for I := 0 to W^.len - 1 do
  239. begin
  240. C := W^.Data[I];
  241. case C of
  242. Ord('"'), Ord('\'), Ord('/'):
  243. write(f, '\', Chr(C));
  244. 8:
  245. write(f, '\b');
  246. 9:
  247. write(f, '\t');
  248. 10:
  249. write(f, '\n');
  250. 13:
  251. write(f, '\r');
  252. 12:
  253. write(f, '\f');
  254. else
  255. if (C < 32) or (C > 127) then
  256. write(f,'\u',hexStr(Longint(C), 4))
  257. else
  258. write(f,Chr(C));
  259. end;
  260. end;
  261. donewidestring(W);
  262. write(f,'"}');
  263. R:=TResourceStringItem(R.Next);
  264. if assigned(R) then
  265. writeln(f,',')
  266. else
  267. writeln(f);
  268. end;
  269. writeln(f,']}');
  270. close(f);
  271. end;
  272. procedure Tresourcestrings.ConstSym_Register(p:TObject;arg:pointer);
  273. begin
  274. if (tsym(p).typ=constsym) and
  275. (tconstsym(p).consttyp=constresourcestring) then
  276. List.Concat(TResourceStringItem.Create(TConstsym(p)));
  277. end;
  278. procedure Tresourcestrings.RegisterResourceStrings;
  279. begin
  280. if assigned(current_module.globalsymtable) then
  281. current_module.globalsymtable.SymList.ForEachCall(@ConstSym_Register,nil);
  282. current_module.localsymtable.SymList.ForEachCall(@ConstSym_Register,nil);
  283. end;
  284. Procedure GenerateResourceStrings;
  285. var
  286. resstrs : Tresourcestrings;
  287. begin
  288. resstrs:=Tresourcestrings.Create;
  289. resstrs.RegisterResourceStrings;
  290. if not resstrs.List.Empty then
  291. begin
  292. current_module.flags:=current_module.flags or uf_has_resourcestrings;
  293. resstrs.CreateResourceStringData;
  294. resstrs.WriteRSJFile;
  295. end;
  296. resstrs.Free;
  297. end;
  298. end.