Permalink
Browse files

Convert codebase to be legal C++

  • Loading branch information...
1 parent 25f6a1a commit ca8deb768251df3af3119959e312e04461c64fac @nelhage committed Nov 9, 2010
View
@@ -1,12 +1,12 @@
-CFLAGS=-m32 -O2 -g -Wall -pthread
+CXXFLAGS=-m32 -O2 -g -Wall -pthread
ASFLAGS=-m32
LDFLAGS=-m32 -pthread
-SRCS=bemu.c bcpu.c bdecode.c bt.c bclock.c bconsole.c
+SRCS=bemu.cpp bcpu.cpp bdecode.cpp bt.cpp bclock.cpp bconsole.cpp
ASMSRCS=bt_helper.S
-OBJECTS=$(SRCS:.c=.o) $(ASMSRCS:.S=.o)
+OBJECTS=$(SRCS:.cpp=.o) $(ASMSRCS:.S=.o)
GEN_H=opcodes.h
-HEADERS=$(SRCS:.c=.h) $(GEN_H) x86.h
+HEADERS=$(SRCS:.cpp=.h) $(GEN_H) x86.h
BEMU=bemu
UASM=uasm/uasm
@@ -17,8 +17,9 @@ TESTS_BIN=$(TESTS:%=tests/%.bin)
all: $(BEMU) $(TESTS_BIN)
$(BEMU): $(OBJECTS)
+ $(CXX) -o $@ $(LDFLAGS) $^
-$(UASM): CFLAGS += -w
+$(UASM): CXXFLAGS += -w
$(UASM):
uasm: $(UASM)
View
File renamed without changes.
@@ -17,10 +17,10 @@ extern beta_cpu CPU;
void* console_process(void *arg UNUSED) {
int err;
- struct pollfd pollfd = {
- .fd = 0,
- .events = POLLIN
- };
+ struct pollfd pollfd = {};
+ pollfd.fd = 0;
+ pollfd.events = POLLIN;
+
while(1) {
err = poll(&pollfd, 1, -1);
if(err < 0) {
View
File renamed without changes.
View
2 bcpu.h
@@ -46,7 +46,7 @@ typedef struct {
typedef uint32_t byteptr;
typedef uint32_t wordptr;
-void bcpu_process_interrupt(beta_cpu *cpu);
+extern "C" void bcpu_process_interrupt(beta_cpu *cpu);
void bcpu_execute_one(beta_cpu *cpu, bdecode *decode);
void bcpu_reset(beta_cpu *cpu);
void bcpu_step_one(beta_cpu *cpu);
@@ -15,14 +15,14 @@
bool decode_valid(bdecode *decode)
{
switch (decode->opcode & 0x3F) {
-#define OP(c) case (c): return TRUE;
+#define OP(c) case (c): return true;
ALL_OPS
#undef OP
- default: return FALSE;
+ default: return false;
}
}
-char *op_name(beta_op op)
+const char *op_name(beta_op op)
{
static char opbuf[20];
switch(op) {
View
@@ -3,13 +3,6 @@
#include <stdint.h>
-#ifndef bool
-#define bool uint8_t
-#endif
-
-#define TRUE 1
-#define FALSE 0
-
typedef enum {
CLASS_ARITH = 0x2,
CLASS_ARITHC = 0x3,
@@ -89,7 +82,7 @@ static inline void decode_op(uint32_t instr, bdecode *decode)
__attribute__ ((always_inline));
bool decode_valid(bdecode *decode);
char *pp_decode(bdecode *decode);
-char *op_name(beta_op op);
+const char *op_name(beta_op op);
/*
* Decode a 32-bit \Beta opcode into a bdecode struct
View
@@ -11,8 +11,7 @@
storing the result in RESULT.
Return 1 if the difference is negative, otherwise 0. */
int
-timeval_subtract (result, x, y)
- struct timeval *result, *x, *y;
+timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y)
{
/* Perform the carry for the later subtraction by updating y. */
if (x->tv_usec < y->tv_usec) {
@@ -48,7 +47,7 @@ void usage() {
exit(1);
}
-struct {
+struct _cpu_options {
bool emulate;
bool do_time;
bool do_profile;
@@ -123,7 +122,8 @@ struct profile_result {
};
int cmp_profile(const void *plhs, const void *prhs) {
- const struct profile_result *lhs = plhs, *rhs = prhs;
+ const struct profile_result *lhs = (const struct profile_result*)plhs;
+ const struct profile_result *rhs = (const struct profile_result*)prhs;
return rhs->count - lhs->count;
}
@@ -168,8 +168,8 @@ int main(int argc, char **argv)
exit(-1);
}
- if((CPU.memory = mmap(NULL, stat.st_size, PROT_READ|PROT_WRITE,
- MAP_PRIVATE, fd, 0)) == MAP_FAILED) {
+ if((CPU.memory = (uint32_t*)mmap(NULL, stat.st_size, PROT_READ|PROT_WRITE,
+ MAP_PRIVATE, fd, 0)) == MAP_FAILED) {
perror("mmap");
exit(-1);
}
View
@@ -1,4 +1,3 @@
-#define _GNU_SOURCE
#include "bemu.h"
#include <setjmp.h>
@@ -39,13 +38,13 @@ static fault_entry *fault_table_alloc;
compiled_frag *bt_frag_hash[256];
/* bt_helper.S */
-extern void bt_enter(ccbuff buf) __attribute__((noreturn));
-extern void bt_continue(void);
-extern void bt_continue_chain(void);
-extern void bt_continue_ic(void);
-extern void bt_interrupt(void);
+extern "C" void bt_enter(ccbuff buf) __attribute__((noreturn));
+extern "C" void bt_continue(void);
+extern "C" void bt_continue_chain(void);
+extern "C" void bt_continue_ic(void);
+extern "C" void bt_interrupt(void);
-void bt_translate_and_run(beta_cpu *cpu, uint32_t used, ccbuff chainptr) __attribute__((noreturn, used));
+extern "C" void bt_translate_and_run(beta_cpu *cpu, uint32_t used, ccbuff chainptr) __attribute__((noreturn, used));
static ccbuff bt_translate_frag(compiled_frag *cfrag, decode_frag *frag);
/*
@@ -128,17 +127,16 @@ static int bt_alloc_segdesc(uint32_t base, uint32_t pages)
{
/* FIXME to actually allocate an unused descriptor */
int segment = 0;
- struct user_desc segdesc = {
- .entry_number = segment,
- .base_addr = base,
- .limit = pages,
- .seg_32bit = 0,
- .contents = 0,
- .read_exec_only = 0,
- .limit_in_pages = 1,
- .seg_not_present = 0,
- .useable = 0,
- };
+ struct user_desc segdesc = {};
+ segdesc.entry_number = segment;
+ segdesc.base_addr = base;
+ segdesc.limit = pages;
+ segdesc.seg_32bit = 0;
+ segdesc.contents = 0;
+ segdesc.read_exec_only = 0;
+ segdesc.limit_in_pages = 1;
+ segdesc.seg_not_present = 0;
+ segdesc.useable = 0;
if(modify_ldt(1, &segdesc, sizeof(segdesc)) < 0) {
perror("modify_ldt");
@@ -216,10 +214,10 @@ void bt_segv(int signo UNUSED, siginfo_t *info, void *ctx) {
}
void bt_setup_segv_handler() {
- struct sigaction action = {
- .sa_sigaction = bt_segv,
- .sa_flags = SA_SIGINFO
- };
+ struct sigaction action = {};
+ action.sa_sigaction = bt_segv;
+ action.sa_flags = SA_SIGINFO;
+
sigemptyset(&action.sa_mask);
if(sigaction(SIGSEGV, &action, NULL) < 0) {
perror("sigaction");
@@ -353,7 +351,7 @@ inline void bt_translate_arith(ccbuff *pbuf, byteptr pc UNUSED, bdecode *inst) {
break;
case OP_CMPLT:
case OP_CMPEQ:
- case OP_CMPLE:
+ case OP_CMPLE: {
/* cmp %ecx, %eax */
X86_CMP_RM32_R32(buf, MOD_REG, X86_ECX, X86_EAX);
X86_MOV_IMM32_R32(buf, X86_EAX);
@@ -363,6 +361,7 @@ inline void bt_translate_arith(ccbuff *pbuf, byteptr pc UNUSED, bdecode *inst) {
: CC_Z));
X86_SETCC_RM8(buf, cc, MOD_REG, X86_EAX);
break;
+ }
default:
panic("Unknown arithmetic opcode: 0x%02x\n", inst->opcode);
}
@@ -428,7 +427,7 @@ inline void bt_translate_arithc(ccbuff *pbuf, byteptr pc UNUSED, bdecode *inst)
break;
case OP_CMPLTC:
case OP_CMPEQC:
- case OP_CMPLEC:
+ case OP_CMPLEC: {
/* cmp $IMM32, %eax */
X86_CMP_IMM32_RM32(buf, MOD_REG, X86_EAX);
X86_IMM32(buf, constant);
@@ -439,6 +438,7 @@ inline void bt_translate_arithc(ccbuff *pbuf, byteptr pc UNUSED, bdecode *inst)
: CC_Z));
X86_SETCC_RM8(buf, cc, MOD_REG, X86_EAX);
break;
+ }
default:
panic("Unknown constant arithmetic opcode: 0x%02x\n", inst->opcode);
}
@@ -722,15 +722,15 @@ void bt_run(beta_cpu *cpu) {
}
if(!bt_stack_base) {
- bt_stack_base = valloc(BT_STACK_SIZE);
+ bt_stack_base = (uint8_t*)valloc(BT_STACK_SIZE);
if(bt_stack_base == NULL) {
panic("Unable to allocate BT stack!\n");
}
}
bt_stack_base += BT_STACK_SIZE;
if(!frag_code_cache) {
- frag_code_cache = valloc(BT_CACHE_SIZE);
+ frag_code_cache = (uint8_t*)valloc(BT_CACHE_SIZE);
if(frag_code_cache == NULL) {
panic("Could not allocate BT cache!");
}
@@ -774,18 +774,18 @@ void bt_translate_and_run(beta_cpu *cpu, uint32_t exact, ccbuff chainptr) {
if(!cfrag) {
frag.start_pc = pc;
- frag.tail = FALSE;
+ frag.tail = false;
for(i = 0; i < MAX_FRAG_SIZE; i++) {
inst = beta_read_mem32(cpu, pc);
decode_op(inst, &frag.insts[i]);
if(bt_ends_frag(&frag.insts[i])) {
- frag.tail = TRUE;
+ frag.tail = true;
break;
}
pc += 4;
}
frag.ninsts = i;
- cfrag = bt_alloc_cfrag(TRUE);
+ cfrag = bt_alloc_cfrag(true);
if (chainptr == cfrag->code) {
cfrag->code -= 5;
chainptr = NULL;

0 comments on commit ca8deb7

Please sign in to comment.