CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
154 lines
4.3 KiB
C
154 lines
4.3 KiB
C
/*===-- calc.c - tool for testing libLLVM and llvm-c API ------------------===*\
|
|
|* *|
|
|
|* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|
|
|
|* Exceptions. *|
|
|
|* See https://llvm.org/LICENSE.txt for license information. *|
|
|
|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *|
|
|
|* *|
|
|
|*===----------------------------------------------------------------------===*|
|
|
|* *|
|
|
|* 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;
|
|
|
|
LLVMContextRef C = LLVMGetBuilderContext(builder);
|
|
|
|
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;
|
|
}
|
|
|
|
LLVMTypeRef ty = LLVMInt64TypeInContext(C);
|
|
off = LLVMBuildGEP2(builder, ty, param, &stack[depth - 1], 1, "");
|
|
stack[depth - 1] = LLVMBuildLoad2(builder, ty, 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(LLVMInt64TypeInContext(C), 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;
|
|
|
|
LLVMContextRef C = LLVMContextCreate();
|
|
LLVMModuleRef M = LLVMModuleCreateWithNameInContext(name, C);
|
|
|
|
LLVMTypeRef I64ty = LLVMInt64TypeInContext(C);
|
|
LLVMTypeRef I64Ptrty = LLVMPointerType(I64ty, 0);
|
|
LLVMTypeRef Fty = LLVMFunctionType(I64ty, &I64Ptrty, 1, 0);
|
|
|
|
LLVMValueRef F = LLVMAddFunction(M, name, Fty);
|
|
LLVMBuilderRef builder = LLVMCreateBuilderInContext(C);
|
|
LLVMPositionBuilderAtEnd(builder,
|
|
LLVMAppendBasicBlockInContext(C, 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);
|
|
LLVMContextDispose(C);
|
|
}
|
|
|
|
int llvm_calc(void) {
|
|
|
|
llvm_tokenize_stdin(handle_line);
|
|
|
|
return 0;
|
|
}
|