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