Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如果将 llvm-ir 融合到豫语中,会否有一些好处呢? #68

Open
nobodxbodon opened this issue Jan 15, 2024 · 8 comments
Open

Comments

@nobodxbodon
Copy link
Contributor

nobodxbodon commented Jan 15, 2024

比如:

 %4 = add i32 %2, %3

相当于

%4的定义是加使用于%2使用于%3

又如:

icmp eq i32 4, 5

相当于

4等于5

如果这样llvm汇编会成为豫语的一个子集?

早先一点对汇编中文化的 相关设想请见此

另外这位 中文化了字节码,如“如果假跳转”。

@UltimatePea
Copy link
Collaborator

UltimatePea commented Jan 16, 2024

可以参考一下目前的豫言编译器生成的LLVM IR,也是使用了大量中文的。
多态列.ll.txt

在我的设计中让LLVMIR成为豫言子集应当并非难事,但LLVM IR是作为编译中的中间表达形式,我并不能够看到中文化LLVM IR的价值,因为所有的LLVM IR能够表达的东西高级语言都可以非常方便地表达。

但如果目的是设计以中文为基底的底层语言,我认为一种可能的形式是 豫言编译器自己的某些中间表示形式,例如这种形式
豫言IR_sample.txt更正:这种可能更直观些豫言IR_sample2.txt),可能会更有价值:其他中文编程语言可以编译到豫言编译器理解的一种IR,然后豫言编译器可以生成后端代码。目前这种形式是以JSON字节码的方式呈现的,比如多态列.擦除后形式.json,豫言编译器中的某些工作可以直接理解JSON字节码并进行变换。不过这些都处于非常早期的阶段。

@nobodxbodon
Copy link
Contributor Author

《Hackers & Painters》的"The dream language"里提到为了解决瓶颈而内嵌的可能性:

A number of languages now compile into byte code,which is then executed by an interpreter. This is usually done to make the implementation easier to port, but it could be a useful language feature. It might be a good idea to make the byte code an official part of the language, and to allow programmers to use inline byte code in bottlenecks. Then such optimizations would be portable too.

另外,个人认为如果是为通用设计的 IR,它的可/易读性要求就更高。

@UltimatePea
Copy link
Collaborator

是的,我之前说错了。豫言本身就可以被用做底层IR。

@UltimatePea
Copy link
Collaborator

从另一个角度来说,豫言是一种高级语言,直接操作内存可行,但通常不推荐这样做。

@nobodxbodon
Copy link
Contributor Author

同一章的《Hackability》一节供参考:

A really good language should be both clean and dirty: cleanly designed, with a small core of well understood and highly orthogonal operators, but dirty in the sense that it lets hackers have their way with it. C is like this. So were the early Lisps. A real hacker's language will always have a slightly raffish character.

个人认为对底层操作的支持可以成为优势,而且可以很大程度上抵消语言性能的压力(《Efficiency》 一节):

A good language, as everyone knows, should generate fast code. But in practice I don't think fast code comes primarily from things you do in the design of the language. As Knuth pointed out long ago, speed only matters in certain critical bottlenecks. And as many programmers have observed since, one is often mistaken about where these bottlenecks are.

So, in practice, the way to get fast code is to have a good profiler, rather than by, say, making the language statically typed. You don't need to know the type of every argument in every call in the program. You do need to be able to declare the types of arguments in the bottlenecks. And even more, you need to be able to find out where the bottlenecks are.

@UltimatePea
Copy link
Collaborator

UltimatePea commented Jan 16, 2024

如果对底层的支持是说这个的话:

it lets hackers have their way with it. C is like this. So were the early Lisps.

豫言目前以及可能支持的底层操作有:

  1. 直接调用C的函数,C函数可以直接读写任何的内存,包括豫言存储的变量数据
  2. 在豫言标准库中集成内存操作函数

这两者都不需要涉及LLVM IR,而可以直接用豫言本身或者C语言,或者LLVM IR语言实现。不需要在豫言中内嵌LLVM IR。

@nobodxbodon
Copy link
Contributor Author

nobodxbodon commented Jan 16, 2024

了解。请问有第一点(直接调用C的函数,C函数可以直接读写任何的内存)的演示或文档吗?

@UltimatePea
Copy link
Collaborator

UltimatePea commented Jan 16, 2024

第一点目前没有文档,因为包管理器和构建系统仍然没有开始搭建,需要等包管理器和构建系统设计开发完成之后才能开始文档撰写。

演示的话可以参考标准库中小数操作的实现,https://github.com/yuyan-lang/yuyan/blob/master/yylib/标准库/数据结构/小数操作%E3%80%82豫 , 小数操作的实现目前是直接调用相关的C函数。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants