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 private static final String
[] specialhoblibfunc
= {
35 "BuildResourceDescriptorHob",
40 "BuildMemoryAllocationHob"
43 //---------------------------------------inner classes---------------------------------------//
44 private static class r8tor9
{
45 r8tor9(String r8
, String r9
) {
49 public String r8thing
;
50 public String r9thing
;
53 private class IdleLaplace
extends Common
.Laplace
{
54 public String
operation(String wholeline
) {
58 public boolean recognize(String filename
) {
59 return filename
.contains(".h") || filename
.contains(".H") || filename
.contains(".uni");
62 public String
namechange(String oldname
) {
63 if (oldname
.contains(".H")) {
64 return oldname
.replaceFirst(".H", ".h");
70 private class DxsLaplace
extends Common
.Laplace
{
71 public String
operation(String wholeline
) {
72 if (mi
.getModuleType().equals("PEIM")) {
73 return addincludefile(wholeline
, "\\<PeimDepex.h\\>");
75 return addincludefile(wholeline
, "\\<DxeDepex.h\\>");
79 public boolean recognize(String filename
) {
80 return filename
.contains(".dxs");
83 public String
namechange(String oldname
) {
88 private class CLaplace
extends Common
.Laplace
{
89 public String
operation(String wholeline
) {
90 boolean addr8
= false;
92 Pattern pat
= Pattern
.compile("g?(BS|RT)(\\s*->\\s*)([a-zA-Z_]\\w*)", Pattern
.MULTILINE
); // ! only two level () bracket allowed !
93 //Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*[#$]*)(\\s*\\(([^\\(\\)]*(\\([^\\(\\)]*\\))?[^\\(\\)]*)*\\))", Pattern.MULTILINE);
95 // replace BS -> gBS , RT -> gRT
96 Matcher mat
= pat
.matcher(wholeline
);
97 if (mat
.find()) { // add a library here
98 MigrationTool
.ui
.println("Converting all BS->gBS, RT->gRT");
99 wholeline
= mat
.replaceAll("g$1$2$3"); //unknown correctiveness
103 if (mat
.group(1).matches("BS")) {
104 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib");
106 if (mat
.group(1).matches("RT")) {
107 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib");
110 // remove EFI_DRIVER_ENTRY_POINT
111 wholeline
= wholeline
.replaceAll("(EFI_\\w+_ENTRY_POINT)", MigrationTool
.MIGRATIONCOMMENT
+ " $1");
113 // start replacing names
117 // Converting non-locla function
118 it
= mi
.hashnonlocalfunc
.iterator();
119 while (it
.hasNext()) {
121 if (r8thing
.matches("EfiInitializeDriverLib")) { //s
122 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib"); //p
123 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib"); //e
124 } else if (r8thing
.matches("DxeInitializeDriverLib")) { //c
125 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib"); //i
126 mi
.hashrequiredr9libs
.add("UefiRuntimeServicesTableLib"); //a
127 mi
.hashrequiredr9libs
.add("DxeServicesTableLib"); //l
129 mi
.hashrequiredr9libs
.add(MigrationTool
.db
.getR9Lib(r8thing
)); // add a library here
133 if ((r9thing
= MigrationTool
.db
.getR9Func(r8thing
)) != null) {
134 if (!r8thing
.equals(r9thing
)) {
135 if (wholeline
.contains(r8thing
)) {
136 wholeline
= wholeline
.replaceAll(r8thing
, r9thing
);
137 filefunc
.add(new r8tor9(r8thing
, r9thing
));
138 Iterator
<r8tor9
> rt
= filefunc
.iterator();
139 while (rt
.hasNext()) {
141 if (MigrationTool
.db
.r8only
.contains(temp
.r8thing
)) {
142 filer8only
.add(r8thing
);
143 mi
.hashr8only
.add(r8thing
);
150 } //is any of the guids changed?
152 wholeline
= addincludefile(wholeline
, "\"R8Lib.h\"");
156 it
= mi
.hashnonlocalmacro
.iterator();
157 while (it
.hasNext()) { //macros are all assumed MdePkg currently
159 //mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing));
160 if ((r9thing
= MigrationTool
.db
.getR9Macro(r8thing
)) != null) {
161 if (wholeline
.contains(r8thing
)) {
162 wholeline
= wholeline
.replaceAll(r8thing
, r9thing
);
163 filemacro
.add(new r8tor9(r8thing
, r9thing
));
169 replaceGuid(wholeline
, mi
.guid
, "guid", fileguid
);
170 replaceGuid(wholeline
, mi
.ppi
, "ppi", fileppi
);
171 replaceGuid(wholeline
, mi
.protocol
, "protocol", fileprotocol
);
174 // First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #%
175 Pattern ptnpei
= Pattern
.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern
.MULTILINE
);
176 if (mi
.getModuleType().matches("PEIM")) {
177 //if (mi.moduletype.contains("PEIM")) {
178 Matcher mtrpei
= ptnpei
.matcher(wholeline
);
179 while (mtrpei
.find()) { // ! add a library here !
180 wholeline
= mtrpei
.replaceAll("PeiServices$1#%$2");
181 mi
.hashrequiredr9libs
.add("PeiServicesLib");
184 if (wholeline
.contains("PeiServicesCopyMem")) {
185 wholeline
= wholeline
.replaceAll("PeiServicesCopyMem#%", "CopyMem");
186 mi
.hashrequiredr9libs
.add("BaseMemoryLib");
188 if (wholeline
.contains("PeiServicesSetMem")) {
189 wholeline
= wholeline
.replaceAll("PeiServicesSetMem#%", "SetMem");
190 mi
.hashrequiredr9libs
.add("BaseMemoryLib");
193 // Second , find all #% to drop the arg "PeiServices"
194 Pattern ptnpeiarg
= Pattern
.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern
.MULTILINE
);
195 Matcher mtrpeiarg
= ptnpeiarg
.matcher(wholeline
);
196 while (mtrpeiarg
.find()) {
197 wholeline
= mtrpeiarg
.replaceAll("$1");
201 wholeline
= hobLibFuncDropStatus(wholeline
);
204 mtrmac
= Pattern
.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(wholeline
);
206 wholeline
= mtrmac
.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");
208 mtrmac
= Pattern
.compile("EFI_MIN\\((.*), (.*)\\)").matcher(wholeline
);
210 wholeline
= mtrmac
.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");
212 mtrmac
= Pattern
.compile("EFI_MAX\\((.*), (.*)\\)").matcher(wholeline
);
214 wholeline
= mtrmac
.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");
216 mtrmac
= Pattern
.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(wholeline
);
218 wholeline
= mtrmac
.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");
220 if (wholeline
.contains("EFI_UINTN_ALIGN_MASK")) {
221 wholeline
= wholeline
.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");
224 show(filefunc
, "function");
225 show(filemacro
, "macro");
226 show(fileguid
, "guid");
227 show(fileppi
, "ppi");
228 show(fileprotocol
, "protocol");
229 if (!filer8only
.isEmpty()) {
230 MigrationTool
.ui
.println("Converting r8only : " + filer8only
);
237 fileprotocol
.clear();
243 public boolean recognize(String filename
) {
244 return filename
.contains(".c") || filename
.contains(".C");
247 public String
namechange(String oldname
) {
248 if (oldname
.contains(".C")) {
249 return oldname
.replaceFirst(".C", ".c");
255 //---------------------------------------inner classes---------------------------------------//
257 //-------------------------------------process functions-------------------------------------//
258 private static final String
addincludefile(String wholeline
, String hfile
) {
259 return wholeline
.replaceFirst("(\\*/\\s)", "$1\n#include " + hfile
+ "\n");
262 private static final void show(Set
<r8tor9
> hash
, String sh
) {
263 Iterator
<r8tor9
> it
= hash
.iterator();
265 if (!hash
.isEmpty()) {
266 MigrationTool
.ui
.print("Converting " + sh
+ " : ");
267 while (it
.hasNext()) {
269 MigrationTool
.ui
.print("[" + temp
.r8thing
+ "->" + temp
.r9thing
+ "] ");
271 MigrationTool
.ui
.println("");
275 private static final void replaceGuid(String line
, Set
<String
> hash
, String kind
, Set
<r8tor9
> filehash
) {
279 it
= hash
.iterator();
280 while (it
.hasNext()) {
282 if ((r9thing
= MigrationTool
.db
.getR9Guidname(r8thing
)) != null) {
283 if (!r8thing
.equals(r9thing
)) {
284 if (line
.contains(r8thing
)) {
285 line
= line
.replaceAll(r8thing
, r9thing
);
286 filehash
.add(new r8tor9(r8thing
, r9thing
));
293 private final String
hobLibFuncDropStatus(String wholeline
) { // or use regex to find pattern "Status = ..."
294 Pattern ptnhobstatus
;
295 Matcher mtrhobstatus
;
296 String templine
= wholeline
;
297 for (int i
= 0; i
< specialhoblibfunc
.length
; i
++) {
298 ptnhobstatus
= Pattern
.compile("(Status\\s*=\\s*)?" + specialhoblibfunc
[i
] + "(.*?\\)\\s*;)", Pattern
.DOTALL
);
299 mtrhobstatus
= ptnhobstatus
.matcher(templine
);
300 if (mtrhobstatus
.find()) {
301 templine
= mtrhobstatus
.replaceAll(specialhoblibfunc
[i
] + mtrhobstatus
.group(2) + "\n //Migration comments: R9 Hob-building library functions will assert if build failure.\n Status = EFI_SUCCESS;");
307 private final void addr8only() throws Exception
{
308 String paragraph
= null;
309 String line
= Common
.file2string(MigrationTool
.db
.DatabasePath
+ File
.separator
+ "R8Lib.c");
310 PrintWriter outfile1
= new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool
.ModuleInfoMap
.get(mi
) + File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ "R8Lib.c")));
311 PrintWriter outfile2
= new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool
.ModuleInfoMap
.get(mi
) + File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ "R8Lib.h")));
312 Pattern ptnr8only
= Pattern
.compile("////#?(\\w*)?(.*?R8_(\\w*).*?)////~", Pattern
.DOTALL
);
313 Matcher mtrr8only
= ptnr8only
.matcher(line
);
314 Matcher mtrr8onlyhead
;
317 Matcher mtrr8onlyheadcomment
= Critic
.PTN_NEW_HEAD_COMMENT
.matcher(line
);
318 if (mtrr8onlyheadcomment
.find()) {
319 outfile1
.append(mtrr8onlyheadcomment
.group() + "\n\n");
320 outfile2
.append(mtrr8onlyheadcomment
.group() + "\n\n");
324 while (mtrr8only
.find()) {
325 if (mi
.hashr8only
.contains(mtrr8only
.group(3))) {
326 paragraph
= mtrr8only
.group(2);
327 outfile1
.append(paragraph
+ "\n\n");
328 if (mtrr8only
.group(1).length() != 0) {
329 mi
.hashrequiredr9libs
.add(mtrr8only
.group(1));
332 while ((mtrr8onlyhead
= Func
.ptnbrace
.matcher(paragraph
)).find()) {
333 paragraph
= mtrr8onlyhead
.replaceAll(";");
335 outfile2
.append(paragraph
+ "\n\n");
343 mi
.localmodulesources
.add("R8Lib.h");
344 mi
.localmodulesources
.add("R8Lib.c");
346 //-------------------------------------process functions-------------------------------------//
348 //-----------------------------------ForDoAll-----------------------------------//
349 public void run(String filepath
) throws Exception
{
350 String inname
= filepath
.replace(mi
.modulepath
+ File
.separator
, "");
351 String tempinpath
= mi
.modulepath
+ File
.separator
+ "temp" + File
.separator
;
352 String tempoutpath
= MigrationTool
.ModuleInfoMap
.get(mi
) + File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
;
354 Iterator
<Common
.Laplace
> itLaplace
= Laplaces
.iterator();
355 while (itLaplace
.hasNext()) {
356 Common
.Laplace lap
= itLaplace
.next();
357 if (lap
.recognize(inname
)) {
358 MigrationTool
.ui
.println("\nHandling file: " + inname
);
359 lap
.transform(tempinpath
+ inname
, tempoutpath
+ lap
.namechange(inname
));
364 public boolean filter(File dir
) {
367 //-----------------------------------ForDoAll-----------------------------------//
369 private final void setModuleInfo(ModuleInfo moduleinfo
) {
373 private final void start() throws Exception
{
374 Laplaces
.add(new DxsLaplace());
375 Laplaces
.add(new CLaplace());
376 Laplaces
.add(new IdleLaplace());
378 Common
.toDoAll(mi
.localmodulesources
, this);
380 if (!mi
.hashr8only
.isEmpty()) {
387 public static final void fireAt(ModuleInfo moduleinfo
) throws Exception
{
388 SFReplacer
.setModuleInfo(moduleinfo
);