From 6102bbe2d3b689344d2dd817ae08845b0e7f94de Mon Sep 17 00:00:00 2001 From: jakeallen Date: Thu, 11 Jan 2024 18:17:39 -0800 Subject: [PATCH] sysy_lib --- compiler/runtime/CMakeLists.txt | 31 ++++++++++++ compiler/runtime/include/sylib.h | 31 ++++++++++++ compiler/runtime/src/sylib.c | 83 ++++++++++++++++++++++++++++++++ 3 files changed, 145 insertions(+) create mode 100644 compiler/runtime/CMakeLists.txt create mode 100644 compiler/runtime/include/sylib.h create mode 100644 compiler/runtime/src/sylib.c diff --git a/compiler/runtime/CMakeLists.txt b/compiler/runtime/CMakeLists.txt new file mode 100644 index 0000000..1808e9b --- /dev/null +++ b/compiler/runtime/CMakeLists.txt @@ -0,0 +1,31 @@ +cmake_minimum_required(VERSION 3.21) + +project(SysYRuntime VERSION 1.0 + DESCRIPTION "SysY Language official runtime environment" + LANGUAGES C) + +set(CMAKE_C_STANDARD 99) +set(CMAKE_C_COMPILER "riscv64-unknown-elf-gcc") + +set(INCLUDE_DIRECTORY "${PROJECT_SOURCE_DIR}/include") +include_directories(${INCLUDE_DIRECTORY}) + +set(SOURCE_FILES "${PROJECT_SOURCE_DIR}/src/sylib.c") + +if(BUILD_IR_TESTING) + add_custom_target(sysy-ir ALL + COMMAND clang ${CMAKE_C_FLAGS} + -I ${INCLUDE_DIRECTORY} + -S -emit-llvm + ${SOURCE_FILES} + -o ${CMAKE_CURRENT_BINARY_DIR}/sysy.ll + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + COMMENT "Compiling SysY Runtime Library to LLVM-IR" + SOURCES ${SOURCE_FILES}) +endif(BUILD_IR_TESTING) + +add_library(sysy STATIC ${SOURCE_FILES}) + +# target_compile_options(sysy PUBLIC ${CMAKE_C_FLAGS} -flto) +# target_compile_options(sysy PUBLIC ${CMAKE_C_FLAGS} -emit-llvm -S) +target_compile_options(sysy PRIVATE ${CMAKE_C_FLAGS} --verbose) diff --git a/compiler/runtime/include/sylib.h b/compiler/runtime/include/sylib.h new file mode 100644 index 0000000..de9066b --- /dev/null +++ b/compiler/runtime/include/sylib.h @@ -0,0 +1,31 @@ +#ifndef __SYLIB_H_ +#define __SYLIB_H_ + +#include +#include +#include +/* Input & output functions */ +int getint(),getch(),getarray(int a[]); +float getfloat(); +int getfarray(float a[]); + +void putint(int a),putch(int a),putarray(int n,int a[]); +void putfloat(float a); +void putfarray(int n, float a[]); + +void putf(char a[], ...); + +/* Timing function implementation */ +struct timeval _sysy_start,_sysy_end; +#define starttime() _sysy_starttime(__LINE__) +#define stoptime() _sysy_stoptime(__LINE__) +#define _SYSY_N 1024 +int _sysy_l1[_SYSY_N],_sysy_l2[_SYSY_N]; +int _sysy_h[_SYSY_N], _sysy_m[_SYSY_N],_sysy_s[_SYSY_N],_sysy_us[_SYSY_N]; +int _sysy_idx; +__attribute((constructor)) void before_main(); +__attribute((destructor)) void after_main(); +void _sysy_starttime(int lineno); +void _sysy_stoptime(int lineno); + +#endif diff --git a/compiler/runtime/src/sylib.c b/compiler/runtime/src/sylib.c new file mode 100644 index 0000000..dc3d92c --- /dev/null +++ b/compiler/runtime/src/sylib.c @@ -0,0 +1,83 @@ +#include +#include +#include +#include"sylib.h" +/* Input & output functions */ +int getint(){int t; scanf("%d",&t); return t; } +int getch(){char c; scanf("%c",&c); return (int)c; } +float getfloat(){ + float n; + scanf("%a", &n); + return n; +} + +int getarray(int a[]){ + int n; + scanf("%d",&n); + for(int i=0;i