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;
91 // remove EFI_DRIVER_ENTRY_POINT
92 wholeline
= wholeline
.replaceAll("(EFI_[A-Z]+_ENTRY_POINT\\s*\\(\\s*" + mi
.entrypoint
+ "\\s*\\)\\s*;)", MigrationTool
.MIGRATIONCOMMENT
+ " $1");
93 // redefine module entry point for some self-relocated modules
94 wholeline
= wholeline
.replaceAll (mi
.entrypoint
+ "([^{]*?})", "_ModuleEntryPoint" + "$1");
95 // remove R8 library contractor
96 wholeline
= wholeline
.replaceAll ("(\\b(?:Efi|Dxe)InitializeDriverLib\\b)", MigrationTool
.MIGRATIONCOMMENT
+ " $1");
97 // Add Library Class for potential reference of gBS, gRT & gDS.
98 if (Common
.find (wholeline
, "\\bg?BS\\b")) {
99 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib");
101 if (Common
.find (wholeline
, "\\bg?RT\\b")) {
102 mi
.hashrequiredr9libs
.add ("UefiRuntimeServicesTableLib");
104 if (Common
.find (wholeline
, "\\bgDS\\b")) {
105 mi
.hashrequiredr9libs
.add ("DxeServicesTableLib");
107 // start replacing names
111 // Converting non-locla function
112 it
= mi
.hashnonlocalfunc
.iterator();
113 while (it
.hasNext()) {
115 mi
.hashrequiredr9libs
.add(MigrationTool
.db
.getR9Lib(r8thing
)); // add a library here
118 if ((r9thing
= MigrationTool
.db
.getR9Func(r8thing
)) != null) {
119 if (!r8thing
.equals(r9thing
)) {
120 if (wholeline
.contains(r8thing
)) {
121 wholeline
= wholeline
.replaceAll(r8thing
, r9thing
);
122 filefunc
.add(new r8tor9(r8thing
, r9thing
));
123 Iterator
<r8tor9
> rt
= filefunc
.iterator();
124 while (rt
.hasNext()) {
126 if (MigrationTool
.db
.r8only
.contains(temp
.r8thing
)) {
127 filer8only
.add(r8thing
);
128 mi
.hashr8only
.add(r8thing
);
135 } //is any of the guids changed?
137 wholeline
= addincludefile(wholeline
, "\"R8Lib.h\"");
141 it
= mi
.hashnonlocalmacro
.iterator();
142 while (it
.hasNext()) { //macros are all assumed MdePkg currently
144 //mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing));
145 if ((r9thing
= MigrationTool
.db
.getR9Macro(r8thing
)) != null) {
146 if (wholeline
.contains(r8thing
)) {
147 wholeline
= wholeline
.replaceAll(r8thing
, r9thing
);
148 filemacro
.add(new r8tor9(r8thing
, r9thing
));
154 replaceGuid(wholeline
, mi
.guid
, "guid", fileguid
);
155 replaceGuid(wholeline
, mi
.ppi
, "ppi", fileppi
);
156 replaceGuid(wholeline
, mi
.protocol
, "protocol", fileprotocol
);
159 // First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #%
160 Pattern ptnpei
= Pattern
.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern
.MULTILINE
);
161 if (mi
.getModuleType().matches("PEIM")) {
162 //if (mi.moduletype.contains("PEIM")) {
163 Matcher mtrpei
= ptnpei
.matcher(wholeline
);
164 while (mtrpei
.find()) { // ! add a library here !
165 wholeline
= mtrpei
.replaceAll("PeiServices$1#%$2");
166 mi
.hashrequiredr9libs
.add("PeiServicesLib");
169 if (wholeline
.contains("PeiServicesCopyMem")) {
170 wholeline
= wholeline
.replaceAll("PeiServicesCopyMem#%", "CopyMem");
171 mi
.hashrequiredr9libs
.add("BaseMemoryLib");
173 if (wholeline
.contains("PeiServicesSetMem")) {
174 wholeline
= wholeline
.replaceAll("PeiServicesSetMem#%", "SetMem");
175 mi
.hashrequiredr9libs
.add("BaseMemoryLib");
178 // Second , find all #% to drop the arg "PeiServices"
179 Pattern ptnpeiarg
= Pattern
.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern
.MULTILINE
);
180 Matcher mtrpeiarg
= ptnpeiarg
.matcher(wholeline
);
181 while (mtrpeiarg
.find()) {
182 wholeline
= mtrpeiarg
.replaceAll("$1");
186 wholeline
= hobLibFuncDropStatus(wholeline
);
189 mtrmac
= Pattern
.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(wholeline
);
191 wholeline
= mtrmac
.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");
193 mtrmac
= Pattern
.compile("EFI_MIN\\((.*), (.*)\\)").matcher(wholeline
);
195 wholeline
= mtrmac
.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");
197 mtrmac
= Pattern
.compile("EFI_MAX\\((.*), (.*)\\)").matcher(wholeline
);
199 wholeline
= mtrmac
.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");
201 mtrmac
= Pattern
.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(wholeline
);
203 wholeline
= mtrmac
.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");
205 if (wholeline
.contains("EFI_UINTN_ALIGN_MASK")) {
206 wholeline
= wholeline
.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");
209 show(filefunc
, "function");
210 show(filemacro
, "macro");
211 show(fileguid
, "guid");
212 show(fileppi
, "ppi");
213 show(fileprotocol
, "protocol");
214 if (!filer8only
.isEmpty()) {
215 MigrationTool
.ui
.println("Converting r8only : " + filer8only
);
222 fileprotocol
.clear();
228 public boolean recognize(String filename
) {
229 return filename
.contains(".c") || filename
.contains(".C");
232 public String
namechange(String oldname
) {
233 if (oldname
.contains(".C")) {
234 return oldname
.replaceFirst(".C", ".c");
240 //---------------------------------------inner classes---------------------------------------//
242 //-------------------------------------process functions-------------------------------------//
243 private static final String
addincludefile(String wholeline
, String hfile
) {
244 return wholeline
.replaceFirst("(\\*/\\s)", "$1\n#include " + hfile
+ "\n");
247 private static final void show(Set
<r8tor9
> hash
, String sh
) {
248 Iterator
<r8tor9
> it
= hash
.iterator();
250 if (!hash
.isEmpty()) {
251 MigrationTool
.ui
.print("Converting " + sh
+ " : ");
252 while (it
.hasNext()) {
254 MigrationTool
.ui
.print("[" + temp
.r8thing
+ "->" + temp
.r9thing
+ "] ");
256 MigrationTool
.ui
.println("");
260 private static final void replaceGuid(String line
, Set
<String
> hash
, String kind
, Set
<r8tor9
> filehash
) {
264 it
= hash
.iterator();
265 while (it
.hasNext()) {
267 if ((r9thing
= MigrationTool
.db
.getR9Guidname(r8thing
)) != null) {
268 if (!r8thing
.equals(r9thing
)) {
269 if (line
.contains(r8thing
)) {
270 line
= line
.replaceAll(r8thing
, r9thing
);
271 filehash
.add(new r8tor9(r8thing
, r9thing
));
278 private final String
hobLibFuncDropStatus(String wholeline
) { // or use regex to find pattern "Status = ..."
279 Pattern ptnhobstatus
;
280 Matcher mtrhobstatus
;
281 String templine
= wholeline
;
282 for (int i
= 0; i
< specialhoblibfunc
.length
; i
++) {
283 ptnhobstatus
= Pattern
.compile("(Status\\s*=\\s*)?" + specialhoblibfunc
[i
] + "(.*?\\)\\s*;)", Pattern
.DOTALL
);
284 mtrhobstatus
= ptnhobstatus
.matcher(templine
);
285 if (mtrhobstatus
.find()) {
286 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;");
292 private final void addr8only() throws Exception
{
293 String paragraph
= null;
294 String line
= Common
.file2string(MigrationTool
.db
.DatabasePath
+ File
.separator
+ "R8Lib.c");
295 PrintWriter outfile1
= new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool
.ModuleInfoMap
.get(mi
) + File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ "R8Lib.c")));
296 PrintWriter outfile2
= new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool
.ModuleInfoMap
.get(mi
) + File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ "R8Lib.h")));
297 Pattern ptnr8only
= Pattern
.compile("////#?(\\w*)?(.*?R8_(\\w*).*?)////~", Pattern
.DOTALL
);
298 Matcher mtrr8only
= ptnr8only
.matcher(line
);
299 Matcher mtrr8onlyhead
;
302 Matcher mtrr8onlyheadcomment
= Critic
.PTN_NEW_HEAD_COMMENT
.matcher(line
);
303 if (mtrr8onlyheadcomment
.find()) {
304 outfile1
.append(mtrr8onlyheadcomment
.group() + "\n\n");
305 outfile2
.append(mtrr8onlyheadcomment
.group() + "\n\n");
309 while (mtrr8only
.find()) {
310 if (mi
.hashr8only
.contains(mtrr8only
.group(3))) {
311 paragraph
= mtrr8only
.group(2);
312 outfile1
.append(paragraph
+ "\n\n");
313 if (mtrr8only
.group(1).length() != 0) {
314 mi
.hashrequiredr9libs
.add(mtrr8only
.group(1));
317 while ((mtrr8onlyhead
= Func
.ptnbrace
.matcher(paragraph
)).find()) {
318 paragraph
= mtrr8onlyhead
.replaceAll(";");
320 outfile2
.append(paragraph
+ "\n\n");
328 mi
.localmodulesources
.add("R8Lib.h");
329 mi
.localmodulesources
.add("R8Lib.c");
331 //-------------------------------------process functions-------------------------------------//
333 //-----------------------------------ForDoAll-----------------------------------//
334 public void run(String filepath
) throws Exception
{
335 String inname
= filepath
.replace(mi
.modulepath
+ File
.separator
, "");
336 String tempinpath
= mi
.modulepath
+ File
.separator
+ "temp" + File
.separator
;
337 String tempoutpath
= MigrationTool
.ModuleInfoMap
.get(mi
) + File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
;
339 Iterator
<Common
.Laplace
> itLaplace
= Laplaces
.iterator();
340 while (itLaplace
.hasNext()) {
341 Common
.Laplace lap
= itLaplace
.next();
342 if (lap
.recognize(inname
)) {
343 MigrationTool
.ui
.println("\nHandling file: " + inname
);
344 lap
.transform(tempinpath
+ inname
, tempoutpath
+ lap
.namechange(inname
));
349 public boolean filter(File dir
) {
352 //-----------------------------------ForDoAll-----------------------------------//
354 private final void setModuleInfo(ModuleInfo moduleinfo
) {
358 private final void start() throws Exception
{
359 Laplaces
.add(new DxsLaplace());
360 Laplaces
.add(new CLaplace());
361 Laplaces
.add(new IdleLaplace());
363 Common
.toDoAll(mi
.localmodulesources
, this);
365 if (!mi
.hashr8only
.isEmpty()) {
372 public static final void fireAt(ModuleInfo moduleinfo
) throws Exception
{
373 SFReplacer
.setModuleInfo(moduleinfo
);