1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "mir_parser.h"
17 #include "debug_info.h"
18 #include "bin_mplt.h"
19 #include "opcode_info.h"
20 #include <cstdlib>
21 #include "mir_function.h"
22 #include "mir_type.h"
23 #include <iostream>
24 #include <fstream>
25
26 using namespace maple;
27
28 std::unordered_set<std::string> dumpFuncSet = {};
29
main(int argc,char ** argv)30 int main(int argc, char **argv)
31 {
32 if (argc < k2BitSize) {
33 MIR_PRINTF("usage: mpldbg foo.mpl\n");
34 exit(1);
35 }
36 std::vector<maple::MIRModule *> themodule(argc, nullptr);
37 bool useBinary = false;
38 MIRSrcLang srcLang = kSrcLangUnknown;
39 // process the options which must come first
40 maple::int32 i = 1;
41 while (argv[i][0] == '-') {
42 if (argv[i][1] == 'b' && argv[i][k2BitSize] == '\0') {
43 useBinary = true;
44 } else if (strncmp(argv[i], "-dumpfunc=", k10BitSize) == 0 && strlen(argv[i]) > k10BitSize) {
45 std::string funcName(&argv[i][k10BitSize]);
46 dumpFuncSet.insert(funcName);
47 } else if (strcmp(argv[i], "-srclang=c") == 0) {
48 srcLang = kSrcLangC;
49 } else if (strcmp(argv[i], "-srclang=c++") == 0) {
50 srcLang = kSrcLangCPlusPlus;
51 } else {
52 ERR(kLncErr, "mpldbg: unrecognized command line option");
53 return 1;
54 }
55 i++;
56 }
57 // process the input files
58 while (i < argc) {
59 themodule[i] = new maple::MIRModule(argv[i]);
60 themodule[i]->SetSrcLang(srcLang);
61 std::string::size_type lastdot = themodule[i]->GetFileName().find_last_of(".");
62 bool ismplt = themodule[i]->GetFileName().compare(lastdot, k5BitSize, ".mplt") == 0;
63 bool istmpl = themodule[i]->GetFileName().compare(lastdot, k5BitSize, ".tmpl") == 0;
64 bool ismpl = themodule[i]->GetFileName().compare(lastdot, k5BitSize, ".mpl\0") == 0;
65 bool isbpl = themodule[i]->GetFileName().compare(lastdot, k5BitSize, ".bpl\0") == 0;
66 if (!ismplt && !istmpl && !ismpl && !isbpl) {
67 ERR(kLncErr, "mpldbg: input must be .mplt or .mpl or .bpl or .tmpl file");
68 return 1;
69 }
70 // input the file
71 if (ismpl || istmpl) {
72 maple::MIRParser theparser(*themodule[i]);
73 } else {
74 BinaryMplImport binMplt(*themodule[i]);
75 binMplt.SetImported(false);
76 }
77
78 themodule[i]->GetDbgInfo()->BuildDebugInfo();
79 themodule[i]->SetWithDbgInfo(true);
80
81 // output the file
82 themodule[i]->OutputAsciiMpl(".dbg", (ismpl || isbpl) ? ".mpl" : ".tmpl");
83 i++;
84 }
85 return 0;
86 }
87