Anders Waldenborg | b932c66 | 2013-10-23 08:10:20 +0000 | [diff] [blame^] | 1 | /*===-- calc.c - tool for testing libLLVM and llvm-c API ------------------===*\ |
| 2 | |* *| |
| 3 | |* The LLVM Compiler Infrastructure *| |
| 4 | |* *| |
| 5 | |* This file is distributed under the University of Illinois Open Source *| |
| 6 | |* License. See LICENSE.TXT for details. *| |
| 7 | |* *| |
| 8 | |*===----------------------------------------------------------------------===*| |
| 9 | |* *| |
| 10 | |* This file implements the --calc command in llvm-c-test. --calc reads lines *| |
| 11 | |* from stdin, parses them as a name and an expression in reverse polish *| |
| 12 | |* notation and prints a module with a function with the expression. *| |
| 13 | |* *| |
| 14 | \*===----------------------------------------------------------------------===*/ |
| 15 | |
| 16 | #include "llvm-c-test.h" |
| 17 | #include "llvm-c/Core.h" |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | #include <assert.h> |
| 22 | |
| 23 | typedef LLVMValueRef (*binop_func_t)(LLVMBuilderRef, LLVMValueRef LHS, |
| 24 | LLVMValueRef RHS, const char *Name); |
| 25 | |
| 26 | static LLVMOpcode op_to_opcode(char op) { |
| 27 | switch (op) { |
| 28 | case '+': return LLVMAdd; |
| 29 | case '-': return LLVMSub; |
| 30 | case '*': return LLVMMul; |
| 31 | case '/': return LLVMSDiv; |
| 32 | case '&': return LLVMAnd; |
| 33 | case '|': return LLVMOr; |
| 34 | case '^': return LLVMXor; |
| 35 | } |
| 36 | assert(0 && "unknown operation"); |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | #define MAX_DEPTH 32 |
| 41 | |
| 42 | static LLVMValueRef build_from_tokens(char **tokens, int ntokens, |
| 43 | LLVMBuilderRef builder, |
| 44 | LLVMValueRef param) { |
| 45 | LLVMValueRef stack[MAX_DEPTH]; |
| 46 | int depth = 0; |
| 47 | |
| 48 | for (int i = 0; i < ntokens; i++) { |
| 49 | char tok = tokens[i][0]; |
| 50 | switch (tok) { |
| 51 | case '+': |
| 52 | case '-': |
| 53 | case '*': |
| 54 | case '/': |
| 55 | case '&': |
| 56 | case '|': |
| 57 | case '^': |
| 58 | if (depth < 2) { |
| 59 | printf("stack underflow\n"); |
| 60 | return NULL; |
| 61 | } |
| 62 | |
| 63 | stack[depth - 2] = LLVMBuildBinOp(builder, op_to_opcode(tok), |
| 64 | stack[depth - 1], stack[depth - 2], ""); |
| 65 | depth--; |
| 66 | |
| 67 | break; |
| 68 | |
| 69 | case '@': |
| 70 | if (depth < 1) { |
| 71 | printf("stack underflow\n"); |
| 72 | return NULL; |
| 73 | } |
| 74 | |
| 75 | LLVMValueRef off = LLVMBuildGEP(builder, param, &stack[depth - 1], 1, ""); |
| 76 | stack[depth - 1] = LLVMBuildLoad(builder, off, ""); |
| 77 | |
| 78 | break; |
| 79 | |
| 80 | default: { |
| 81 | char *end; |
| 82 | long val = strtol(tokens[i], &end, 0); |
| 83 | if (end[0] != '\0') { |
| 84 | printf("error parsing number\n"); |
| 85 | return NULL; |
| 86 | } |
| 87 | |
| 88 | if (depth >= MAX_DEPTH) { |
| 89 | printf("stack overflow\n"); |
| 90 | return NULL; |
| 91 | } |
| 92 | |
| 93 | stack[depth++] = LLVMConstInt(LLVMInt64Type(), val, 1); |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | if (depth < 1) { |
| 100 | printf("stack underflow at return\n"); |
| 101 | return NULL; |
| 102 | } |
| 103 | |
| 104 | LLVMBuildRet(builder, stack[depth - 1]); |
| 105 | |
| 106 | return stack[depth - 1]; |
| 107 | } |
| 108 | |
| 109 | static void handle_line(char **tokens, int ntokens) { |
| 110 | char *name = tokens[0]; |
| 111 | |
| 112 | LLVMModuleRef M = LLVMModuleCreateWithName(name); |
| 113 | |
| 114 | LLVMTypeRef I64ty = LLVMInt64Type(); |
| 115 | LLVMTypeRef I64Ptrty = LLVMPointerType(I64ty, 0); |
| 116 | LLVMTypeRef Fty = LLVMFunctionType(I64ty, &I64Ptrty, 1, 0); |
| 117 | |
| 118 | LLVMValueRef F = LLVMAddFunction(M, name, Fty); |
| 119 | LLVMBuilderRef builder = LLVMCreateBuilder(); |
| 120 | LLVMPositionBuilderAtEnd(builder, LLVMAppendBasicBlock(F, "entry")); |
| 121 | |
| 122 | LLVMValueRef param; |
| 123 | LLVMGetParams(F, ¶m); |
| 124 | LLVMSetValueName(param, "in"); |
| 125 | |
| 126 | LLVMValueRef res = build_from_tokens(tokens + 1, ntokens - 1, builder, param); |
| 127 | if (res) { |
| 128 | char *irstr = LLVMPrintModuleToString(M); |
| 129 | puts(irstr); |
| 130 | LLVMDisposeMessage(irstr); |
| 131 | } |
| 132 | |
| 133 | LLVMDisposeBuilder(builder); |
| 134 | |
| 135 | LLVMDisposeModule(M); |
| 136 | } |
| 137 | |
| 138 | int calc(void) { |
| 139 | |
| 140 | tokenize_stdin(handle_line); |
| 141 | |
| 142 | return 0; |
| 143 | } |