gpt4 book ai didi

LLVM 标准输入/标准输出/标准错误

转载 作者:行者123 更新时间:2023-12-04 02:05:44 25 4
gpt4 key购买 nike

如何申报stdin , stout , 和 stderr (最好是 C 版本)在 LLVM 中?我正在尝试在我正在创建的玩具语言中使用一些 stdio 函数。一个这样的功能是fgets :

char * fgets ( char * str, int num, FILE * stream );

为了使用它,我需要 stdin .所以我写了一些 LLVM API 代码来生成我找到的 FILE 的定义,并声明 stdin外部全局。代码生成了这个:
%file = type { i32, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8*, %marker*, %file*, i32, i32, i64, i16, i8, [1 x i8], i8*, i64, i8*, i8*, i8*, i8*, i64, i32, [20 x i8] }
%marker = type { %marker*, %file*, i32 }

@stdin = external global %file*

但是,当我运行生成的模块时,它给了我这个错误:
Undefined symbols for architecture x86_64:
"_stdin", referenced from:
_main in cc9A5m3z.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

显然,我写的东西没有用。所以我的问题是我必须在 LLVM API 中写什么来声明 stdin , stout , 和 stderr对于 fgets 等功能在玩具语言编译器之类的东西中?

最佳答案

如果有人感兴趣,我找到了我的问题的答案。经过一番激烈的搜索,我找到了获得 stdin 的方法。无需进行 C 扩展即可流式传输:fdopen和制作 FILE一个不透明的结构。

FILE* fdopen (int fildes, const char *mode)

当 fdopen 为文件描述符 ( fildes ) 传递 0 时,它返回 stdin溪流。使用 LLVM API,我生成了以下 LLVM 程序集:
%FILE = type opaque
declare %FILE* @fdopen(i32, i8*)
@r = constant [2 x i8] c"r\00"

然后我能够检索到 stdin使用此调用语句:
%stdin = call %FILE* @fdopen(i32 0, i8* getelementptr inbounds ([2 x i8]* @r, i32 0, i32 0))

关于LLVM 标准输入/标准输出/标准错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10867377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com