1 /*
2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3 *
4 * HDF is dual licensed: you can use it either under the terms of
5 * the GPL, or the BSD license, at your option.
6 * See the LICENSE file in the root of this repository for complete details.
7 */
8
9 #include <unistd.h>
10 #include "hcs_file.h"
11 #include "hcs_compiler.h"
12 #include "hcs_log.h"
13
14 #define ARG_COUNT_MIN 2
15 #define USAGE(option, info) HCS_PRINT(" %-12s%s\n", option, info)
16
17 static bool g_genTextConfigOutput = false;
18 static bool g_genByteCodeConfigOutput = true;
19 static bool g_genByteCodeHexdump = false;
20 static bool g_verbosePrint = false;
21 static bool g_decompile = false;
22 static bool g_shouldAlign = false;
23 static const char *g_symbolPrefix = NULL;
24
HcsOptShouldAlign(void)25 bool HcsOptShouldAlign(void)
26 {
27 return g_shouldAlign;
28 }
29
HcsOptSetAlign(bool align)30 void HcsOptSetAlign(bool align)
31 {
32 g_shouldAlign = align;
33 }
34
HcsOptShouldGenTextConfig(void)35 bool HcsOptShouldGenTextConfig(void)
36 {
37 return g_genTextConfigOutput;
38 }
39
HcsOptShouldGenByteCodeConfig(void)40 bool HcsOptShouldGenByteCodeConfig(void)
41 {
42 return g_genByteCodeConfigOutput;
43 }
44
HcsOptDecompile(void)45 bool HcsOptDecompile(void)
46 {
47 return g_decompile;
48 }
49
HcsOptGetSymbolNamePrefix(void)50 const char *HcsOptGetSymbolNamePrefix(void)
51 {
52 return g_symbolPrefix;
53 }
54
HcsOptShouldGenHexdump(void)55 bool HcsOptShouldGenHexdump(void)
56 {
57 return g_genByteCodeHexdump;
58 }
59
ShowUsage()60 void ShowUsage()
61 {
62 HCS_PRINT("Usage: hc-gen [Options] [File]\n");
63 HCS_PRINT("options:\n");
64 USAGE("-o <file>", "output file name, default same as input");
65 USAGE("-a", "hcb align with four bytes");
66 USAGE("-b", "output binary output, default enable");
67 USAGE("-t", "output config in C language source file style");
68 USAGE("-i", "output binary hex dump in C language source file style");
69 USAGE("-p <prefix>", "prefix of generated symbol name");
70 USAGE("-d", "decompile hcb to hcs");
71 USAGE("-V", "show verbose info");
72 USAGE("-v", "show version");
73 USAGE("-h", "show this help message");
74 }
75
76 #define HCS_SUPPORT_ARGS "o:ap:bditvVh"
77
ShowVersionInfo()78 void ShowVersionInfo()
79 {
80 HCS_PRINT("Hcs compiler v%u.%u\n", HCS_COMPILER_VERSION_MAJOR, HCS_COMPILER_VERSION_MINOR);
81 HCS_PRINT("Copyright (c) 2020-2021 Huawei Device Co., Ltd.\n");
82 }
83
ProcessOption(int32_t argc,char * argv[])84 static int32_t ProcessOption(int32_t argc, char *argv[])
85 {
86 int32_t op = 0;
87 while (op != HCS_OPTION_END) {
88 op = getopt(argc, argv, HCS_SUPPORT_ARGS);
89 switch (op) {
90 case 'o':
91 if (HcsSetOutPutName(optarg)) {
92 return EOPTION;
93 }
94 break;
95 case 'a':
96 g_shouldAlign = true;
97 break;
98 case 'b':
99 g_genByteCodeConfigOutput = true;
100 break;
101 case 't':
102 g_genTextConfigOutput = true;
103 g_genByteCodeConfigOutput = false;
104 break;
105 case 'p':
106 g_symbolPrefix = optarg;
107 break;
108 case 'i':
109 g_genByteCodeHexdump = true;
110 break;
111 case 'V':
112 g_verbosePrint = true;
113 break;
114 case 'd':
115 g_decompile = true;
116 break;
117 case 'v':
118 ShowVersionInfo();
119 return EFAIL;
120 case 'h': /* fall-through */
121 case '?':
122 ShowUsage();
123 return EOPTION;
124 default:
125 break;
126 }
127 }
128
129 return NOERR;
130 }
131
DoOption(int32_t argc,char * argv[])132 int32_t DoOption(int32_t argc, char *argv[])
133 {
134 if (argc < ARG_COUNT_MIN) {
135 ShowUsage();
136 return EOPTION;
137 }
138
139 int32_t ret = ProcessOption(argc, argv);
140 if (ret) {
141 return ret;
142 }
143
144 if (optind >= argc) {
145 HCS_ERROR("Miss input file name");
146 return EOPTION;
147 } else {
148 const char *inputFIlePath = argv[optind];
149 HCS_DEBUG("opt info:input file name:%s\n", inputFIlePath);
150 HcsSetInputFileName(inputFIlePath);
151 }
152 return 0;
153 }
154
HcsVerbosePrint(void)155 bool HcsVerbosePrint(void)
156 {
157 return g_verbosePrint;
158 }
159