Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewriter: non-functional changes for #514 (mostly formatting) #516

Merged
merged 2 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 43 additions & 13 deletions tools/rewriter/CAbi.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#pragma once

#include <cassert>
#include <cstdint>
#include <vector>
#include <string>
#include <vector>

class ArgLocation {
public:
Expand All @@ -23,18 +24,20 @@ class ArgLocation {
Kind _kind;
unsigned _size;
unsigned _align;
const char* _reg = nullptr;
const char *_reg = nullptr;
size_t _stack_offset = 0;

public:
static ArgLocation Register(Kind kind, unsigned size) {
return ArgLocation(kind, size, size);
}
static ArgLocation Stack(int64_t size, int64_t align) {
auto loc = ArgLocation(Kind::Memory, static_cast<unsigned>(size), static_cast<unsigned>(align));

static ArgLocation Stack(int64_t size, int64_t align) {
auto loc = ArgLocation(Kind::Memory, static_cast<unsigned>(size), static_cast<unsigned>(align));
loc._onStack = true;
return loc;
}

static ArgLocation Indirect(unsigned size, unsigned align) {
auto loc = ArgLocation(Kind::Integral, static_cast<unsigned>(size), static_cast<unsigned>(align));
loc._indirectOnStack = true;
Expand All @@ -46,35 +49,62 @@ class ArgLocation {
_allocated = true;
_reg = r;
}

void allocate_stack(size_t offset) {
assert(!_allocated);
_allocated = true;
_onStack = true;
_stack_offset = offset;
}

void set_indirect_on_stack() {
assert(!_indirectOnStack);
_indirectOnStack = true;
}

bool is_allocated() const { return _allocated; }
bool is_stack() const { return _onStack; }
bool is_indirect() const { return _indirectOnStack; }
bool is_allocated() const {
return _allocated;
}

bool is_stack() const {
return _onStack;
}

bool is_indirect() const {
return _indirectOnStack;
}

bool is_128bit_float() const {
return _reg != nullptr && ((_reg[0] == 'x' && _reg[1] == 'm' && _reg[2] == 'm') || _reg[0] == 'q');
}
const char* as_str() const {

const char *as_str() const {
if (_onStack) {
return "<stack>";
} else {
return _reg;
}
}
operator const char *() const { return as_str(); }
Kind kind() const { return _kind; }
size_t size() const { return is_128bit_float() ? 16 : _size; }
size_t align() const { return _align; }
size_t stack_offset() const { return _stack_offset; }

operator const char *() const {
return as_str();
}

Kind kind() const {
return _kind;
}

size_t size() const {
return is_128bit_float() ? 16 : _size;
}

size_t align() const {
return _align;
}

size_t stack_offset() const {
return _stack_offset;
}
};

struct AbiSignature {
Expand Down
15 changes: 10 additions & 5 deletions tools/rewriter/DetermineAbi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@
#define VERBOSE_DEBUG 0

#if VERBOSE_DEBUG
#define DEBUG(X) do { X; } while (0)
#define DEBUG(X) \
do { \
X; \
} while (0)
#else
#define DEBUG(X) do { } while (0)
#define DEBUG(X) \
do { \
} while (0)
#endif

static ArgLocation classifyScalarType(const llvm::Type &type) {
Expand Down Expand Up @@ -104,7 +109,7 @@ abiSlotsForArg(const clang::QualType &qt,
// We have a scalar type, so classify it.
return {classifyScalarType(*Ty)};
}
case Kind::Ignore: // no ABI presence
case Kind::Ignore: // no ABI presence
return {};
case Kind::InAlloca: // via implicit pointer
// It looks like inalloca is only valid on Win32
Expand Down Expand Up @@ -139,7 +144,7 @@ cgFunctionInfo(clang::CodeGen::CodeGenModule &cgm,
}

AbiSignature determineAbi(const clang::CodeGen::CGFunctionInfo &info,
const clang::ASTContext &astContext, Arch arch) {
const clang::ASTContext &astContext, Arch arch) {
// get ABI for return type and each parameter
AbiSignature sig;
sig.variadic = info.isVariadic();
Expand Down Expand Up @@ -220,7 +225,7 @@ AbiSignature determineAbiForDecl(const clang::FunctionDecl &fnDecl, Arch arch) {
}

AbiSignature determineAbiForProtoType(const clang::FunctionProtoType &fpt,
clang::ASTContext &astContext, Arch arch) {
clang::ASTContext &astContext, Arch arch) {
// FIXME: This is copied verbatim from determineAbiForDecl and could be
// factored out. This depends on what we do with PR #78 so I'm leaving it as
// is for now.
Expand Down
4 changes: 2 additions & 2 deletions tools/rewriter/DetermineAbi.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
auto determineAbiForDecl(const clang::FunctionDecl &fnDecl, Arch arch) -> AbiSignature;

AbiSignature determineAbiForProtoType(const clang::FunctionProtoType &fpt,
clang::ASTContext &astContext,
Arch arch);
clang::ASTContext &astContext,
Arch arch);
9 changes: 8 additions & 1 deletion tools/rewriter/GenCallAsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1159,5 +1159,12 @@ std::string emit_asm_wrapper(AbiSignature sig,
// for indirect wrappers or `.text` for the direct case
add_asm_line(aw, ".previous");

return aw.ss.str();
std::string wrapper = "asm("s;
if (as_macro) {
wrapper += "\\";
}
wrapper += "\n";
wrapper += aw.ss.str();
wrapper += ");\n";
return wrapper;
}
10 changes: 0 additions & 10 deletions tools/rewriter/SourceRewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1274,9 +1274,7 @@ int main(int argc, const char **argv) {
std::string asm_wrapper =
emit_asm_wrapper(info.sig, {info.wrapper_sig}, wrapper_name, std::nullopt,
WrapperKind::IndirectCallsite, caller_pkey, 0, Target);
wrapper_out << "asm(\n";
wrapper_out << asm_wrapper;
wrapper_out << ");\n";

if (!type_id_macros_generated.contains(mangled_ty)) {
header_out << "#define IA2_TYPE_"s << mangled_ty << " " << info.type_str << "\n";
Expand Down Expand Up @@ -1421,9 +1419,7 @@ int main(int argc, const char **argv) {
std::string asm_wrapper =
emit_asm_wrapper(c_abi_sig, std::nullopt, wrapper_name, target_fn,
WrapperKind::Direct, caller_pkey, target_pkey, Target);
wrapper_out << "asm(\n";
wrapper_out << asm_wrapper;
wrapper_out << ");\n";

write_to_file(ld_args_out, caller_pkey, "--wrap="s + fn_name + '\n', ".ld");
}
Expand All @@ -1443,9 +1439,7 @@ int main(int argc, const char **argv) {
std::string asm_wrapper =
emit_asm_wrapper(c_abi_sig, std::nullopt, wrapper_name, fn_name, WrapperKind::Direct,
0, compartment_pkey, Target);
wrapper_out << "asm(\n";
wrapper_out << asm_wrapper;
wrapper_out << ");\n";

write_to_file(ld_args_out, compartment_pkey,
"--wrap="s + fn_name + '\n', ".ld");
Expand Down Expand Up @@ -1484,9 +1478,7 @@ int main(int argc, const char **argv) {
WrapperKind::Pointer, 0, target_pkey, Target,
true /* as_macro */);
macros_defining_wrappers += "#define IA2_DEFINE_WRAPPER_"s + fn_name + " \\\n";
macros_defining_wrappers += "asm(\\\n";
macros_defining_wrappers += asm_wrapper;
macros_defining_wrappers += ");\n";

/*
* Invoke IA2_DEFINE_WRAPPER from ia2.h in the source file defining the
Expand Down Expand Up @@ -1527,9 +1519,7 @@ int main(int argc, const char **argv) {
c_abi_sig, std::nullopt, wrapper_name, fn_name, WrapperKind::PointerToStatic, 0,
target_pkey, Target, true /* as_macro */);
macros_defining_wrappers += "#define IA2_DEFINE_WRAPPER_"s + fn_name + " \\\n";
macros_defining_wrappers += "asm(\\\n";
macros_defining_wrappers += asm_wrapper;
macros_defining_wrappers += ");\n";

header_out << "extern " << opaque << " " << wrapper_name << ";\n";

Expand Down