You've already forked linux-packaging-mono
acceptance-tests
data
debian
docs
external
Newtonsoft.Json
api-doc-tools
api-snapshot
aspnetwebstack
bdwgc
binary-reference-assemblies
bockbuild
boringssl
cecil
cecil-legacy
corefx
corert
helix-binaries
ikdasm
ikvm
illinker-test-assets
linker
llvm-project
clang
clang-tools-extra
compiler-rt
eng
libcxx
libcxxabi
libunwind
lld
lldb
llvm
bindings
cmake
docs
examples
include
lib
projects
resources
runtimes
scripts
test
tools
bugpoint
bugpoint-passes
dsymutil
gold
llc
lli
llvm-ar
llvm-as
llvm-as-fuzzer
llvm-bcanalyzer
llvm-c-test
CMakeLists.txt
attributes.c
calc.c
debuginfo.c
diagnostic.c
disassemble.c
echo.cpp
helpers.c
include-all.c
llvm-c-test.h
main.c
metadata.c
module.c
object.c
targets.c
llvm-cat
llvm-cfi-verify
llvm-config
llvm-cov
llvm-cvtres
llvm-cxxdump
llvm-cxxfilt
llvm-demangle-fuzzer
llvm-diff
llvm-dis
llvm-dwarfdump
llvm-dwp
llvm-extract
llvm-go
llvm-isel-fuzzer
llvm-jitlistener
llvm-link
llvm-lto
llvm-lto2
llvm-mc
llvm-mc-assemble-fuzzer
llvm-mc-disassemble-fuzzer
llvm-mcmarkup
llvm-modextract
llvm-mt
llvm-nm
llvm-objcopy
llvm-objdump
llvm-opt-fuzzer
llvm-opt-report
llvm-pdbutil
llvm-profdata
llvm-rc
llvm-readobj
llvm-rtdyld
llvm-shlib
llvm-size
llvm-special-case-list-fuzzer
llvm-split
llvm-stress
llvm-strings
llvm-symbolizer
llvm-xray
lto
msbuild
obj2yaml
opt
opt-viewer
sancov
sanstats
verify-uselistorder
xcode-toolchain
yaml2obj
CMakeLists.txt
LLVMBuild.txt
unittests
utils
.arcconfig
.clang-format
.clang-tidy
.gitattributes
.gitignore
CMakeLists.txt
CODE_OWNERS.TXT
CREDITS.TXT
LICENSE.TXT
LLVMBuild.txt
README.txt
RELEASE_TESTERS.TXT
configure
llvm.spec.in
version.txt.in
nuget
openmp
polly
Directory.Build.props
Directory.Build.targets
NuGet.config
azure-pipelines.yml
build.cmd
build.sh
dir.common.props
global.json
llvm.proj
mxe-Win64.cmake.in
nuget-buildtasks
nunit-lite
roslyn-binaries
rx
xunit-binaries
how-to-bump-roslyn-binaries.md
ikvm-native
llvm
m4
man
mcs
mono
msvc
netcore
po
runtime
samples
scripts
support
tools
COPYING.LIB
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
code_of_conduct.md
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h
148 lines
4.0 KiB
C
148 lines
4.0 KiB
C
/*===-- calc.c - tool for testing libLLVM and llvm-c API ------------------===*\
|
|
|* *|
|
|
|* The LLVM Compiler Infrastructure *|
|
|
|* *|
|
|
|* This file is distributed under the University of Illinois Open Source *|
|
|
|* License. See LICENSE.TXT for details. *|
|
|
|* *|
|
|
|*===----------------------------------------------------------------------===*|
|
|
|* *|
|
|
|* This file implements the --calc command in llvm-c-test. --calc reads lines *|
|
|
|* from stdin, parses them as a name and an expression in reverse polish *|
|
|
|* notation and prints a module with a function with the expression. *|
|
|
|* *|
|
|
\*===----------------------------------------------------------------------===*/
|
|
|
|
#include "llvm-c-test.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
|
|
typedef LLVMValueRef (*binop_func_t)(LLVMBuilderRef, LLVMValueRef LHS,
|
|
LLVMValueRef RHS, const char *Name);
|
|
|
|
static LLVMOpcode op_to_opcode(char op) {
|
|
switch (op) {
|
|
case '+': return LLVMAdd;
|
|
case '-': return LLVMSub;
|
|
case '*': return LLVMMul;
|
|
case '/': return LLVMSDiv;
|
|
case '&': return LLVMAnd;
|
|
case '|': return LLVMOr;
|
|
case '^': return LLVMXor;
|
|
}
|
|
assert(0 && "unknown operation");
|
|
return 0;
|
|
}
|
|
|
|
#define MAX_DEPTH 32
|
|
|
|
static LLVMValueRef build_from_tokens(char **tokens, int ntokens,
|
|
LLVMBuilderRef builder,
|
|
LLVMValueRef param) {
|
|
LLVMValueRef stack[MAX_DEPTH];
|
|
int depth = 0;
|
|
int i;
|
|
|
|
for (i = 0; i < ntokens; i++) {
|
|
char tok = tokens[i][0];
|
|
switch (tok) {
|
|
case '+':
|
|
case '-':
|
|
case '*':
|
|
case '/':
|
|
case '&':
|
|
case '|':
|
|
case '^':
|
|
if (depth < 2) {
|
|
printf("stack underflow\n");
|
|
return NULL;
|
|
}
|
|
|
|
stack[depth - 2] = LLVMBuildBinOp(builder, op_to_opcode(tok),
|
|
stack[depth - 1], stack[depth - 2], "");
|
|
depth--;
|
|
|
|
break;
|
|
|
|
case '@': {
|
|
LLVMValueRef off;
|
|
|
|
if (depth < 1) {
|
|
printf("stack underflow\n");
|
|
return NULL;
|
|
}
|
|
|
|
off = LLVMBuildGEP(builder, param, &stack[depth - 1], 1, "");
|
|
stack[depth - 1] = LLVMBuildLoad(builder, off, "");
|
|
|
|
break;
|
|
}
|
|
|
|
default: {
|
|
char *end;
|
|
long val = strtol(tokens[i], &end, 0);
|
|
if (end[0] != '\0') {
|
|
printf("error parsing number\n");
|
|
return NULL;
|
|
}
|
|
|
|
if (depth >= MAX_DEPTH) {
|
|
printf("stack overflow\n");
|
|
return NULL;
|
|
}
|
|
|
|
stack[depth++] = LLVMConstInt(LLVMInt64Type(), val, 1);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (depth < 1) {
|
|
printf("stack underflow at return\n");
|
|
return NULL;
|
|
}
|
|
|
|
LLVMBuildRet(builder, stack[depth - 1]);
|
|
|
|
return stack[depth - 1];
|
|
}
|
|
|
|
static void handle_line(char **tokens, int ntokens) {
|
|
char *name = tokens[0];
|
|
LLVMValueRef param;
|
|
LLVMValueRef res;
|
|
|
|
LLVMModuleRef M = LLVMModuleCreateWithName(name);
|
|
|
|
LLVMTypeRef I64ty = LLVMInt64Type();
|
|
LLVMTypeRef I64Ptrty = LLVMPointerType(I64ty, 0);
|
|
LLVMTypeRef Fty = LLVMFunctionType(I64ty, &I64Ptrty, 1, 0);
|
|
|
|
LLVMValueRef F = LLVMAddFunction(M, name, Fty);
|
|
LLVMBuilderRef builder = LLVMCreateBuilder();
|
|
LLVMPositionBuilderAtEnd(builder, LLVMAppendBasicBlock(F, "entry"));
|
|
|
|
LLVMGetParams(F, ¶m);
|
|
LLVMSetValueName(param, "in");
|
|
|
|
res = build_from_tokens(tokens + 1, ntokens - 1, builder, param);
|
|
if (res) {
|
|
char *irstr = LLVMPrintModuleToString(M);
|
|
puts(irstr);
|
|
LLVMDisposeMessage(irstr);
|
|
}
|
|
|
|
LLVMDisposeBuilder(builder);
|
|
|
|
LLVMDisposeModule(M);
|
|
}
|
|
|
|
int llvm_calc(void) {
|
|
|
|
llvm_tokenize_stdin(handle_line);
|
|
|
|
return 0;
|
|
}
|