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
;
17 import java
.util
.regex
.Matcher
;
18 import java
.util
.regex
.Pattern
;
20 public final class SourceFileReplacer
implements Common
.ForDoAll
{
21 private static final SourceFileReplacer SFReplacer
= new SourceFileReplacer();
22 private ModuleInfo mi
;
23 private static final Set
<Common
.Laplace
> Laplaces
= new HashSet
<Common
.Laplace
>();
25 // these sets are used only for printing log of the changes in current file
26 private static final Set
<r8tor9
> filefunc
= new HashSet
<r8tor9
>();
27 private static final Set
<r8tor9
> filemacro
= new HashSet
<r8tor9
>();
28 private static final Set
<r8tor9
> fileguid
= new HashSet
<r8tor9
>();
29 private static final Set
<r8tor9
> fileppi
= new HashSet
<r8tor9
>();
30 private static final Set
<r8tor9
> fileprotocol
= new HashSet
<r8tor9
>();
31 private static final Set
<String
> filer8only
= new HashSet
<String
>();
33 //---------------------------------------inner classes---------------------------------------//
34 private static class r8tor9
{
35 r8tor9(String r8
, String r9
) {
39 public String r8thing
;
40 public String r9thing
;
43 private class IdleLaplace
extends Common
.Laplace
{
44 public String
operation(String wholeline
) {
48 public boolean recognize(String filename
) {
49 return filename
.contains(".h") || filename
.contains(".H") || filename
.contains(".uni");
52 public String
namechange(String oldname
) {
53 if (oldname
.contains(".H")) {
54 return oldname
.replaceFirst(".H", ".h");
60 private class DxsLaplace
extends Common
.Laplace
{
61 public String
operation(String wholeline
) {
62 if (mi
.getModuleType().equals("PEIM")) {
63 return addincludefile(wholeline
, "\\<PeimDepex.h\\>");
65 return addincludefile(wholeline
, "\\<DxeDepex.h\\>");
69 public boolean recognize(String filename
) {
70 return filename
.contains(".dxs");
73 public String
namechange(String oldname
) {
78 private class CLaplace
extends Common
.Laplace
{
79 public String
operation(String wholeline
) {
80 boolean addr8
= false;
82 Pattern pat
= Pattern
.compile("g?(BS|RT)(\\s*->\\s*)([a-zA-Z_]\\w*)", Pattern
.MULTILINE
); // ! only two level () bracket allowed !
83 //Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*[#$]*)(\\s*\\(([^\\(\\)]*(\\([^\\(\\)]*\\))?[^\\(\\)]*)*\\))", Pattern.MULTILINE);
85 // replace BS -> gBS , RT -> gRT
86 Matcher mat
= pat
.matcher(wholeline
);
87 if (mat
.find()) { // add a library here
88 MigrationTool
.ui
.println("Converting all BS->gBS, RT->gRT");
89 wholeline
= mat
.replaceAll("g$1$2$3"); //unknown correctiveness
93 if (mat
.group(1).matches("BS")) {
94 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib");
96 if (mat
.group(1).matches("RT")) {
97 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib");
100 // remove EFI_DRIVER_ENTRY_POINT
101 wholeline
= wholeline
.replaceAll("(EFI_\\w+_ENTRY_POINT)", MigrationTool
.MIGRATIONCOMMENT
+ " $1");
103 // start replacing names
107 // Converting non-locla function
108 it
= mi
.hashnonlocalfunc
.iterator();
109 while (it
.hasNext()) {
111 if (r8thing
.matches("EfiInitializeDriverLib")) { //s
112 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib"); //p
113 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib"); //e
114 } else if (r8thing
.matches("DxeInitializeDriverLib")) { //c
115 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib"); //i
116 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib"); //a
117 mi
.hashrequiredr9libs
.add("DxeServicesTableLib"); //l
119 mi
.hashrequiredr9libs
.add(MigrationTool
.db
.getR9Lib(r8thing
)); // add a library here
123 if ((r9thing
= MigrationTool
.db
.getR9Func(r8thing
)) != null) {
124 if (!r8thing
.equals(r9thing
)) {
125 if (wholeline
.contains(r8thing
)) {
126 wholeline
= wholeline
.replaceAll(r8thing
, r9thing
);
127 filefunc
.add(new r8tor9(r8thing
, r9thing
));
128 Iterator
<r8tor9
> rt
= filefunc
.iterator();
129 while (rt
.hasNext()) {
131 if (MigrationTool
.db
.r8only
.contains(temp
.r8thing
)) {
132 filer8only
.add(r8thing
);
133 mi
.hashr8only
.add(r8thing
);
140 } //is any of the guids changed?
142 wholeline
= addincludefile(wholeline
, "\"R8Lib.h\"");
146 it
= mi
.hashnonlocalmacro
.iterator();
147 while (it
.hasNext()) { //macros are all assumed MdePkg currently
149 //mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing));
150 if ((r9thing
= MigrationTool
.db
.getR9Macro(r8thing
)) != null) {
151 if (wholeline
.contains(r8thing
)) {
152 wholeline
= wholeline
.replaceAll(r8thing
, r9thing
);
153 filemacro
.add(new r8tor9(r8thing
, r9thing
));
159 replaceGuid(wholeline
, mi
.guid
, "guid", fileguid
);
160 replaceGuid(wholeline
, mi
.ppi
, "ppi", fileppi
);
161 replaceGuid(wholeline
, mi
.protocol
, "protocol", fileprotocol
);
164 // First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #%
165 Pattern ptnpei
= Pattern
.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern
.MULTILINE
);
166 if (mi
.moduletype
.contains("PEIM")) {
167 Matcher mtrpei
= ptnpei
.matcher(wholeline
);
168 while (mtrpei
.find()) { // ! add a library here !
169 wholeline
= mtrpei
.replaceAll("PeiServices$1#%$2");
170 mi
.hashrequiredr9libs
.add("PeiServicesLib");
173 if (wholeline
.contains("PeiServicesCopyMem")) {
174 wholeline
= wholeline
.replaceAll("PeiServicesCopyMem#%", "CopyMem");
175 mi
.hashrequiredr9libs
.add("BaseMemoryLib");
177 if (wholeline
.contains("PeiServicesSetMem")) {
178 wholeline
= wholeline
.replaceAll("PeiServicesSetMem#%", "SetMem");
179 mi
.hashrequiredr9libs
.add("BaseMemoryLib");
182 // Second , find all #% to drop the arg "PeiServices"
183 Pattern ptnpeiarg
= Pattern
.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern
.MULTILINE
);
184 Matcher mtrpeiarg
= ptnpeiarg
.matcher(wholeline
);
185 while (mtrpeiarg
.find()) {
186 wholeline
= mtrpeiarg
.replaceAll("$1");
191 mtrmac
= Pattern
.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(wholeline
);
193 wholeline
= mtrmac
.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");
195 mtrmac
= Pattern
.compile("EFI_MIN\\((.*), (.*)\\)").matcher(wholeline
);
197 wholeline
= mtrmac
.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");
199 mtrmac
= Pattern
.compile("EFI_MAX\\((.*), (.*)\\)").matcher(wholeline
);
201 wholeline
= mtrmac
.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");
203 mtrmac
= Pattern
.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(wholeline
);
205 wholeline
= mtrmac
.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");
207 if (wholeline
.contains("EFI_UINTN_ALIGN_MASK")) {
208 wholeline
= wholeline
.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");
211 show(filefunc
, "function");
212 show(filemacro
, "macro");
213 show(fileguid
, "guid");
214 show(fileppi
, "ppi");
215 show(fileprotocol
, "protocol");
216 if (!filer8only
.isEmpty()) {
217 MigrationTool
.ui
.println("Converting r8only : " + filer8only
);
224 fileprotocol
.clear();
230 public boolean recognize(String filename
) {
231 return filename
.contains(".c") || filename
.contains(".C");
234 public String
namechange(String oldname
) {
235 if (oldname
.contains(".C")) {
236 return oldname
.replaceFirst(".C", ".c");
242 //---------------------------------------inner classes---------------------------------------//
244 private static final String
addincludefile(String wholeline
, String hfile
) {
245 return wholeline
.replaceFirst("(\\*/\\s)", "$1\n#include " + hfile
+ "\n");
248 private static final void show(Set
<r8tor9
> hash
, String sh
) {
249 Iterator
<r8tor9
> it
= hash
.iterator();
251 if (!hash
.isEmpty()) {
252 MigrationTool
.ui
.print("Converting " + sh
+ " : ");
253 while (it
.hasNext()) {
255 MigrationTool
.ui
.print("[" + temp
.r8thing
+ "->" + temp
.r9thing
+ "] ");
257 MigrationTool
.ui
.println("");
261 private static final void replaceGuid(String line
, Set
<String
> hash
, String kind
, Set
<r8tor9
> filehash
) {
265 it
= hash
.iterator();
266 while (it
.hasNext()) {
268 if ((r9thing
= MigrationTool
.db
.getR9Guidname(r8thing
)) != null) {
269 if (!r8thing
.equals(r9thing
)) {
270 if (line
.contains(r8thing
)) {
271 line
= line
.replaceAll(r8thing
, r9thing
);
272 filehash
.add(new r8tor9(r8thing
, r9thing
));
279 private final void addr8only() throws Exception
{
280 String paragraph
= null;
281 String line
= Common
.file2string(MigrationTool
.db
.DatabasePath
+ File
.separator
+ "R8Lib.c");
282 PrintWriter outfile1
= new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool
.ModuleInfoMap
.get(mi
) + File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ "R8Lib.c")));
283 PrintWriter outfile2
= new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool
.ModuleInfoMap
.get(mi
) + File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ "R8Lib.h")));
284 Pattern ptnr8only
= Pattern
.compile("////#?(\\w*)?.*?R8_(\\w*).*?////~", Pattern
.DOTALL
);
285 Matcher mtrr8only
= ptnr8only
.matcher(line
);
286 Matcher mtrr8onlyhead
;
287 while (mtrr8only
.find()) {
288 if (mi
.hashr8only
.contains(mtrr8only
.group(2))) {
289 paragraph
= mtrr8only
.group();
290 outfile1
.append(paragraph
+ "\n\n");
291 if (mtrr8only
.group(1).length() != 0) {
292 mi
.hashrequiredr9libs
.add(mtrr8only
.group(1));
295 while ((mtrr8onlyhead
= Func
.ptnbrace
.matcher(paragraph
)).find()) {
296 paragraph
= mtrr8onlyhead
.replaceAll(";");
298 outfile2
.append(paragraph
+ "\n\n");
306 mi
.localmodulesources
.add("R8Lib.h");
307 mi
.localmodulesources
.add("R8Lib.c");
310 //-----------------------------------ForDoAll-----------------------------------//
311 public void run(String filepath
) throws Exception
{
312 String inname
= filepath
.replace(mi
.modulepath
+ File
.separator
, "");
313 String tempinpath
= mi
.modulepath
+ File
.separator
+ "temp" + File
.separator
;
314 String tempoutpath
= MigrationTool
.ModuleInfoMap
.get(mi
) + File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
;
316 Iterator
<Common
.Laplace
> itLaplace
= Laplaces
.iterator();
317 while (itLaplace
.hasNext()) {
318 Common
.Laplace lap
= itLaplace
.next();
319 if (lap
.recognize(inname
)) {
320 MigrationTool
.ui
.println("\nHandling file: " + inname
);
321 lap
.transform(tempinpath
+ inname
, tempoutpath
+ lap
.namechange(inname
));
326 public boolean filter(File dir
) {
329 //-----------------------------------ForDoAll-----------------------------------//
331 private final void setModuleInfo(ModuleInfo moduleinfo
) {
335 private final void start() throws Exception
{
336 Laplaces
.add(new DxsLaplace());
337 Laplaces
.add(new CLaplace());
338 Laplaces
.add(new IdleLaplace());
340 Common
.toDoAll(mi
.localmodulesources
, this);
342 if (!mi
.hashr8only
.isEmpty()) {
349 public static final void fireAt(ModuleInfo moduleinfo
) throws Exception
{
350 SFReplacer
.setModuleInfo(moduleinfo
);