From 74bf69110ea5250a6ba795ab68ee067f19235150 Mon Sep 17 00:00:00 2001 From: "xiongyihui3@gmail.com" Date: Thu, 22 Nov 2012 03:39:22 +0000 Subject: [PATCH] add support of arm standard c library, support using armcc to compile lua git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2429 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- components/external/lua/README.TXT | 11 +- components/external/lua/lua/compiler.h | 43 ++++++++ components/external/lua/lua/lrotable.c | 7 +- components/external/lua/lua/lstring.c | 8 +- components/libc/armlibc/Sconscript | 16 +++ components/libc/armlibc/mem_std.c | 19 ++++ components/libc/armlibc/stubs.c | 146 +++++++++++++++++++++++++ 7 files changed, 238 insertions(+), 12 deletions(-) create mode 100644 components/external/lua/lua/compiler.h create mode 100644 components/libc/armlibc/Sconscript create mode 100644 components/libc/armlibc/mem_std.c create mode 100644 components/libc/armlibc/stubs.c diff --git a/components/external/lua/README.TXT b/components/external/lua/README.TXT index d983056198..beb1cec948 100644 --- a/components/external/lua/README.TXT +++ b/components/external/lua/README.TXT @@ -2,7 +2,7 @@ ## 简介 RT-Thread中的Lua修改自[eLua](http://www.eluaproject.net/)的Lua-5.1.4版本。 -采用gcc工具链时,Lua依赖于newlib库,其它工具链暂时还不支持。 +采用gcc工具链时,Lua依赖于newlib库,采用keil时,Lua依赖于arm的标准c库。 启动lua的RAM占用情况 - 标准的lua 17.1904296875 KB - 优化的lua 5.01953125 KB @@ -24,11 +24,12 @@ RT-Thread中的Lua修改自[eLua](http://www.eluaproject.net/)的Lua-5.1.4版本 3.更多的配置项可以在luaconf.h中找到 ## 开发相关 - - 采用gcc工具链时,依赖于newlib,需在rtconfig.h中定义RT_USING_NEWLIB + - 采用gcc工具链时,依赖于newlib,需在rtconfig.h中定义RT_USING_NEWLIB + 采用keil工具链时,依赖于arm的标准c库,需在rtconfig.h中定义RT_USING_ARM_LIBC - 开启编译器对C99的支持,如MDK中,在C/C++选项的Misc Controls输入框中添加--c99 - - 需要在链接脚本中定义_stext和_etext,.ordata*放在两者之间。 - 用于判断数据是read-only和writable。MDK中如何实现?? - - 添加新的模块,参见lexample.c + - 使用gcc时,需要在链接脚本中定义_stext和_etext,.ordata*放在两者之间。用于判断数据是read-only和writable + 使用keil时,需要在分散加载文件中把rodata放在ER_IROM1区 + - 添加新的模块,参见exlibs/lexamplelib.c ## 目录说明 - lua:从eLua获得Lua-5.1.4版本代码 diff --git a/components/external/lua/lua/compiler.h b/components/external/lua/lua/compiler.h new file mode 100644 index 0000000000..9a73de99eb --- /dev/null +++ b/components/external/lua/lua/compiler.h @@ -0,0 +1,43 @@ +/** + * define start/end address of ro data. + * different compiler with different implementation. + */ + +#ifndef __COMPILER_H__ +#define __COMPILER_H__ + +#if defined(__CC_ARM) // armcc + +#warning "Please check scatter file to ensure rodata is in ER_IROM1 region." + +/* symbols reference to the scatter file */ +extern char Image$$ER_IROM1$$Base; +extern char Image$$ER_IROM1$$Limit; + +#define RODATA_START_ADDRESS (&Image$$ER_IROM1$$Base) +#define RODATA_END_ADDRESS (&Image$$ER_IROM1$$Limit) + +#elif defined(__GNUC__) // gcc + +#warning "Please check linker script to ensure rodata is between _stext and _etext." + +/* symbols defined in linker script */ +extern char _stext; +extern char _etext; + +#define RODATA_START_ADDRESS (&_stext) +#define RODATA_END_ADDRESS (&_etext) + +#else // other compilers + +/* Firstly, modify rodata's start/end address. Then, comment the line below */ +#error "Please modify RODATA_START_ADDRESS and RODATA_END_ADDRESS below */ + +/* Perhaps you can use start/end address of flash */ +#define RODATA_START_ADDRESS ((char*)0x08000000) +#define RODATA_END_ADDRESS ((char*)0x08080000) + +#endif + +#endif // __COMPILER_H__ + diff --git a/components/external/lua/lua/lrotable.c b/components/external/lua/lua/lrotable.c index b8840346f9..c8a8688cca 100644 --- a/components/external/lua/lua/lrotable.c +++ b/components/external/lua/lua/lrotable.c @@ -126,9 +126,10 @@ void luaR_getcstr(char *dest, const TString *src, size_t maxsize) { /* Return 1 if the given pointer is a rotable */ #ifdef LUA_META_ROTABLES -extern char _stext; -extern char _etext; + +#include "compiler.h" + int luaR_isrotable(void *p) { - return &_stext <= ( char* )p && ( char* )p <= &_etext; + return RODATA_START_ADDRESS <= (char*)p && (char*)p <= RODATA_END_ADDRESS; } #endif diff --git a/components/external/lua/lua/lstring.c b/components/external/lua/lua/lstring.c index 83c0081545..b0c68115e6 100644 --- a/components/external/lua/lua/lstring.c +++ b/components/external/lua/lua/lstring.c @@ -100,14 +100,14 @@ static TString *luaS_newlstr_helper (lua_State *L, const char *str, size_t l, in return newlstr(L, str, l, h, readonly); /* not found */ } -extern char _stext; -extern char _etext; - static int lua_is_ptr_in_ro_area(const char *p) { #ifdef LUA_CROSS_COMPILER return 0; #else - return p >= &_stext && p <= &_etext; + +#include "compiler.h" + + return p >= RODATA_START_ADDRESS && p <= RODATA_END_ADDRESS; #endif } diff --git a/components/libc/armlibc/Sconscript b/components/libc/armlibc/Sconscript new file mode 100644 index 0000000000..11aba6b4f8 --- /dev/null +++ b/components/libc/armlibc/Sconscript @@ -0,0 +1,16 @@ +Import('rtconfig') +from building import * + +if GetDepend('RT_USING_ARM_LIBC') and rtconfig.CROSS_TOOL != 'keil': + print '================ERROR==============================' + print 'Please use ARM CC compiler if using ARM C library' + print '===================================================' + exit(0) + +cwd = GetCurrentDir() +src = Glob('*.c') +CPPPATH = [cwd] + +group = DefineGroup('libc', src, depend = ['RT_USING_ARM_LIBC'], CPPPATH = CPPPATH) + +Return('group') diff --git a/components/libc/armlibc/mem_std.c b/components/libc/armlibc/mem_std.c new file mode 100644 index 0000000000..7e9c036377 --- /dev/null +++ b/components/libc/armlibc/mem_std.c @@ -0,0 +1,19 @@ + +#include "rtthread.h" + +#pragma import(__use_no_heap) + +void * malloc(int n) +{ + return rt_malloc(n); +} + +void * realloc(void *rmem, rt_size_t newsize) +{ + return rt_realloc(rmem, newsize); +} + +void free(void *rmem) +{ + rt_free(rmem); +} diff --git a/components/libc/armlibc/stubs.c b/components/libc/armlibc/stubs.c new file mode 100644 index 0000000000..17be36ab20 --- /dev/null +++ b/components/libc/armlibc/stubs.c @@ -0,0 +1,146 @@ +/** + * reimplement arm c library's basic functions + */ + +#include +#include + +#include "rtthread.h" + +#pragma import(__use_no_semihosting_swi) + +int remove(const char *filename) +{ + RT_ASSERT(0); + for(;;); +} + +/* rename() */ + +int system(const char *string) +{ + RT_ASSERT(0); + for(;;); +} + +/* Standard IO device handles. */ +#define STDIN 1 +#define STDOUT 2 +#define STDERR 3 + +/* Standard IO device name defines. */ +const char __stdin_name[] = "STDIN"; +const char __stdout_name[] = "STDOUT"; +const char __stderr_name[] = "STDERR"; + +FILEHANDLE _sys_open(const char *name, int openmode) +{ + /* Register standard Input Output devices. */ + if (strcmp(name, __stdin_name) == 0) + return (STDIN); + if (strcmp(name, __stdout_name) == 0) + return (STDOUT); + if (strcmp(name, __stderr_name) == 0) + return (STDERR); + +#ifndef RT_USING_DFS + return 0; +#else + /* TODO: adjust open file mode */ + return open(name, openmode, 0); +#endif +} + +int _sys_close(FILEHANDLE fh) +{ +#ifndef RT_USING_DFS + return 0; +#else + if (fh < 3) + return 0; + + return close(fh); +#endif +} + +int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode) +{ + if (fh == STDIN) + { + /* TODO */ + + return 0; + } + +#ifndef RT_USING_DFS + return 0; +#else + return read(fh, buf, len); +#endif +} + +int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode) +{ + if ((fh == STDOUT) || (fh == STDERR)) + { +#ifndef RT_USING_CONSOLE + return 0; +#else + rt_device_t console_device; + extern rt_device_t rt_console_get_device(void); + + console_device = rt_console_get_device(); + if (console_device != 0) rt_device_write(console_device, 0, buf, len); + return len; +#endif + } + +#ifndef RT_USING_DFS + return 0; +#else + return write(fh, buf, len); +#endif +} + +int _sys_seek(FILEHANDLE fh, long pos) +{ +#ifndef RT_USING_DFS + return 0; +#else + /* TODO: adjust last parameter */ + return lseek(fh, pos, 0); +#endif +} + +int _sys_tmpnam(char *name, int fileno, unsigned maxlength) +{ + return 0; +} + +char *_sys_command_string(char *cmd, int len) +{ + return cmd; +} + +void _ttywrch(int ch) +{ + +} + +void _sys_exit(int return_code) +{ + while (1); +} + +long _sys_flen(FILEHANDLE fh) +{ + return 0; +} + +int _sys_istty(FILEHANDLE fh) +{ + return 0; +} + + +