]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/MigrationTools/org/tianocore/migration/SourceFileReplacer.java
Fix one bug in PeiMain to make it output correct ImageStartAddress. And in DxeIplX64P...
[mirror_edk2.git] / Tools / Source / MigrationTools / org / tianocore / migration / SourceFileReplacer.java
CommitLineData
b0282412 1/** @file\r
2 \r
3 Copyright (c) 2006, Intel Corporation\r
4 All rights reserved. This program and the accompanying materials\r
5 are licensed and made available under the terms and conditions of the BSD License\r
6 which accompanies this distribution. The full text of the license may be found at\r
7 http://opensource.org/licenses/bsd-license.php\r
8 \r
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11 \r
12 **/\r
0dc8c589 13package org.tianocore.migration;\r
14\r
15import java.io.*;\r
16import java.util.*;\r
17import java.util.regex.Matcher;\r
18import java.util.regex.Pattern;\r
19\r
5ea254f6 20public final class SourceFileReplacer {\r
7bcb8d17 21 SourceFileReplacer(String path, String outpath, ModuleInfo moduleinfo) {\r
0dc8c589 22 modulepath = path;\r
5ea254f6 23 outputpath = outpath;\r
0dc8c589 24 mi = moduleinfo;\r
0dc8c589 25 }\r
26 private String modulepath;\r
5ea254f6 27 private String outputpath;\r
0dc8c589 28 private ModuleInfo mi;\r
0dc8c589 29 private boolean showdetails = false;\r
30 \r
31 private class r8tor9 {\r
32 r8tor9(String r8, String r9) {\r
33 r8thing = r8;\r
34 r9thing = r9;\r
35 }\r
36 public String r8thing;\r
37 public String r9thing;\r
38 }\r
39 \r
40 // these sets are used only for printing log of the changes in current file\r
41 private Set<r8tor9> filefunc = new HashSet<r8tor9>();\r
42 private Set<r8tor9> filemacro = new HashSet<r8tor9>();\r
43 private Set<r8tor9> fileguid = new HashSet<r8tor9>();\r
44 private Set<r8tor9> fileppi = new HashSet<r8tor9>();\r
45 private Set<r8tor9> fileprotocol = new HashSet<r8tor9>();\r
46 private Set<String> filer8only = new HashSet<String>();\r
47 \r
5ea254f6 48 private static final String r8only = "EfiLibInstallDriverBinding " +\r
0dc8c589 49 "EfiLibInstallAllDriverProtocols " +\r
50 "EfiLibCompareLanguage " +\r
51 "BufToHexString " +\r
e6a5df3b 52 "EfiStrTrim " + //is the r8only lib going to be enlarged???? Caution !!!!\r
0dc8c589 53 "EfiValueToHexStr " +\r
54 "HexStringToBuf " +\r
55 "IsHexDigit " +\r
56 "NibbleToHexChar " +\r
57 "GetHob " +\r
58 "GetHobListSize " +\r
59 "GetHobVersion " +\r
60 "GetHobBootMode " +\r
61 "GetCpuHobInfo " +\r
62 "GetDxeCoreHobInfo " +\r
63 "GetNextFirmwareVolumeHob " +\r
64 "GetNextGuidHob " +\r
65 "GetPalEntryHobInfo " +\r
66 "GetIoPortSpaceAddressHobInfo ";\r
67 \r
68 public void flush() throws Exception {\r
e6a5df3b 69 String outname = null;\r
70 String inname = null;\r
7bcb8d17 71 if (MigrationTool.ui.yesOrNo("Changes will be made to the Source Code. View details?")) {\r
0dc8c589 72 showdetails = true;\r
73 }\r
e6a5df3b 74 \r
75 Iterator<String> di = mi.localmodulesources.iterator();\r
76 while (di.hasNext()) {\r
77 inname = di.next();\r
78 if (inname.contains(".c") || inname.contains(".C")) {\r
79 if (inname.contains(".C")) {\r
80 outname = inname.replaceFirst(".C", ".c");\r
81 } else {\r
82 outname = inname;\r
83 }\r
7bcb8d17 84 MigrationTool.ui.println("\nModifying file: " + inname);\r
5ea254f6 85 Common.string2file(sourcefilereplace(modulepath + File.separator + "temp" + File.separator + inname), outputpath + File.separator + "Migration_" + mi.modulename + File.separator + outname);\r
e6a5df3b 86 } else if (inname.contains(".h") || inname.contains(".H") || inname.contains(".dxs") || inname.contains(".uni")) {\r
87 if (inname.contains(".H")) {\r
88 outname = inname.replaceFirst(".H", ".h");\r
0dc8c589 89 } else {\r
e6a5df3b 90 outname = inname;\r
0dc8c589 91 }\r
7bcb8d17 92 MigrationTool.ui.println("\nCopying file: " + inname);\r
5ea254f6 93 Common.string2file(Common.file2string(modulepath + File.separator + "temp" + File.separator + inname), outputpath + File.separator + "Migration_" + mi.modulename + File.separator + outname);\r
0dc8c589 94 }\r
95 }\r
96\r
97 if (!mi.hashr8only.isEmpty()) {\r
98 addr8only();\r
99 }\r
100 }\r
101 \r
102 private void addr8only() throws Exception {\r
103 String paragraph = null;\r
fed802b1 104 String line = Common.file2string(Database.defaultpath + File.separator + "R8Lib.c");\r
90503bad 105 Common.ensureDir(modulepath + File.separator + "result" + File.separator + "R8Lib.c");\r
5ea254f6 106 PrintWriter outfile1 = new PrintWriter(new BufferedWriter(new FileWriter(outputpath + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.c")));\r
107 PrintWriter outfile2 = new PrintWriter(new BufferedWriter(new FileWriter(outputpath + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.h")));\r
0dc8c589 108 Pattern ptnr8only = Pattern.compile("////#?(\\w*)?.*?R8_(\\w*).*?////~", Pattern.DOTALL);\r
109 Matcher mtrr8only = ptnr8only.matcher(line);\r
110 Matcher mtrr8onlyhead;\r
111 while (mtrr8only.find()) {\r
112 if (mi.hashr8only.contains(mtrr8only.group(2))) {\r
113 paragraph = mtrr8only.group();\r
114 outfile1.append(paragraph + "\n\n");\r
115 if (mtrr8only.group(1).length() != 0) {\r
116 mi.hashrequiredr9libs.add(mtrr8only.group(1));\r
117 }\r
118 //generate R8lib.h\r
119 while ((mtrr8onlyhead = Func.ptnbrace.matcher(paragraph)).find()) {\r
120 paragraph = mtrr8onlyhead.replaceAll(";");\r
121 }\r
122 outfile2.append(paragraph + "\n\n");\r
123 }\r
124 }\r
125 outfile1.flush();\r
126 outfile1.close();\r
127 outfile2.flush();\r
128 outfile2.close();\r
e6a5df3b 129 \r
130 mi.localmodulesources.add("R8Lib.h");\r
131 mi.localmodulesources.add("R8Lib.c");\r
0dc8c589 132 }\r
133 \r
0dc8c589 134 // Caution : if there is @ in file , it will be replaced with \n , so is you use Doxygen ... God Bless you!\r
135 private String sourcefilereplace(String filename) throws Exception {\r
136 BufferedReader rd = new BufferedReader(new FileReader(filename));\r
137 StringBuffer wholefile = new StringBuffer();\r
138 String line;\r
139 String r8thing;\r
140 String r9thing;\r
141 r8tor9 temp;\r
142 boolean addr8 = false;\r
143\r
144 Pattern pat = Pattern.compile("g?(BS|RT)(\\s*->\\s*)([a-zA-Z_]\\w*)", Pattern.MULTILINE); // ! only two level () bracket allowed !\r
145 //Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*[#$]*)(\\s*\\(([^\\(\\)]*(\\([^\\(\\)]*\\))?[^\\(\\)]*)*\\))", Pattern.MULTILINE);\r
146\r
147 while ((line = rd.readLine()) != null) {\r
148 wholefile.append(line + "\n");\r
149 }\r
150 line = wholefile.toString();\r
151 \r
152 // replace BS -> gBS , RT -> gRT\r
153 Matcher mat = pat.matcher(line);\r
154 if (mat.find()) { // add a library here\r
7bcb8d17 155 MigrationTool.ui.println("Converting all BS->gBS, RT->gRT");\r
0dc8c589 156 line = mat.replaceAll("g$1$2$3"); //unknown correctiveness\r
157 }\r
158 mat.reset();\r
159 while (mat.find()) {\r
160 if (mat.group(1).matches("BS")) {\r
161 mi.hashrequiredr9libs.add("UefiBootServicesTableLib");\r
162 }\r
163 if (mat.group(1).matches("RT")) {\r
164 mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib");\r
165 }\r
166 }\r
167 /*\r
168 // remove EFI_DRIVER_ENTRY_POINT\r
169 Pattern patentrypoint = Pattern.compile("EFI_DRIVER_ENTRY_POINT[^\\}]*\\}");\r
170 Matcher matentrypoint = patentrypoint.matcher(line);\r
171 if (matentrypoint.find()) {\r
7bcb8d17 172 MigrationTool.ui.println("Deleting Entry_Point");\r
0dc8c589 173 line = matentrypoint.replaceAll("");\r
174 }\r
175 */\r
176 // start replacing names\r
177 Iterator<String> it;\r
178 // Converting non-locla function\r
179 it = mi.hashnonlocalfunc.iterator();\r
180 while (it.hasNext()) {\r
181 r8thing = it.next();\r
182 if (r8thing.matches("EfiInitializeDriverLib")) { //s\r
183 mi.hashrequiredr9libs.add("UefiBootServicesTableLib"); //p\r
184 mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //e\r
185 } else if (r8thing.matches("DxeInitializeDriverLib")) { //c\r
186 mi.hashrequiredr9libs.add("UefiBootServicesTableLib"); //i\r
187 mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //a\r
188 mi.hashrequiredr9libs.add("DxeServicesTableLib"); //l\r
189 } else { //\r
7bcb8d17 190 mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing)); // add a library here\r
0dc8c589 191 }\r
192 \r
7bcb8d17 193 if ((r9thing = MigrationTool.db.getR9Func(r8thing)) != null) {\r
0dc8c589 194 if (!r8thing.equals(r9thing)) {\r
195 if (line.contains(r8thing)) {\r
196 line = line.replaceAll(r8thing, r9thing);\r
197 filefunc.add(new r8tor9(r8thing, r9thing));\r
198 Iterator<r8tor9> rt = filefunc.iterator();\r
199 while (rt.hasNext()) {\r
200 temp = rt.next();\r
201 if (r8only.contains(temp.r8thing)) {\r
0dc8c589 202 filer8only.add(r8thing);\r
203 mi.hashr8only.add(r8thing);\r
204 addr8 = true;\r
205 }\r
206 }\r
207 }\r
208 }\r
209 }\r
210 } //is any of the guids changed?\r
211 if (addr8 == true) {\r
212 line = line.replaceFirst("\\*/\n", "\\*/\n#include \"R8Lib.h\"\n");\r
213 }\r
214 \r
215 // Converting macro\r
216 it = mi.hashnonlocalmacro.iterator();\r
217 while (it.hasNext()) { //macros are all assumed MdePkg currently\r
218 r8thing = it.next();\r
7bcb8d17 219 //mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing)); \r
220 if ((r9thing = MigrationTool.db.getR9Macro(r8thing)) != null) {\r
0dc8c589 221 if (line.contains(r8thing)) {\r
222 line = line.replaceAll(r8thing, r9thing);\r
223 filemacro.add(new r8tor9(r8thing, r9thing));\r
224 }\r
225 }\r
226 }\r
227\r
228 // Converting guid\r
229 replaceGuid(line, mi.guid, "guid", fileguid);\r
230 replaceGuid(line, mi.ppi, "ppi", fileppi);\r
231 replaceGuid(line, mi.protocol, "protocol", fileprotocol);\r
232\r
233 // Converting Pei\r
234 // First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #%\r
235 Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern.MULTILINE);\r
236 if (mi.moduletype.contains("PEIM")) {\r
237 Matcher mtrpei = ptnpei.matcher(line);\r
238 while (mtrpei.find()) { // ! add a library here !\r
239 line = mtrpei.replaceAll("PeiServices$1#%$2");\r
240 mi.hashrequiredr9libs.add("PeiServicesLib");\r
241 }\r
242 mtrpei.reset();\r
243 if (line.contains("PeiServicesCopyMem")) {\r
244 line = line.replaceAll("PeiServicesCopyMem#%", "CopyMem");\r
245 mi.hashrequiredr9libs.add("BaseMemoryLib");\r
246 }\r
247 if (line.contains("PeiServicesSetMem")) {\r
248 line = line.replaceAll("PeiServicesSetMem#%", "SetMem");\r
249 mi.hashrequiredr9libs.add("BaseMemoryLib");\r
250 }\r
251\r
252 // Second , find all #% to drop the arg "PeiServices"\r
253 Pattern ptnpeiarg = Pattern.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern.MULTILINE);\r
254 Matcher mtrpeiarg = ptnpeiarg.matcher(line);\r
255 while (mtrpeiarg.find()) {\r
256 line = mtrpeiarg.replaceAll("$1");\r
257 }\r
258 }\r
259 \r
260 Matcher mtrmac;\r
261 mtrmac = Pattern.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(line);\r
262 if (mtrmac.find()) {\r
263 line = mtrmac.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");\r
264 }\r
265 mtrmac = Pattern.compile("EFI_MIN\\((.*), (.*)\\)").matcher(line);\r
266 if (mtrmac.find()) {\r
267 line = mtrmac.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");\r
268 }\r
269 mtrmac = Pattern.compile("EFI_MAX\\((.*), (.*)\\)").matcher(line);\r
270 if (mtrmac.find()) {\r
271 line = mtrmac.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");\r
272 }\r
273 mtrmac = Pattern.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(line);\r
274 if (mtrmac.find()) {\r
275 line = mtrmac.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");\r
276 }\r
277 if (line.contains("EFI_UINTN_ALIGN_MASK")) {\r
278 line = line.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");\r
279 }\r
280\r
281 show(filefunc, "function");\r
282 show(filemacro, "macro");\r
283 show(fileguid, "guid");\r
284 show(fileppi, "ppi");\r
285 show(fileprotocol, "protocol");\r
286 if (!filer8only.isEmpty()) {\r
7bcb8d17 287 MigrationTool.ui.println("Converting r8only : " + filer8only);\r
0dc8c589 288 }\r
289\r
290 filefunc.clear();\r
291 filemacro.clear();\r
292 fileguid.clear();\r
293 fileppi.clear();\r
294 fileprotocol.clear();\r
295 filer8only.clear();\r
e6a5df3b 296\r
0dc8c589 297 return line;\r
298 }\r
299 \r
300 private void show(Set<r8tor9> hash, String sh) {\r
301 Iterator<r8tor9> it = hash.iterator();\r
302 r8tor9 temp;\r
303 if (showdetails) {\r
304 if (!hash.isEmpty()) {\r
7bcb8d17 305 MigrationTool.ui.print("Converting " + sh + " : ");\r
0dc8c589 306 while (it.hasNext()) {\r
307 temp = it.next();\r
7bcb8d17 308 MigrationTool.ui.print("[" + temp.r8thing + "->" + temp.r9thing + "] ");\r
0dc8c589 309 }\r
7bcb8d17 310 MigrationTool.ui.println("");\r
0dc8c589 311 }\r
312 }\r
313 }\r
314 \r
315 private void replaceGuid(String line, Set<String> hash, String kind, Set<r8tor9> filehash) {\r
316 Iterator<String> it;\r
317 String r8thing;\r
318 String r9thing;\r
319 it = hash.iterator();\r
320 while (it.hasNext()) {\r
321 r8thing = it.next();\r
7bcb8d17 322 if ((r9thing = MigrationTool.db.getR9Guidname(r8thing)) != null) {\r
0dc8c589 323 if (!r8thing.equals(r9thing)) {\r
324 if (line.contains(r8thing)) {\r
325 line = line.replaceAll(r8thing, r9thing);\r
326 filehash.add(new r8tor9(r8thing, r9thing));\r
327 }\r
328 }\r
329 }\r
330 }\r
331 }\r
332}\r