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",
41 "BuildMemoryAllocationHob"
44 //---------------------------------------inner classes---------------------------------------//
45 private static class r8tor9
{
46 r8tor9(String r8
, String r9
) {
50 public String r8thing
;
51 public String r9thing
;
54 private class IdleLaplace
extends Common
.Laplace
{
55 public String
operation(String wholeline
) {
59 public boolean recognize(String filename
) {
60 return filename
.contains(".h") || filename
.contains(".H") || filename
.contains(".uni");
63 public String
namechange(String oldname
) {
64 if (oldname
.contains(".H")) {
65 return oldname
.replaceFirst(".H", ".h");
71 private class DxsLaplace
extends Common
.Laplace
{
72 public String
operation(String wholeline
) {
73 if (mi
.getModuleType().equals("PEIM")) {
74 return addincludefile(wholeline
, "\\<PeimDepex.h\\>");
76 return addincludefile(wholeline
, "\\<DxeDepex.h\\>");
80 public boolean recognize(String filename
) {
81 return filename
.contains(".dxs");
84 public String
namechange(String oldname
) {
89 private class CLaplace
extends Common
.Laplace
{
90 public String
operation(String wholeline
) {
91 boolean addr8
= false;
92 // remove EFI_DRIVER_ENTRY_POINT
93 wholeline
= wholeline
.replaceAll("(EFI_[A-Z]+_ENTRY_POINT\\s*\\(\\s*" + mi
.entrypoint
+ "\\s*\\)\\s*;)", MigrationTool
.MIGRATIONCOMMENT
+ " $1");
94 // redefine module entry point for some self-relocated modules
95 wholeline
= wholeline
.replaceAll (mi
.entrypoint
+ "([^{]*?})", "_ModuleEntryPoint" + "$1");
96 // remove R8 library contractor
97 wholeline
= wholeline
.replaceAll ("(\\b(?:Efi|Dxe)InitializeDriverLib\\b)", MigrationTool
.MIGRATIONCOMMENT
+ " $1");
98 // Add Library Class for potential reference of gBS, gRT & gDS.
99 if (Common
.find (wholeline
, "\\bg?BS\\b")) {
100 mi
.hashrequiredr9libs
.add("UefiBootServicesTableLib");
102 if (Common
.find (wholeline
, "\\bg?RT\\b")) {
103 mi
.hashrequiredr9libs
.add ("UefiRuntimeServicesTableLib");
105 if (Common
.find (wholeline
, "\\bgDS\\b")) {
106 mi
.hashrequiredr9libs
.add ("DxeServicesTableLib");
108 // start replacing names
112 // Converting non-locla function
113 it
= mi
.hashnonlocalfunc
.iterator();
114 while (it
.hasNext()) {
116 mi
.hashrequiredr9libs
.add(MigrationTool
.db
.getR9Lib(r8thing
)); // add a library here
119 if ((r9thing
= MigrationTool
.db
.getR9Func(r8thing
)) != null) {
120 if (!r8thing
.equals(r9thing
)) {
121 if (wholeline
.contains(r8thing
)) {
122 wholeline
= wholeline
.replaceAll(r8thing
, r9thing
);
123 filefunc
.add(new r8tor9(r8thing
, r9thing
));
124 Iterator
<r8tor9
> rt
= filefunc
.iterator();
125 while (rt
.hasNext()) {
127 if (MigrationTool
.db
.r8only
.contains(temp
.r8thing
)) {
128 filer8only
.add(r8thing
);
129 mi
.hashr8only
.add(r8thing
);
136 } //is any of the guids changed?
138 wholeline
= addincludefile(wholeline
, "\"R8Lib.h\"");
142 it
= mi
.hashnonlocalmacro
.iterator();
143 while (it
.hasNext()) { //macros are all assumed MdePkg currently
145 //mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing));
146 if ((r9thing
= MigrationTool
.db
.getR9Macro(r8thing
)) != null) {
147 if (wholeline
.contains(r8thing
)) {
148 wholeline
= wholeline
.replaceAll(r8thing
, r9thing
);
149 filemacro
.add(new r8tor9(r8thing
, r9thing
));
155 replaceGuid(wholeline
, mi
.guid
, "guid", fileguid
);
156 replaceGuid(wholeline
, mi
.ppi
, "ppi", fileppi
);
157 replaceGuid(wholeline
, mi
.protocol
, "protocol", fileprotocol
);
160 // First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #%
161 Pattern ptnpei
= Pattern
.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern
.MULTILINE
);
162 if (mi
.getModuleType().matches("PEIM")) {
163 //if (mi.moduletype.contains("PEIM")) {
164 Matcher mtrpei
= ptnpei
.matcher(wholeline
);
165 while (mtrpei
.find()) { // ! add a library here !
166 wholeline
= mtrpei
.replaceAll("PeiServices$1#%$2");
167 mi
.hashrequiredr9libs
.add("PeiServicesLib");
170 if (wholeline
.contains("PeiServicesCopyMem")) {
171 wholeline
= wholeline
.replaceAll("PeiServicesCopyMem#%", "CopyMem");
172 mi
.hashrequiredr9libs
.add("BaseMemoryLib");
174 if (wholeline
.contains("PeiServicesSetMem")) {
175 wholeline
= wholeline
.replaceAll("PeiServicesSetMem#%", "SetMem");
176 mi
.hashrequiredr9libs
.add("BaseMemoryLib");
179 // Second , find all #% to drop the arg "PeiServices"
180 Pattern ptnpeiarg
= Pattern
.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern
.MULTILINE
);
181 Matcher mtrpeiarg
= ptnpeiarg
.matcher(wholeline
);
182 while (mtrpeiarg
.find()) {
183 wholeline
= mtrpeiarg
.replaceAll("$1");
187 wholeline
= hobLibFuncDropStatus(wholeline
);
190 mtrmac
= Pattern
.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(wholeline
);
192 wholeline
= mtrmac
.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");
194 mtrmac
= Pattern
.compile("EFI_MIN\\((.*), (.*)\\)").matcher(wholeline
);
196 wholeline
= mtrmac
.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");
198 mtrmac
= Pattern
.compile("EFI_MAX\\((.*), (.*)\\)").matcher(wholeline
);
200 wholeline
= mtrmac
.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");
202 mtrmac
= Pattern
.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(wholeline
);
204 wholeline
= mtrmac
.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");
206 if (wholeline
.contains("EFI_UINTN_ALIGN_MASK")) {
207 wholeline
= wholeline
.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");
210 show(filefunc
, "function");
211 show(filemacro
, "macro");
212 show(fileguid
, "guid");
213 show(fileppi
, "ppi");
214 show(fileprotocol
, "protocol");
215 if (!filer8only
.isEmpty()) {
216 MigrationTool
.ui
.println("Converting r8only : " + filer8only
);
223 fileprotocol
.clear();
229 public boolean recognize(String filename
) {
230 return filename
.contains(".c") || filename
.contains(".C");
233 public String
namechange(String oldname
) {
234 if (oldname
.contains(".C")) {
235 return oldname
.replaceFirst(".C", ".c");
241 //---------------------------------------inner classes---------------------------------------//
243 //-------------------------------------process functions-------------------------------------//
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 String
hobLibFuncDropStatus(String wholeline
) { // or use regex to find pattern "Status = ..."
280 Pattern ptnhobstatus
;
281 Matcher mtrhobstatus
;
282 String templine
= wholeline
;
283 for (int i
= 0; i
< specialhoblibfunc
.length
; i
++) {
284 ptnhobstatus
= Pattern
.compile("(Status\\s*=\\s*)?" + specialhoblibfunc
[i
] + "(.*?\\)\\s*;)", Pattern
.DOTALL
);
285 mtrhobstatus
= ptnhobstatus
.matcher(templine
);
286 if (mtrhobstatus
.find()) {
287 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;");
293 private final void addr8only() throws Exception
{
294 String paragraph
= null;
295 String line
= Common
.file2string(MigrationTool
.db
.DatabasePath
+ File
.separator
+ "R8Lib.c");
296 PrintWriter outfile1
= new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool
.ModuleInfoMap
.get(mi
) + File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ "R8Lib.c")));
297 PrintWriter outfile2
= new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool
.ModuleInfoMap
.get(mi
) + File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ "R8Lib.h")));
298 Pattern ptnr8only
= Pattern
.compile("////#?(\\w*)?(.*?R8_(\\w*).*?)////~", Pattern
.DOTALL
);
299 Matcher mtrr8only
= ptnr8only
.matcher(line
);
300 Matcher mtrr8onlyhead
;
303 Matcher mtrr8onlyheadcomment
= Critic
.PTN_NEW_HEAD_COMMENT
.matcher(line
);
304 if (mtrr8onlyheadcomment
.find()) {
305 outfile1
.append(mtrr8onlyheadcomment
.group() + "\n\n");
306 outfile2
.append(mtrr8onlyheadcomment
.group() + "\n\n");
310 while (mtrr8only
.find()) {
311 if (mi
.hashr8only
.contains(mtrr8only
.group(3))) {
312 paragraph
= mtrr8only
.group(2);
313 outfile1
.append(paragraph
+ "\n\n");
314 if (mtrr8only
.group(1).length() != 0) {
315 mi
.hashrequiredr9libs
.add(mtrr8only
.group(1));
318 while ((mtrr8onlyhead
= Func
.ptnbrace
.matcher(paragraph
)).find()) {
319 paragraph
= mtrr8onlyhead
.replaceAll(";");
321 outfile2
.append(paragraph
+ "\n\n");
329 mi
.localmodulesources
.add("R8Lib.h");
330 mi
.localmodulesources
.add("R8Lib.c");
332 //-------------------------------------process functions-------------------------------------//
334 //-----------------------------------ForDoAll-----------------------------------//
335 public void run(String filepath
) throws Exception
{
336 String inname
= filepath
.replace(mi
.modulepath
+ File
.separator
, "");
337 String tempinpath
= mi
.modulepath
+ File
.separator
+ "temp" + File
.separator
;
338 String tempoutpath
= MigrationTool
.ModuleInfoMap
.get(mi
) + File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
;
340 Iterator
<Common
.Laplace
> itLaplace
= Laplaces
.iterator();
341 while (itLaplace
.hasNext()) {
342 Common
.Laplace lap
= itLaplace
.next();
343 if (lap
.recognize(inname
)) {
344 MigrationTool
.ui
.println("\nHandling file: " + inname
);
345 lap
.transform(tempinpath
+ inname
, tempoutpath
+ lap
.namechange(inname
));
350 public boolean filter(File dir
) {
353 //-----------------------------------ForDoAll-----------------------------------//
355 private final void setModuleInfo(ModuleInfo moduleinfo
) {
359 private final void start() throws Exception
{
360 Laplaces
.add(new DxsLaplace());
361 Laplaces
.add(new CLaplace());
362 Laplaces
.add(new IdleLaplace());
364 Common
.toDoAll(mi
.localmodulesources
, this);
366 if (!mi
.hashr8only
.isEmpty()) {
373 public static final void fireAt(ModuleInfo moduleinfo
) throws Exception
{
374 SFReplacer
.setModuleInfo(moduleinfo
);