Skip to content

Commit

Permalink
rewriter: reformat CAbi.h, DetermineAbi.{h,cpp}
Browse files Browse the repository at this point in the history
  • Loading branch information
kkysen committed Feb 27, 2025
1 parent c5e8467 commit 53e5146
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 20 deletions.
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);

0 comments on commit 53e5146

Please sign in to comment.