llvm-c-test: Make them C89-compliant.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@193254 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
NAKAMURA Takumi 2013-10-23 17:56:37 +00:00
parent e21c3137e1
commit 90fd79a222
5 changed files with 29 additions and 15 deletions

View File

@ -44,8 +44,9 @@ static LLVMValueRef build_from_tokens(char **tokens, int ntokens,
LLVMValueRef param) { LLVMValueRef param) {
LLVMValueRef stack[MAX_DEPTH]; LLVMValueRef stack[MAX_DEPTH];
int depth = 0; int depth = 0;
int i;
for (int i = 0; i < ntokens; i++) { for (i = 0; i < ntokens; i++) {
char tok = tokens[i][0]; char tok = tokens[i][0];
switch (tok) { switch (tok) {
case '+': case '+':
@ -66,16 +67,19 @@ static LLVMValueRef build_from_tokens(char **tokens, int ntokens,
break; break;
case '@': case '@': {
LLVMValueRef off;
if (depth < 1) { if (depth < 1) {
printf("stack underflow\n"); printf("stack underflow\n");
return NULL; return NULL;
} }
LLVMValueRef off = LLVMBuildGEP(builder, param, &stack[depth - 1], 1, ""); off = LLVMBuildGEP(builder, param, &stack[depth - 1], 1, "");
stack[depth - 1] = LLVMBuildLoad(builder, off, ""); stack[depth - 1] = LLVMBuildLoad(builder, off, "");
break; break;
}
default: { default: {
char *end; char *end;
@ -108,6 +112,8 @@ static LLVMValueRef build_from_tokens(char **tokens, int ntokens,
static void handle_line(char **tokens, int ntokens) { static void handle_line(char **tokens, int ntokens) {
char *name = tokens[0]; char *name = tokens[0];
LLVMValueRef param;
LLVMValueRef res;
LLVMModuleRef M = LLVMModuleCreateWithName(name); LLVMModuleRef M = LLVMModuleCreateWithName(name);
@ -119,11 +125,10 @@ static void handle_line(char **tokens, int ntokens) {
LLVMBuilderRef builder = LLVMCreateBuilder(); LLVMBuilderRef builder = LLVMCreateBuilder();
LLVMPositionBuilderAtEnd(builder, LLVMAppendBasicBlock(F, "entry")); LLVMPositionBuilderAtEnd(builder, LLVMAppendBasicBlock(F, "entry"));
LLVMValueRef param;
LLVMGetParams(F, &param); LLVMGetParams(F, &param);
LLVMSetValueName(param, "in"); LLVMSetValueName(param, "in");
LLVMValueRef res = build_from_tokens(tokens + 1, ntokens - 1, builder, param); res = build_from_tokens(tokens + 1, ntokens - 1, builder, param);
if (res) { if (res) {
char *irstr = LLVMPrintModuleToString(M); char *irstr = LLVMPrintModuleToString(M);
puts(irstr); puts(irstr);

View File

@ -20,8 +20,9 @@
#include <stdlib.h> #include <stdlib.h>
static void pprint(int pos, unsigned char *buf, int len, const char *disasm) { static void pprint(int pos, unsigned char *buf, int len, const char *disasm) {
int i;
printf("%04x: ", pos); printf("%04x: ", pos);
for (int i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
if (i < len) { if (i < len) {
printf("%02x ", buf[i]); printf("%02x ", buf[i]);
} else { } else {
@ -34,14 +35,15 @@ static void pprint(int pos, unsigned char *buf, int len, const char *disasm) {
static void do_disassemble(const char *triple, unsigned char *buf, int siz) { static void do_disassemble(const char *triple, unsigned char *buf, int siz) {
LLVMDisasmContextRef D = LLVMCreateDisasm(triple, NULL, 0, NULL, NULL); LLVMDisasmContextRef D = LLVMCreateDisasm(triple, NULL, 0, NULL, NULL);
char outline[1024];
int pos;
if (!D) { if (!D) {
printf("ERROR: Couldn't create disassebler for triple %s\n", triple); printf("ERROR: Couldn't create disassebler for triple %s\n", triple);
return; return;
} }
char outline[1024]; pos = 0;
int pos = 0;
while (pos < siz) { while (pos < siz) {
size_t l = LLVMDisasmInstruction(D, buf + pos, siz - pos, 0, outline, size_t l = LLVMDisasmInstruction(D, buf + pos, siz - pos, 0, outline,
sizeof(outline)); sizeof(outline));
@ -61,10 +63,11 @@ static void handle_line(char **tokens, int ntokens) {
unsigned char disbuf[128]; unsigned char disbuf[128];
size_t disbuflen = 0; size_t disbuflen = 0;
char *triple = tokens[0]; char *triple = tokens[0];
int i;
printf("triple: %s\n", triple); printf("triple: %s\n", triple);
for (int i = 1; i < ntokens; i++) { for (i = 1; i < ntokens; i++) {
disbuf[disbuflen++] = strtol(tokens[i], NULL, 16); disbuf[disbuflen++] = strtol(tokens[i], NULL, 16);
if (disbuflen >= sizeof(disbuf)) { if (disbuflen >= sizeof(disbuf)) {
fprintf(stderr, "Warning: Too long line, truncating\n"); fprintf(stderr, "Warning: Too long line, truncating\n");

View File

@ -58,16 +58,18 @@ int module_list_functions(void) {
if (LLVMIsDeclaration(f)) { if (LLVMIsDeclaration(f)) {
printf("FunctionDeclaration: %s\n", LLVMGetValueName(f)); printf("FunctionDeclaration: %s\n", LLVMGetValueName(f));
} else { } else {
LLVMBasicBlockRef bb;
LLVMValueRef isn;
unsigned nisn = 0; unsigned nisn = 0;
unsigned nbb = 0; unsigned nbb = 0;
printf("FunctionDefinition: %s [#bb=%u]\n", LLVMGetValueName(f), printf("FunctionDefinition: %s [#bb=%u]\n", LLVMGetValueName(f),
LLVMCountBasicBlocks(f)); LLVMCountBasicBlocks(f));
for (LLVMBasicBlockRef bb = LLVMGetFirstBasicBlock(f); bb; for (bb = LLVMGetFirstBasicBlock(f); bb;
bb = LLVMGetNextBasicBlock(bb)) { bb = LLVMGetNextBasicBlock(bb)) {
nbb++; nbb++;
for (LLVMValueRef isn = LLVMGetFirstInstruction(bb); isn; for (isn = LLVMGetFirstInstruction(bb); isn;
isn = LLVMGetNextInstruction(isn)) { isn = LLVMGetNextInstruction(isn)) {
nisn++; nisn++;
if (LLVMIsACallInst(isn)) { if (LLVMIsACallInst(isn)) {

View File

@ -20,6 +20,7 @@
int object_list_sections(void) { int object_list_sections(void) {
LLVMMemoryBufferRef MB; LLVMMemoryBufferRef MB;
LLVMObjectFileRef O; LLVMObjectFileRef O;
LLVMSectionIteratorRef sect;
char *msg = NULL; char *msg = NULL;
if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) { if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
@ -33,7 +34,7 @@ int object_list_sections(void) {
exit(1); exit(1);
} }
LLVMSectionIteratorRef sect = LLVMGetSections(O); sect = LLVMGetSections(O);
while (!LLVMIsSectionIteratorAtEnd(O, sect)) { while (!LLVMIsSectionIteratorAtEnd(O, sect)) {
printf("'%s': @0x%08" PRIx64 " +%" PRIu64 "\n", LLVMGetSectionName(sect), printf("'%s': @0x%08" PRIx64 " +%" PRIu64 "\n", LLVMGetSectionName(sect),
LLVMGetSectionAddress(sect), LLVMGetSectionSize(sect)); LLVMGetSectionAddress(sect), LLVMGetSectionSize(sect));
@ -51,6 +52,8 @@ int object_list_sections(void) {
int object_list_symbols(void) { int object_list_symbols(void) {
LLVMMemoryBufferRef MB; LLVMMemoryBufferRef MB;
LLVMObjectFileRef O; LLVMObjectFileRef O;
LLVMSectionIteratorRef sect;
LLVMSymbolIteratorRef sym;
char *msg = NULL; char *msg = NULL;
if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) { if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
@ -64,8 +67,8 @@ int object_list_symbols(void) {
exit(1); exit(1);
} }
LLVMSectionIteratorRef sect = LLVMGetSections(O); sect = LLVMGetSections(O);
LLVMSymbolIteratorRef sym = LLVMGetSymbols(O); sym = LLVMGetSymbols(O);
while (!LLVMIsSymbolIteratorAtEnd(O, sym)) { while (!LLVMIsSymbolIteratorAtEnd(O, sym)) {
LLVMMoveToContainingSection(sect, sym); LLVMMoveToContainingSection(sect, sym);

View File

@ -15,10 +15,11 @@
#include <stdio.h> #include <stdio.h>
int targets_list(void) { int targets_list(void) {
LLVMTargetRef t;
LLVMInitializeAllTargetInfos(); LLVMInitializeAllTargetInfos();
LLVMInitializeAllTargets(); LLVMInitializeAllTargets();
for (LLVMTargetRef t = LLVMGetFirstTarget(); t; t = LLVMGetNextTarget(t)) { for (t = LLVMGetFirstTarget(); t; t = LLVMGetNextTarget(t)) {
printf("%s", LLVMGetTargetName(t)); printf("%s", LLVMGetTargetName(t));
if (LLVMTargetHasJIT(t)) if (LLVMTargetHasJIT(t))
printf(" (+jit)"); printf(" (+jit)");