源代码和GCC中文手册可自由下载
缘由:首先单纯的uart驱动,没有什么难度,配置2440寄存器即可实现。
stdio程序移植libc库,实现printf scanf sscanf 等函数,利用串口获取十进制数,转换成十六进制。
功能较简单,但是对于libc库的移植较复杂,其中还涉及gnu assemble语法等等,因此比较感兴趣,想总结一下。
1. 首先是顶层Makefile。
其中一句:
INCLUDEDIR := $(shell pwd)/include
CFLAGS := -Wall -O2
CPPFLAGS := -nostdinc -I$(INCLUDEDIR)
参考gcc中文手册 -nostdinc:
不要在标准系统目录中寻找头文件.只搜索`-I'选项指定的目录(以及当前目录,如果合适).
结合使用`-nostdinc'和`-I-'选项,你可以把包含文件搜索限制在显式指定的目录.
-I$(INCLUDEDIR)
指定当前目录下的include目录为头文件的搜索目录。
2. 查看lib目录下的Makefile
libc.a: $(objs)
${AR} -r -o $@ $^
这里用到了AR命令,AR为arm-linux-ar, 关于ar命令有篇文章讲的较细致可以参考:关于静态链接库
也可以参考 arm-linux-ar --help
ar作用是生成静态库
-r 表示:replace existing or insert new file(s) into the archive, 替换归档库中已存在的文件或插入新的文件。
-o:preserve original dates ,保留原来的日期。
3. Ctype分析
我看了半天,大致明白了Ctype的作用,其实就是分析一个字符是什么类型的
#define __ismask(x) (_ctype[(int)(unsigned char)(x)])
#define isupper(c) ((__ismask(c)&(_U)) != 0)
这里以isupper来举例,isupper顾名思义,判断字符是否为大写。假设字符为大写字母'A' ,则
isupper('A') => ((__ismask(‘A’)&(_U)) != 0) => (_ctype[65] & _U != 0 ) , 查看_ctype[65] 值为 _U|_X ,则 (_U|_X) & _U ,结果为true。
ctype.h 提供了 两个函数: __tolower __toupper ,支持大小写字母转换。
4. __attribute__
有关__attribute__有篇文章较详细:Using GNU C __attribute__
另外还有一份 :The GNU C Reference Manual
继续拓展 attribute,
/* like printf() but to standard error only */
extern void eprintf(const char *format, ...)
__attribute__((format(printf, 1, 2))); /* 1=format 2=params */
extern void eprintf(const char *format, ...)
__attribute__((format(printf, 1, 2))); /* 1=format 2=params */
这里1 代表指定传入函数的第1个参数是格式化字符串, 2 代表指定从函数的第几个参数开始按上述规则进行检查。开始我也没理解文档中英文的含义。参见这篇博客:__attribute__ 详解
5. gcclib.h
/* gcclib.h -- definitions for various functions 'borrowed' from gcc-2.95.3 */
/* I Molton 29/07/01 */
#define BITS_PER_UNIT 8
#define SI_TYPE_SIZE (sizeof (SItype) * BITS_PER_UNIT)
typedef unsigned int UQItype __attribute__ ((mode (QI)));
typedef int SItype __attribute__ ((mode (SI)));
typedef unsigned int USItype __attribute__ ((mode (SI)));
typedef int DItype __attribute__ ((mode (DI)));
typedef int word_type __attribute__ ((mode (__word__)));
typedef unsigned int UDItype __attribute__ ((mode (DI)));
#ifdef __ARMEB__
struct DIstruct {SItype high, low;};
#else
struct DIstruct {SItype low, high;};
#endif
typedef union
{
struct DIstruct s;
DItype ll;
} DIunion;
参考mode的定义:
mode (mode)
This attribute specifies the data type for the declaration--whichevertype corresponds to the modemode. This in effect lets yourequest an integer or floating point type according to its width.
结合上述说明,
typedef unsigned int UQItype __attribute__ ((mode (QI)));
定义了一个新的类型 UQItype, mode属性用来标识它的位宽。QI的定义参见下面两篇文章:
Using vector instructions through built-in functions
具体代码一目了然。
6. va_start va_arg va_end 的使用 参见文章: va_start和va_end使用详解
(待续)