Fix several bugs & enhancement.
[mirror_edk2.git] / Tools / Java / 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
16b7eeef 20public final class SourceFileReplacer implements Common.ForDoAll {\r
27e0221a 21 private static final SourceFileReplacer SFReplacer = new SourceFileReplacer();\r
22 private ModuleInfo mi;\r
23 private static final Set<Common.Laplace> Laplaces = new HashSet<Common.Laplace>();\r
2887f99b 24\r
27e0221a 25 // these sets are used only for printing log of the changes in current file\r
26 private static final Set<r8tor9> filefunc = new HashSet<r8tor9>();\r
27 private static final Set<r8tor9> filemacro = new HashSet<r8tor9>();\r
28 private static final Set<r8tor9> fileguid = new HashSet<r8tor9>();\r
29 private static final Set<r8tor9> fileppi = new HashSet<r8tor9>();\r
30 private static final Set<r8tor9> fileprotocol = new HashSet<r8tor9>();\r
31 private static final Set<String> filer8only = new HashSet<String>();\r
32 \r
33 private static final String[] specialhoblibfunc = {\r
34 "BuildModuleHob",\r
35 "BuildResourceDescriptorHob",\r
36 "BuildFvHob",\r
37 "BuildCpuHob",\r
733d48fe 38 "BuildGuidDataHob",\r
27e0221a 39 "BuildStackHob",\r
40 "BuildBspStoreHob",\r
41 "BuildMemoryAllocationHob"\r
42 };\r
10e4f990 43 private static final String[] peiserviceslibfunc = {\r
44 "InstallPpi",\r
45 "ReInstallPpi",\r
46 "LocatePpi",\r
47 "NotifyPpi",\r
48 "GetBootMode",\r
49 "SetBootMode",\r
50 "GetHobList",\r
51 "CreateHob",\r
52 "FfsFindNextVolume",\r
53 "FfsFindNextFile",\r
54 "FfsFindSectionData",\r
55 "InstallPeiMemory",\r
56 "AllocatePages",\r
57 "AllocatePool",\r
58 "PeiResetSystem"\r
59 };\r
27e0221a 60 //---------------------------------------inner classes---------------------------------------//\r
61 private static class r8tor9 {\r
62 r8tor9(String r8, String r9) {\r
63 r8thing = r8;\r
64 r9thing = r9;\r
65 }\r
66 public String r8thing;\r
67 public String r9thing;\r
68 }\r
69 \r
70 private class IdleLaplace extends Common.Laplace {\r
71 public String operation(String wholeline) {\r
9f98dbb7 72 return replaceLibrary (wholeline, mi.hashmacro);\r
27e0221a 73 }\r
74 \r
75 public boolean recognize(String filename) {\r
76 return filename.contains(".h") || filename.contains(".H") || filename.contains(".uni");\r
77 }\r
2887f99b 78\r
27e0221a 79 public String namechange(String oldname) {\r
80 if (oldname.contains(".H")) {\r
81 return oldname.replaceFirst(".H", ".h");\r
82 } else {\r
83 return oldname;\r
84 }\r
85 }\r
86 }\r
87 private class DxsLaplace extends Common.Laplace {\r
88 public String operation(String wholeline) {\r
49324055 89 wholeline = replaceMacro(wholeline, mi.hashnonlocalmacro);\r
27e0221a 90 if (mi.getModuleType().equals("PEIM")) {\r
91 return addincludefile(wholeline, "\\<PeimDepex.h\\>");\r
92 } else {\r
93 return addincludefile(wholeline, "\\<DxeDepex.h\\>");\r
94 }\r
95 }\r
96 \r
97 public boolean recognize(String filename) {\r
98 return filename.contains(".dxs");\r
99 }\r
2887f99b 100\r
27e0221a 101 public String namechange(String oldname) {\r
102 return oldname;\r
103 }\r
104 }\r
105 \r
106 private class CLaplace extends Common.Laplace {\r
107 public String operation(String wholeline) {\r
27e0221a 108 // remove EFI_DRIVER_ENTRY_POINT\r
49324055 109 wholeline = wholeline.replaceAll("(EFI_[A-Z]+_ENTRY_POINT\\s*\\(\\s*\\w(\\w|\\d)*\\s*\\))", MigrationTool.MIGRATIONCOMMENT + " $1");\r
d47b9900 110 // redefine module entry point for some self-relocated modules\r
111 wholeline = wholeline.replaceAll (mi.entrypoint + "([^{]*?})", "_ModuleEntryPoint" + "$1");\r
06d1ff5b 112 // remove R8 library contractor\r
113 wholeline = wholeline.replaceAll ("(\\b(?:Efi|Dxe)InitializeDriverLib\\b)", MigrationTool.MIGRATIONCOMMENT + " $1");\r
114 // Add Library Class for potential reference of gBS, gRT & gDS.\r
115 if (Common.find (wholeline, "\\bg?BS\\b")) {\r
116 mi.hashrequiredr9libs.add("UefiBootServicesTableLib");\r
117 }\r
118 if (Common.find (wholeline, "\\bg?RT\\b")) {\r
119 mi.hashrequiredr9libs.add ("UefiRuntimeServicesTableLib");\r
120 }\r
121 if (Common.find (wholeline, "\\bgDS\\b")) {\r
122 mi.hashrequiredr9libs.add ("DxeServicesTableLib");\r
123 }\r
9f98dbb7 124\r
125 wholeline = replaceLibrary (wholeline, mi.hashnonlocalfunc);\r
126 wholeline = replaceLibrary (wholeline, mi.hashmacro);\r
27e0221a 127 // Converting macro\r
9f98dbb7 128 wholeline = replaceMacro (wholeline, mi.hashnonlocalmacro);\r
0dc8c589 129\r
27e0221a 130 // Converting guid\r
323e2ffc 131 replaceGuid(wholeline, mi.guids, "guid", fileguid);\r
132 replaceGuid(wholeline, mi.ppis, "ppi", fileppi);\r
133 replaceGuid(wholeline, mi.protocols, "protocol", fileprotocol);\r
2887f99b 134\r
27e0221a 135 // Converting Pei\r
27e0221a 136 if (mi.getModuleType().matches("PEIM")) {\r
10e4f990 137 //\r
138 // Try to remove PeiServicesTablePointer;\r
139 // \r
140 wholeline = dropPeiServicesPointer (wholeline);\r
141 //\r
142 // Drop the possible return Status of Hob building function.\r
143 // \r
144 wholeline = drophobLibReturnStatus (wholeline);\r
27e0221a 145 }\r
10e4f990 146 //\r
147 // Expand obsolete R8 macro.\r
148 // \r
149 wholeline = replaceObsoleteMacro (wholeline);\r
0dc8c589 150\r
27e0221a 151 show(filefunc, "function");\r
152 show(filemacro, "macro");\r
153 show(fileguid, "guid");\r
154 show(fileppi, "ppi");\r
155 show(fileprotocol, "protocol");\r
156 if (!filer8only.isEmpty()) {\r
157 MigrationTool.ui.println("Converting r8only : " + filer8only);\r
158 }\r
2887f99b 159\r
27e0221a 160 filefunc.clear();\r
161 filemacro.clear();\r
162 fileguid.clear();\r
163 fileppi.clear();\r
164 fileprotocol.clear();\r
165 filer8only.clear();\r
2887f99b 166\r
27e0221a 167 return wholeline;\r
168 }\r
169 \r
170 public boolean recognize(String filename) {\r
171 return filename.contains(".c") || filename.contains(".C");\r
172 }\r
0dc8c589 173\r
27e0221a 174 public String namechange(String oldname) {\r
175 if (oldname.contains(".C")) {\r
176 return oldname.replaceFirst(".C", ".c");\r
177 } else {\r
178 return oldname;\r
179 }\r
180 }\r
181 }\r
182 //---------------------------------------inner classes---------------------------------------//\r
99b0f0e6 183\r
27e0221a 184 //-------------------------------------process functions-------------------------------------//\r
185 private static final String addincludefile(String wholeline, String hfile) {\r
186 return wholeline.replaceFirst("(\\*/\\s)", "$1\n#include " + hfile + "\n");\r
187 }\r
188 \r
189 private static final void show(Set<r8tor9> hash, String sh) {\r
190 Iterator<r8tor9> it = hash.iterator();\r
191 r8tor9 temp;\r
192 if (!hash.isEmpty()) {\r
193 MigrationTool.ui.print("Converting " + sh + " : ");\r
194 while (it.hasNext()) {\r
195 temp = it.next();\r
196 MigrationTool.ui.print("[" + temp.r8thing + "->" + temp.r9thing + "] ");\r
197 }\r
198 MigrationTool.ui.println("");\r
199 }\r
200 }\r
71874f93 201\r
27e0221a 202 private static final void replaceGuid(String line, Set<String> hash, String kind, Set<r8tor9> filehash) {\r
203 Iterator<String> it;\r
204 String r8thing;\r
205 String r9thing;\r
206 it = hash.iterator();\r
207 while (it.hasNext()) {\r
208 r8thing = it.next();\r
209 if ((r9thing = MigrationTool.db.getR9Guidname(r8thing)) != null) {\r
210 if (!r8thing.equals(r9thing)) {\r
211 if (line.contains(r8thing)) {\r
212 line = line.replaceAll(r8thing, r9thing);\r
213 filehash.add(new r8tor9(r8thing, r9thing));\r
214 }\r
215 }\r
216 }\r
217 }\r
218 }\r
71874f93 219\r
10e4f990 220 private final String dropPeiServicesPointer (String wholeline) {\r
221 String peiServicesTablePointer;\r
222 String peiServicesTableCaller;\r
223 String regPeiServices;\r
224 Pattern ptnPei;\r
225 Matcher mtrPei;\r
226\r
227 peiServicesTablePointer = "\\w(?:\\w|[0-9]|->)*";\r
228 peiServicesTableCaller = "\\(\\*\\*?\\s*(" + peiServicesTablePointer + ")\\s*\\)[.-]>?\\s*";\r
229 for (int i = 0; i < peiserviceslibfunc.length; i++) {\r
230 regPeiServices = peiServicesTableCaller + peiserviceslibfunc[i] + "\\s*\\(\\s*\\1\\s*,(\\t| )*"; \r
231 ptnPei = Pattern.compile (regPeiServices);\r
232 mtrPei = ptnPei.matcher (wholeline);\r
233 if (mtrPei.find()) {\r
234 wholeline = mtrPei.replaceAll("PeiServices" + peiserviceslibfunc[i] + " (");\r
235 mi.hashrequiredr9libs.add("PeiServicesLib");\r
236 }\r
237 }\r
238 regPeiServices = peiServicesTableCaller + "(CopyMem|SetMem)" + "\\s*\\((\\t| )*";\r
239 ptnPei = Pattern.compile (regPeiServices);\r
240 mtrPei = ptnPei.matcher (wholeline);\r
241 if (mtrPei.find()) {\r
242 wholeline = mtrPei.replaceAll("$2 (");\r
243 mi.hashrequiredr9libs.add("BaseMemoryLib");\r
244 }\r
245\r
246 ptnPei = Pattern.compile("#%+(\\s*\\(+\\s*)" + peiServicesTablePointer + "\\s*,\\s*", Pattern.MULTILINE);\r
247 mtrPei = ptnPei.matcher(wholeline);\r
248 while (mtrPei.find()) {\r
249 wholeline = mtrPei.replaceAll("$1");\r
250 }\r
251\r
252 return wholeline;\r
253 }\r
254\r
255 private final String drophobLibReturnStatus (String wholeline) { // or use regex to find pattern "Status = ..."\r
27e0221a 256 Pattern ptnhobstatus;\r
257 Matcher mtrhobstatus;\r
258 String templine = wholeline;\r
259 for (int i = 0; i < specialhoblibfunc.length; i++) {\r
260 ptnhobstatus = Pattern.compile("(Status\\s*=\\s*)?" + specialhoblibfunc[i] + "(.*?\\)\\s*;)", Pattern.DOTALL);\r
261 mtrhobstatus = ptnhobstatus.matcher(templine);\r
262 if (mtrhobstatus.find()) {\r
10e4f990 263 templine = mtrhobstatus.replaceAll(specialhoblibfunc[i] + mtrhobstatus.group(2) + "\n " + \r
264 MigrationTool.MIGRATIONCOMMENT + "R9 Hob-building library functions will assert if build failure.\n Status = EFI_SUCCESS;");\r
27e0221a 265 }\r
266 }\r
267 return templine;\r
268 }\r
9f98dbb7 269\r
270 private final String replaceMacro (String wholeline, Set<String> symbolSet) {\r
271 String r8thing;\r
272 String r9thing;\r
273 Iterator<String> it;\r
274\r
275 it = symbolSet.iterator();\r
276 while (it.hasNext()) { //macros are all assumed MdePkg currently\r
277 r8thing = it.next();\r
9f98dbb7 278 //mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing)); \r
279 if ((r9thing = MigrationTool.db.getR9Macro(r8thing)) != null) {\r
280 if (wholeline.contains(r8thing)) {\r
49324055 281 String findString = "(?<!(?:\\d|\\w))" + r8thing + "(?!(?:\\d|\\w))";\r
282 wholeline = wholeline.replaceAll(findString, r9thing);\r
9f98dbb7 283 filemacro.add(new r8tor9(r8thing, r9thing));\r
284 }\r
285 }\r
286 }\r
287 return wholeline;\r
288 }\r
289\r
290 private final String replaceLibrary (String wholeline, Set<String> symbolSet) {\r
291 boolean addr8 = false;\r
292 // start replacing names\r
293 String r8thing;\r
294 String r9thing;\r
295 Iterator<String> it;\r
296 // Converting non-locla function\r
297 it = symbolSet.iterator();\r
298 while (it.hasNext()) {\r
299 r8thing = it.next();\r
300 mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing)); // add a library here\r
301 \r
302 r8tor9 temp;\r
303 if ((r9thing = MigrationTool.db.getR9Func(r8thing)) != null) {\r
304 if (!r8thing.equals(r9thing)) {\r
305 if (wholeline.contains(r8thing)) {\r
306 wholeline = wholeline.replaceAll(r8thing, r9thing);\r
307 filefunc.add(new r8tor9(r8thing, r9thing));\r
308 Iterator<r8tor9> rt = filefunc.iterator();\r
309 while (rt.hasNext()) {\r
310 temp = rt.next();\r
311 if (MigrationTool.db.r8only.contains(temp.r8thing)) {\r
312 filer8only.add(r8thing);\r
313 mi.hashr8only.add(r8thing);\r
314 addr8 = true;\r
315 }\r
316 }\r
317 }\r
318 }\r
319 }\r
320 } //is any of the guids changed?\r
321 if (addr8 == true) {\r
322 wholeline = addincludefile(wholeline, "\"R8Lib.h\"");\r
323 }\r
324 return wholeline;\r
325 }\r
326\r
10e4f990 327 private final String replaceObsoleteMacro (String wholeline) {\r
328 Matcher mtrmac;\r
329 mtrmac = Pattern.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(wholeline);\r
330 if (mtrmac.find()) {\r
331 wholeline = mtrmac.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");\r
332 }\r
333 mtrmac = Pattern.compile("EFI_MIN\\((.*), (.*)\\)").matcher(wholeline);\r
334 if (mtrmac.find()) {\r
335 wholeline = mtrmac.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");\r
336 }\r
337 mtrmac = Pattern.compile("EFI_MAX\\((.*), (.*)\\)").matcher(wholeline);\r
338 if (mtrmac.find()) {\r
339 wholeline = mtrmac.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");\r
340 }\r
341 mtrmac = Pattern.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(wholeline);\r
342 if (mtrmac.find()) {\r
343 wholeline = mtrmac.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");\r
344 }\r
345 if (wholeline.contains("EFI_UINTN_ALIGN_MASK")) {\r
346 wholeline = wholeline.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");\r
347 }\r
348 return wholeline;\r
349 }\r
350\r
27e0221a 351 private final void addr8only() throws Exception {\r
352 String paragraph = null;\r
353 String line = Common.file2string(MigrationTool.db.DatabasePath + File.separator + "R8Lib.c");\r
354 PrintWriter outfile1 = new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.c")));\r
355 PrintWriter outfile2 = new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.h")));\r
356 Pattern ptnr8only = Pattern.compile("////#?(\\w*)?(.*?R8_(\\w*).*?)////~", Pattern.DOTALL);\r
357 Matcher mtrr8only = ptnr8only.matcher(line);\r
358 Matcher mtrr8onlyhead;\r
359 \r
360 //add head comment\r
361 Matcher mtrr8onlyheadcomment = Critic.PTN_NEW_HEAD_COMMENT.matcher(line);\r
362 if (mtrr8onlyheadcomment.find()) {\r
363 outfile1.append(mtrr8onlyheadcomment.group() + "\n\n");\r
364 outfile2.append(mtrr8onlyheadcomment.group() + "\n\n");\r
365 }\r
366 \r
367 //add functions body\r
368 while (mtrr8only.find()) {\r
369 if (mi.hashr8only.contains(mtrr8only.group(3))) {\r
370 paragraph = mtrr8only.group(2);\r
371 outfile1.append(paragraph + "\n\n");\r
372 if (mtrr8only.group(1).length() != 0) {\r
373 mi.hashrequiredr9libs.add(mtrr8only.group(1));\r
374 }\r
375 //generate R8lib.h\r
376 while ((mtrr8onlyhead = Func.ptnbrace.matcher(paragraph)).find()) {\r
377 paragraph = mtrr8onlyhead.replaceAll(";");\r
378 }\r
379 outfile2.append(paragraph + "\n\n");\r
380 }\r
381 }\r
382 outfile1.flush();\r
383 outfile1.close();\r
384 outfile2.flush();\r
385 outfile2.close();\r
386 \r
387 mi.localmodulesources.add("R8Lib.h");\r
388 mi.localmodulesources.add("R8Lib.c");\r
389 }\r
390 //-------------------------------------process functions-------------------------------------//\r
391 \r
392 //-----------------------------------ForDoAll-----------------------------------//\r
393 public void run(String filepath) throws Exception {\r
df87de9b 394 String inname = filepath.replace(mi.temppath + File.separator, "");\r
395 String tempinpath = mi.temppath + File.separator;\r
27e0221a 396 String tempoutpath = MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator;\r
16b7eeef 397\r
27e0221a 398 Iterator<Common.Laplace> itLaplace = Laplaces.iterator();\r
399 while (itLaplace.hasNext()) {\r
400 Common.Laplace lap = itLaplace.next();\r
401 if (lap.recognize(inname)) {\r
402 MigrationTool.ui.println("\nHandling file: " + inname);\r
403 lap.transform(tempinpath + inname, tempoutpath + lap.namechange(inname));\r
404 }\r
405 }\r
406 }\r
407 \r
408 public boolean filter(File dir) {\r
409 return true;\r
410 }\r
411 //-----------------------------------ForDoAll-----------------------------------//\r
412 \r
413 private final void setModuleInfo(ModuleInfo moduleinfo) {\r
414 mi = moduleinfo;\r
415 }\r
416 \r
417 private final void start() throws Exception {\r
418 Laplaces.add(new DxsLaplace());\r
419 Laplaces.add(new CLaplace());\r
420 Laplaces.add(new IdleLaplace());\r
421 \r
df87de9b 422 Common.toDoAll(mi.temppath, this, Common.FILE);\r
27e0221a 423 \r
424 if (!mi.hashr8only.isEmpty()) {\r
425 addr8only();\r
426 }\r
427 \r
428 Laplaces.clear();\r
429 }\r
430 \r
431 public static final void fireAt(ModuleInfo moduleinfo) throws Exception {\r
432 SFReplacer.setModuleInfo(moduleinfo);\r
433 SFReplacer.start();\r
434 }\r
0dc8c589 435}\r