Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
172 lines (146 loc) · 7.67 KB
/
Copy pathmain.cpp
File metadata and controls
172 lines (146 loc) · 7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "Pass_C2Pancake.h"
#include "Pass_FunctionCalling.h"
#include "Pass_HoistArraysAndAddresses.h"
#include "Pass_InjectHeaders.h"
#include "Pass_IntegerConversion.h"
#include "Pass_LoopsToWhile.h"
#include "Pass_LowerBitfieldOps.h"
#include "Pass_NormaliseIfStatements.h"
#include "Pass_PromoteRecords.h"
#include "Pass_SwitchToIf.h"
#include "Pass_TransformLogicalExpressions.h"
#include "Pipeline.h"
#include "Utils.h"
#include <clang/Basic/LLVM.h>
#include <clang/Tooling/CommonOptionsParser.h>
#include <llvm/ADT/StringRef.h>
#include <llvm/Config/llvm-config.h>
#include <llvm/Support/CommandLine.h>
#include <llvm/Support/FormatAdapters.h>
#include <llvm/Support/FormatVariadic.h>
#include <llvm/Support/raw_ostream.h>
#include <cstddef>
#include <string>
#include <utility>
using namespace pancake;
namespace {
const char *overview_help = "c2pancake is a tool that converts C code into Pancake code.\n";
llvm::cl::OptionCategory c2_pancake_options("c2pancake options");
const llvm::cl::extrahelp common_help(clang::tooling::CommonOptionsParser::HelpMessage);
const llvm::cl::extrahelp more_help(
R"(Documentation:
https://github.com/someretical/c2pancake
Usage examples:
c2pancake hello_world.c -- -std=c23
)");
void PrintVersion(llvm::raw_ostream &os) {
os << llvm::formatv("c2pancake 0.0.1\n");
os << llvm::formatv("LLVM {0}\n", LLVM_VERSION_STRING);
}
} // namespace
// NOLINTBEGIN(misc-use-internal-linkage)
llvm::cl::opt<bool> opt_treat_external_functions_as_non_ffi(
"treat-header-functions-as-non-ffi",
llvm::cl::desc("Treat functions defined in headers as non-FFI, meaning no shims will be generated for them "
"(default: false, meaning shims will be generated)"),
llvm::cl::cat(c2_pancake_options), llvm::cl::init(false));
llvm::cl::opt<PointerWidth> pointer_bits("pointer-bits", llvm::cl::desc("Override pointer width"),
llvm::cl::cat(c2_pancake_options),
llvm::cl::values(clEnumValN(PointerWidth::W32, "32", "32-bit"),
clEnumValN(PointerWidth::W64, "64", "64-bit")),
llvm::cl::init(PointerWidth::None));
llvm::cl::opt<uint64_t> stack_size_opt("stack-size",
llvm::cl::desc("Override Pancake stack size (default: 0x10000 = 4096 * 16)"),
llvm::cl::cat(c2_pancake_options), llvm::cl::init(0x10000UL));
llvm::cl::opt<std::string> make_path("make-path", llvm::cl::desc("Override make path (default: make)"),
llvm::cl::cat(c2_pancake_options), llvm::cl::init("make"));
llvm::cl::opt<std::string> cake_path("cake-path", llvm::cl::desc("Path to the Cake compiler (default: cake in PATH)"),
llvm::cl::cat(c2_pancake_options), llvm::cl::init("cake"));
llvm::cl::opt<std::string>
cake_options("cake-options",
llvm::cl::desc("Override Cake compiler options (default: --pancake --main_return=true)"),
llvm::cl::cat(c2_pancake_options), llvm::cl::init("--pancake --main_return=true"));
llvm::cl::opt<size_t> max_pass_retries(
"max-pass-retries",
llvm::cl::desc("Maximum number of retries for a pass before moving to the next pass (default: 20)"),
llvm::cl::cat(c2_pancake_options), llvm::cl::init(20));
llvm::cl::opt<std::string>
start_at_pass("start",
llvm::cl::desc("Start the pipeline at this pass (default: empty, meaning start at beginning)"),
llvm::cl::cat(c2_pancake_options), llvm::cl::init(""));
llvm::cl::opt<std::string> end_at_pass(
"end",
llvm::cl::desc(
"End the pipeline after this pass (including retries) (default: empty, meaning end after last pass)"),
llvm::cl::cat(c2_pancake_options), llvm::cl::init(""));
llvm::cl::list<std::string>
skipped_passes("skip", llvm::cl::desc("Passes to skip (default: empty, meaning no passes are skipped)"),
llvm::cl::CommaSeparated, llvm::cl::ZeroOrMore, llvm::cl::cat(c2_pancake_options));
llvm::cl::list<std::string> function_rewrite_extra_headers(
"function-rewrite-extra-headers",
llvm::cl::desc("Extra headers to include in the function rewrite pass (default: empty, meaning no extra headers)"),
llvm::cl::CommaSeparated, llvm::cl::ZeroOrMore, llvm::cl::cat(c2_pancake_options));
// NOLINTEND(misc-use-internal-linkage)
auto main(int argc, const char **argv) -> int {
llvm::cl::SetVersionPrinter(PrintVersion);
auto parser =
clang::tooling::CommonOptionsParser::create(argc, argv, c2_pancake_options, llvm::cl::OneOrMore, overview_help);
if (auto error = parser.takeError()) {
llvm::errs() << llvm::formatv("[c2pancake] {0}", llvm::fmt_consume(std::move(error)));
return 1;
}
/*
To add a new stage to the pipeline, use the following code.
Consumer::HandleTranslationUnit is the entry point and must be implemented.
The main thing HandleTranslationUnit should do is add Replacements to ps_ctx.replacements.
The default finalizer applies them, formats the result, and writes the next pipeline file.
Stages can share named typed data through ps_ctx.run.artifacts.Set(key, value) and
artifacts.Require(key). Publication is single-assignment; use Replace only for intentional updates.
A non-Clang stage can instead
be registered with pipeline.AddFileStage<T>(); T must construct from StageContext&
and implement auto Run() -> llvm::Error, setting the stage control with ctx.SetControl().
class Consumer : public C2PancakePass {
public:
using C2PancakePass::C2PancakePass; // inherit constructor
void HandleTranslationUnit(clang::ASTContext &Ctx) override;
};
class Action : public ClangStage<Consumer> {
public:
explicit Action(StageContext &ctx) : ClangStage<Consumer>(ctx) {
ctx.stage_name = "<name of pass>";
}
};
*/
Pipeline pipeline(*parser);
pipeline.AddStage<pass_inject_headers::Action>();
pipeline.AddStage<pass_name_anon_records::Action>();
pipeline.AddStage<pass_rename_to_be_promoted_records::Action>();
pipeline.AddStage<pass_promote_records::Action>();
pipeline.AddStage<pass_inject_memcpy_polyfill::Action>();
pipeline.AddStage<pass_function_calling::Action>();
pipeline.AddStage<pass_normalise_while_loops::Action>();
pipeline.AddStage<pass_process_continue_in_for_loops::Action>();
pipeline.AddStage<pass_for_to_while::Action>();
pipeline.AddStage<pass_process_continue_in_do_while_loops::Action>();
pipeline.AddStage<pass_do_while_to_while::Action>();
pipeline.AddStage<pass_add_switch_fallthrough::Action>();
pipeline.AddStage<pass_normalise_switches::Action>();
pipeline.AddStage<pass_switch_to_if::Action>();
pipeline.AddStage<pass_normalise_if_statements::Action>();
pipeline.AddStage<pass_hoist_condition_expressions::Action>();
pipeline.AddStage<pass_rewrite_array_indexing::Action>();
pipeline.AddStage<pass_rewrite_struct_stabs::Action>();
pipeline.AddStage<pass_lower_nested_expressions::Action>();
pipeline.AddStage<pass_simplify_double_negation::Action>();
pipeline.AddStage<pass_lower_bitfield_ops::Action>();
pipeline.AddStage<pass_simplify_addrof_deref::Action>();
pipeline.AddStage<pass_implicit_to_explicit_casts::Action>();
pipeline.AddStage<pass_integer_conversion::Action>();
// deliberately repeated. Those final explicit casts are just to make the C compiler happy, they have no effect when
// converting to pancake.
pipeline.AddStage<pass_implicit_to_explicit_casts::Action>();
pipeline.AddStage<pass_rename_to_be_hoisted_globals::Action>();
pipeline.AddStage<pass_hoist_locals::Action>();
pipeline.AddStage<pass_c2pancake::Action>();
return pipeline.Run();
}