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
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.
13 package org
.tianocore
.migration
;
15 import java
.io
.BufferedWriter
;
17 import java
.io
.FileWriter
;
18 import java
.io
.PrintWriter
;
19 import java
.util
.HashSet
;
20 import java
.util
.Iterator
;
22 import java
.util
.regex
.Matcher
;
23 import java
.util
.regex
.Pattern
;
25 import org
.tianocore
.UsageTypes
;
27 public final class SourceFileReplacer
implements Common
.ForDoAll
{
28 private static final SourceFileReplacer SFReplacer
= new SourceFileReplacer();
30 private ModuleInfo mi
;
32 private static final Set
<Common
.Laplace
> Laplaces
= new HashSet
<Common
.Laplace
>();
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
>();
37 private static final Set
<r8tor9
> filemacro
= new HashSet
<r8tor9
>();
39 private static final Set
<r8tor9
> fileguid
= new HashSet
<r8tor9
>();
41 private static final Set
<r8tor9
> fileppi
= new HashSet
<r8tor9
>();
43 private static final Set
<r8tor9
> fileprotocol
= new HashSet
<r8tor9
>();
45 private static final Set
<String
> filer8only
= new HashSet
<String
>();
47 private static final String
[] specialhoblibfunc
= { "BuildModuleHob",
48 "BuildResourceDescriptorHob", "BuildFvHob", "BuildCpuHob",
49 "BuildGuidDataHob", "BuildStackHob", "BuildBspStoreHob",
50 "BuildMemoryAllocationHob" };
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" };
58 // ---------------------------------------inner
59 // classes---------------------------------------//
60 private static class r8tor9
{
61 r8tor9(String r8
, String r9
) {
66 public String r8thing
;
68 public String r9thing
;
71 private class IdleLaplace
extends Common
.Laplace
{
72 public String
operation(String wholeline
) {
73 return replaceLibrary(wholeline
, mi
.hashmacro
);
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"));
83 public String
namechange(String oldname
) {
84 if (oldname
.contains(".H")) {
85 return oldname
.replaceFirst(".H", ".h");
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\\>");
98 return addincludefile(wholeline
, "\\<DxeDepex.h\\>");
102 public boolean recognize(String filename
) {
103 return filename
.contains(".dxs");
106 public String
namechange(String oldname
) {
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");
128 if (Common
.find(wholeline
, "\\bg?RT\\b")) {
129 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib");
131 if (Common
.find(wholeline
, "\\bgDS\\b")) {
132 mi
.hashrequiredr9libs
.add("DxeServicesTableLib");
135 wholeline
= replaceLibrary(wholeline
, mi
.hashnonlocalfunc
);
136 wholeline
= replaceLibrary(wholeline
, mi
.hashmacro
);
138 wholeline
= replaceMacro(wholeline
, mi
.hashnonlocalmacro
);
141 replaceGuid(wholeline
, mi
.guids
, "guid", fileguid
);
142 replaceGuid(wholeline
, mi
.ppis
, "ppi", fileppi
);
143 replaceGuid(wholeline
, mi
.protocols
, "protocol", fileprotocol
);
146 if (mi
.getModuleType().matches("PEIM")) {
148 // Try to remove PeiServicesTablePointer;
150 wholeline
= dropPeiServicesPointer(wholeline
);
152 // Drop the possible return Status of Hob building function.
154 wholeline
= drophobLibReturnStatus(wholeline
);
157 // Expand obsolete R8 macro.
159 wholeline
= replaceObsoleteMacro(wholeline
);
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
);
174 fileprotocol
.clear();
180 public boolean recognize(String filename
) {
181 return filename
.contains(".c") || filename
.contains(".C");
184 public String
namechange(String oldname
) {
185 if (oldname
.contains(".C")) {
186 return oldname
.replaceFirst(".C", ".c");
193 // ---------------------------------------inner
194 // classes---------------------------------------//
196 // -------------------------------------process
197 // functions-------------------------------------//
198 private static final String
addincludefile(String wholeline
, String hfile
) {
199 return wholeline
.replaceFirst("(\\*/\\s)", "$1\n#include " + hfile
203 private static final void show(Set
<r8tor9
> hash
, String sh
) {
204 Iterator
<r8tor9
> it
= hash
.iterator();
206 if (!hash
.isEmpty()) {
207 MigrationTool
.ui
.print("Converting " + sh
+ " : ");
208 while (it
.hasNext()) {
210 MigrationTool
.ui
.print("[" + temp
.r8thing
+ "->" + temp
.r9thing
213 MigrationTool
.ui
.println("");
217 private static final void replaceGuid(String line
, Set
<String
> hash
,
218 String kind
, Set
<r8tor9
> filehash
) {
222 it
= hash
.iterator();
223 while (it
.hasNext()) {
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
));
236 private final String
dropPeiServicesPointer(String wholeline
) {
237 String peiServicesTablePointer
;
238 String peiServicesTableCaller
;
239 String regPeiServices
;
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
);
252 wholeline
= mtrPei
.replaceAll("PeiServices"
253 + peiserviceslibfunc
[i
] + " (");
254 mi
.hashrequiredr9libs
.add("PeiServicesLib");
257 regPeiServices
= peiServicesTableCaller
+ "(CopyMem|SetMem)"
259 ptnPei
= Pattern
.compile(regPeiServices
);
260 mtrPei
= ptnPei
.matcher(wholeline
);
262 wholeline
= mtrPei
.replaceAll("$2 (");
263 mi
.hashrequiredr9libs
.add("BaseMemoryLib");
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");
276 private final String
drophobLibReturnStatus(String wholeline
) { // or use
282 Pattern ptnhobstatus
;
283 Matcher mtrhobstatus
;
284 String templine
= wholeline
;
285 for (int i
= 0; i
< specialhoblibfunc
.length
; i
++) {
287 ptnhobstatus
= Pattern
.compile(
288 "((?:\t| )*)(\\w(?:\\w|\\d)*)\\s*=\\s*"
289 + specialhoblibfunc
[i
] + "(.*?;)",
291 mtrhobstatus
= ptnhobstatus
.matcher(templine
);
292 if (!mtrhobstatus
.find()) {
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
304 templine
= mtrhobstatus
.replaceFirst(replaceString
);
310 private final String
replaceMacro(String wholeline
, Set
<String
> symbolSet
) {
315 it
= symbolSet
.iterator();
316 while (it
.hasNext()) { // macros are all assumed MdePkg currently
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
323 wholeline
= wholeline
.replaceAll(findString
, r9thing
);
324 filemacro
.add(new r8tor9(r8thing
, r9thing
));
331 private final String
replaceLibrary(String wholeline
, Set
<String
> symbolSet
) {
332 boolean addr8
= false;
333 // start replacing names
337 // Converting non-locla function
338 it
= symbolSet
.iterator();
339 while (it
.hasNext()) {
341 mi
.addLibraryClass(MigrationTool
.db
.getR9Lib(r8thing
),
342 UsageTypes
.ALWAYS_CONSUMED
);
343 // mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing)); //
344 // add a library here
347 if ((r9thing
= MigrationTool
.db
.getR9Func(r8thing
)) != null) {
348 if (!r8thing
.equals(r9thing
)) {
349 if (wholeline
.contains(r8thing
)) {
350 String findString
= "(?<!(?:\\d|\\w))" + r8thing
352 wholeline
= wholeline
.replaceAll(findString
, r9thing
);
353 filefunc
.add(new r8tor9(r8thing
, r9thing
));
354 Iterator
<r8tor9
> rt
= filefunc
.iterator();
355 while (rt
.hasNext()) {
357 if (MigrationTool
.db
.r8only
.contains(temp
.r8thing
)) {
358 filer8only
.add(r8thing
);
359 mi
.hashr8only
.add(r8thing
);
366 } // is any of the guids changed?
368 wholeline
= addincludefile(wholeline
, "\"R8Lib.h\"");
373 private final String
replaceObsoleteMacro(String wholeline
) {
375 mtrmac
= Pattern
.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(
379 .replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");
381 mtrmac
= Pattern
.compile("EFI_MIN\\((.*), (.*)\\)").matcher(wholeline
);
384 .replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");
386 mtrmac
= Pattern
.compile("EFI_MAX\\((.*), (.*)\\)").matcher(wholeline
);
389 .replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");
391 mtrmac
= Pattern
.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(
395 .replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");
397 if (wholeline
.contains("EFI_UINTN_ALIGN_MASK")) {
398 wholeline
= wholeline
.replaceAll("EFI_UINTN_ALIGN_MASK",
399 "(sizeof (UINTN) - 1)");
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
;
422 if (mi
.license
!= null) {
423 String header
= "/**@file\n Copyright (c) 2007, Intel Corporation\n\n" +
424 mi
.license
.replace(" ", " ") + "**/\n\n";
425 outfile1
.append(header
);
426 outfile2
.append(header
);
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));
438 while ((mtrr8onlyhead
= Func
.ptnbrace
.matcher(paragraph
))
440 paragraph
= mtrr8onlyhead
.replaceAll(";");
442 outfile2
.append(paragraph
+ "\n\n");
450 mi
.localmodulesources
.add("R8Lib.h");
451 mi
.localmodulesources
.add("R8Lib.c");
454 // -------------------------------------process
455 // functions-------------------------------------//
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
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
));
476 public boolean filter(File dir
) {
480 // -----------------------------------ForDoAll-----------------------------------//
482 private final void setModuleInfo(ModuleInfo moduleinfo
) {
486 private final void start() throws Exception
{
487 Laplaces
.add(new DxsLaplace());
488 Laplaces
.add(new CLaplace());
489 Laplaces
.add(new IdleLaplace());
491 Common
.toDoAll(mi
.temppath
, this, Common
.FILE
);
493 if (!mi
.hashr8only
.isEmpty()) {
500 public static final void fireAt(ModuleInfo moduleinfo
) throws Exception
{
501 SFReplacer
.setModuleInfo(moduleinfo
);