MoSync Intro - Code a World

MoSync Intro

PengLiang posted @ 2011年7月03日 17:06 in Android with tags mosync android , 4600 阅读

声明:本文完全是按照自己理解所写,不对内容的正确性和准确性负责,请以MoSync的官方文档和代码为准。

MoSync 是一个开源的移动应用的跨平台开发框架。

1. Intro

      当前市面上流行的主要的系统有 Android (包括API3-12), Symbian 60 (v2, v3, v5) , IOS, Win Mobile (5, 6, 6pro), Meego... 如果你想只写一套代码能在上面所有平台都能运行,现在来说只能用Java, 而且Android,Meego还是得单独维护, MoSync 就是这样的一个平台, 你只要写一个MoSync Project,  然后看到右侧的图片了么,选择你的手机型号,双击,大功告成~,你想换一个型号么,选择,双击,OK,还能批量选择生成的。使用Android的手机,它就会生成apk程序给你;使用Symbian的手机,它就会生成sis程序给你;Win Mobile? 自然是cab程序了。
      如果你开发过手机应用,几乎为每个型号都维护一个版本,每个型号都有单独的补丁,看到这里你一定激动万分。

2. MoSync

      MoSync 的设计很宏大,它实际上是一个引擎, 目标是接受各种语言的Project(现在只支持C/C++), 输出满足任意型号手机(现在已支持流行的大部分手机)的程序。 实现这个目标理论上只有一种可能----虚拟机。MoSync的核心其实就是个虚拟机,是一个基于寄存器的虚拟机(jvm 是基于stack 的,Android自带的davik也是基于寄存器的),有自己的指令集, 支持自己的bytecode。这个虚拟机设计的非常小巧,针对移动平台优化,不用预装在手机上,会和你的程序打包在一起, 然后你的程序在手机运行的时候,其实是先启动的这个虚拟机。速度上起码要比jvm快。

       MoSync 的代价是抛弃各个平台的SDK,程序必须按照它自己的API写, 意味着你必须花时间学习它的API,不过这点代价对程序员来说应该没什么问题, 而且其官方文档和示例也很全面。它还定制了Eclipse来作为专用的IDE。

3.  MoSync Building Process

        MoSync定制了GCC作为其编译器,它给GCC增加了一个自己的后端(back-end) 负责把程序编译成其自己的中间语言 MoSync IL, 然后用其链接器Pipe-tool把用到的libs一起链接成二进制程序(相当于java 的 class 文件),最后根据你选的手机型号来打包,把你的程序和对应手机平台的虚拟机打包在一起,生成对应的安装包(apk, sis, cab),就可以安装到手机了。

       用MoSync为Mac开发程序要多走一步程序,因为这时Packaging打包生成的不是安装包,而是一个完整的XCode Project, 你需要用XCode打开这个project, 然后就像你以前开发Mac程序一样编译,安装就行了。

      由此可见,MoSync真是煞费苦心,不惜从中间语言(IL) 逆向重新生成源文件来支持Mac,封闭系统害死人啊。

4. MoSync IL

       每一个虚拟机都有一套自己的中间语言,包括特定的指令集,语法等等。 一般都有两种等价形式-----汇编形式和二进制形式, 只是可读和不可读的区别。

      MoSync 的汇编IL包含了详细的stabs格式的元信息(由gcc -stabs 生成), 通过这些元信息,必要的时候完全可以将这些汇编语言转换回高级语言形式,自然也可以生成源代码,上面说的Mac的方式就是这样操作的。 二进制的bytecode 去掉了这些元信息。

     MoSync IL的共有128个寄存器,常用的有32个[1][2]。指令集也非常简单,没有浮点指令,所有浮点运算都转化为调用相应的函数完成。

Instructions
–Binary:   add, sub, mul, div, divu
–Bitwise Binary:   and, or, xor, sll, srl, sra
–Unary:   neg, not, xh (sign-extend HImode), xb (QImode),
–Data Move:  ld, ld.h, ld.b
–Branch:   jp, jc, call, ret, push, pop, case

5. MoSync Pipe-Tool

     Pipe-Tool 是一个复杂的工具,包含了资源编译器, 链接器,还有代码优化的功能[3]。从汇编语言生成源代码的活也是它干的,已经实现可以从汇编语言生成C++或者Java的源代码,无论你源码原来是用什么写的,当然目前你只能用c/c++来写... 下面是从其文档上摘录的几点特别的地方:

Code Verification
–Bounds checking, Division error detection
–Stack abuse detection, Function tracing
Optimization
–Converting immediate values to constant register values
–Eliminating jump-to-jump occurrences
–Rewriting functions to perform constant value inlining
–Analyzing and optimizing program flow
–Analyzing and optimizing register life
–Performing memory index reduction and adaptive index scaling
Dead code elimination (lowest level )
–down to individual instructions and bytes

6. MoSync Runtime Core

         Runtime 是MoSync 的核心, 就是虚拟机的部分。虚拟机最基本的功能就是一行行的解释执行bytecode,或者JIT的形式将bytecode转换成目标机器native的代码,然后再执行。 这两种形式MoSync都实现了,叫VM Core 和 Recompiler Core。 官方还有一种Core叫Generated Core, 说的就是为mac支持的那种重新生成源代码的形式, 这里就不叫它Core了... 我觉的它不算Runtime的部分。

        第一个问题就是要保证MoSync 这个虚拟机本身能在所有手机平台高效运行,这就没什么好办法了,只能每个手机型号维护一个版本,尽量为每个型号进行优化了(这就是牺牲一人,幸福大家的精神啊)... 不过可想而知,为JVM实现的版本,就是JVM虚拟机上再运行MoSync虚拟机,效率肯定不会高。VM Core 最终是要抛弃的,Pipe-Toole 将为只能运行Java程序的手机直接生成Java bytecode, 省得再加一层虚拟机。Recompiler core (其实就是JIT,不知为什么非得再起个名字,弄得我查了半天与JIT的区别) 目前只实现了 win mobile 5 以上 和 symbian s60 以上的版本,程序再启动时先将代码转换成目标机native的代码,这样会有一点延时启动的效果,但是转换后的代码效率就要看转换引擎了。

        关于Android版本的Recompiler Core正在开发当中, 目前采用的还是VM Core。因为涉及到Android 本身应用就不是Native的形式,所以Recompiler Core实现上有些复杂, 这点可以参考[4].

7. Benchmark

       目前无论官方还是第三方都找不到任何MoSync 相关的Benchmark,无法给出准确的数据说明采用MoSync的实现对效率到底有多少影响。我自己做了一个关于win mobile 5 上Native程序与MoSync Recompiler Core 之间简单的Benchmark, 方法是用Zlib对1M随机文本进行内存压缩。 首先要移植Zlib 到MoSync,其实很简单,MoSync的标准C库采用 newlib, 所以直接把代码组织成 MoSync Project, 然后编译就行了(MoSync IDE 默认的是不是采用newlib, 需要设置一下[5])。 Native版本的Win Mobile Zlib 直接从网上下载的 zilbCE。使用同样的版本1.2.3,因为我能找到的最新 zlibCE就是1.2.3了... 采用内存压缩1M 随机文本也是无奈之举, 因为用MoSync 标准C库的 fopen 实现有问题, 无法读取手机上的文件, 而手机又很老了,能跑起来的程序最大只能处理1M文本了。 不过结果还可以还是有可比性的,因为每次都是随机生成的文本, 分别取10次运行的平均结果, MoSync程序用了8000多毫秒, Native的程序只用了不到4000毫秒, 还是有点差距的。

 8. Conclusion

       MoSync还在开发当中,能做到现在的程度已经很不错了,为了跨平台的可靠性, 为了不让程序员们不停的进行各种版本的测试和调试,MoSync自己本身就必须做更多的手机平台测试,而且要保证一致性。

      最重要的是它的用户体验非常好,你只需要维护一套代码,然后去选择手机就可以了,应该没有比这更简单的策略了吧。所以还是很值得期待的。

      更多详细内容请参考官方文档

Reference

[1] http://www.mosync.com/content/how-many-number-registers-used-mosync-il

[2] http://www.mosync.com/files/MoSync%20IL%20register%20set.pdf

[3] http://www.mosync.com/documentation/manualpages/toolchain

[4] Generally questions affect to the MoSync Build Process http://www.mosync.com/node/6690

[5] http://www.mosync.com/content/sdl-stl-standard-cc-libs

 

    

Avatar_small
babysitting services 说:
2019年10月24日 21:36

The two residential along with commercial properties require sparkling clean up floors. In addition to professional serious cleaning, you could also engage throughout regular cleaning to make certain the floors tend not to turn evasive or tainted. Given underneath are many of the safest along with quickest approach to complete your current daily floorboards cleaning undertaking.

Avatar_small
part time maids in d 说:
2021年8月26日 14:07

Cleaning your property is not always easy. It is undoubtedly an art this shows how you cleaned the home and sorted out it. DIALAMAID Carpet cleaners advice you clean the home which meets very high standards. A well-organized cleaning approach makes but not only your dwelling look elegant and with practices.

Avatar_small
maid services dubai 说:
2021年9月18日 13:06

In order your bare floors to stink good, you might use a refresher. It will be easily included in the trading markets. The several other thing can be done is to help make your possess home-made refresher. Not necessarily so pricy. All that ingredients are located in your family home. You really need liquid, white white vinegar, baking soda pop and gasoline.

Avatar_small
monthly maid service 说:
2021年9月29日 17:45

Over the years, workload has gotten heavier and peoples' jobs have become more specialized. Gone are the days when you were expected to keep your own office vacuumed. Companies hire out these jobs now and for good reason. Professional cleaners do an excellent job for the cost of the service. Companies realize the value of having their businesses cleaned by someone devoted to the field.

Avatar_small
full time maids in d 说:
2023年8月31日 18:27

Nowadays when the normal hours at a work seven days are above the usual 40 days, many people could find it complicated to account balance their get the job done life utilizing home everyday life. Hence, your family unit time may trimmed down so that the house hold tidy. And best of all working other hours once the already 8+ you worked and a job.

Avatar_small
seo service UK 说:
2024年1月11日 22:23

Strong blog. I acquired several nice info. I?ve been keeping a watch on this technology for a few time. It?utes attention-grabbing the method it retains totally different, however many of the primary components remain a similar. have you observed a lot change since Search engines created their own latest purchase in the field?

Avatar_small
사나이벳 도메인 说:
2024年1月16日 14:03

I have to express some thanks to you for bailing me out of this challenge. After scouting throughout the internet and coming across concepts which were not powerful, I was thinking my entire life was done. Living devoid of the answers to the issues you have sorted out as a result of your article content is a crucial case, as well as the kind that would have badly damaged my entire career if I hadn’t encountered your blog. Your primary knowledge and kindness in controlling almost everything was precious. I am not sure what I would have done if I had not encountered such a subject like this.

Avatar_small
토토매거진 说:
2024年1月16日 14:08

Being grateful for for your post. I know that within today’s complicated world, folks have many beliefs and this has made it to be really hard for learners just like me. However, you have made the idea very easy for me to comprehend and I now know the correct thing. Your continued reputation among the top experts with this topic may be enhanced through words of appreciation from followers like me. Thanks, once more.

Avatar_small
토토사이트 说:
2024年1月16日 15:10

It’s the best time to make a few plans for the longer term and it is time to be happy. I have learn this post and if I may just I wish to counsel you few attention-grabbing issues or advice. Maybe you can write next articles relating to this article. I wish to read even more things approximately it!|

Avatar_small
시스템배팅사이트 说:
2024年1月16日 15:14

Today, I went to the beachfront with my kids. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is completely off topic but I had to tell someone!|

Avatar_small
토토사이트 说:
2024年1月16日 15:23

Nice post. I discover something tougher on various blogs everyday. Most commonly it is stimulating to see content off their writers and practice a little there. I’d would prefer to use some using the content on my own weblog whether you do not mind. Natually I’ll supply you with a link with your web blog. Thanks for sharing.

Avatar_small
구조대도메인 说:
2024年1月16日 15:31

Does your blog have a contact page? I’m having a tough time locating it but, I’d like to shoot you an e-mail. I’ve got some ideas for your blog you might be interested in hearing. Either way, great blog and I look forward to seeing it grow over time.|

Avatar_small
가입머니즉시지급 说:
2024年1月16日 15:54

Sweet blog! I found it while browsing on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Cheers| Hi. i think that you should add captcha to your blog.

Avatar_small
토팡주소 说:
2024年1月16日 15:54

Fantastic blog! Do you have any tips and hints for aspiring writers? I’m planning to start my own site soon but I’m a little lost on everything. Would you suggest starting with a free platform like WordPress or go for a paid option? There are so many options out there that I’m totally overwhelmed .. Any suggestions? Bless you!|

Avatar_small
토토핫 보증업체 说:
2024年1月16日 16:03

Greetings, I think your blog could be having internet browser compatibility issues. When I look at your website in Safari, it looks fine however, when opening in IE, it has some overlapping issues. I merely wanted to give you a quick heads up! Apart from that, great website!|

Avatar_small
안전공원추천 说:
2024年1月16日 16:10

Hmm is anyone else experiencing problems with the images on this blog loading? I’m trying to figure out if its a problem on my end or if it’s the blog. Any suggestions would be greatly appreciated.|Very nice post. I just stumbled upon your blog and wanted to say that I have truly enjoyed browsing your blog posts. In any case I will be subscribing to your rss feed and I hope you write again soon!|

Avatar_small
개구리먹튀검증 说:
2024年1月16日 16:17

An impressive share, I just given this onto a colleague who had previously been performing a small analysis on this. And hubby in fact bought me breakfast simply because I found it for him.. smile. So permit me to reword that: Thnx for the treat! But yeah Thnkx for spending any time go over this, I find myself strongly regarding this and really like reading much more about this topic. If you can, as you grow expertise, does one mind updating your blog post with increased details? It can be extremely helpful for me. Massive thumb up with this blog post!

Avatar_small
슈어맨주소 说:
2024年1月16日 16:21

I just want to mention I am new to blogging and site-building and really savored this blog site. Very likely I’m likely to bookmark your blog post . You surely come with tremendous article content. Appreciate it for sharing with us your web-site. There may be clearly a bunch to understand this particular. I believe you’ve made certain pleasant points within features also.

Avatar_small
카지노롤링총판 说:
2024年1月16日 16:29

Strong blog. I acquired several nice info. I?ve been keeping a watch on this technology for a few time. It?utes attention-grabbing the method it retains totally different, however many of the primary components remain a similar. have you observed a lot change since Search engines created their own latest purchase in the field?

Avatar_small
엔트리파워볼 说:
2024年1月16日 16:30

Hi are using WordPress for your blog platform? I’m new to the blog world but I’m trying to get started and create my own. Do you require any coding knowledge to make your own blog? Any help would be really appreciated!| I have read so many posts concerning the blogger lovers however this paragraph is in fact a fastidious article, keep it up.|

Avatar_small
사설토토추천 说:
2024年1月16日 16:35

Being grateful for for your post. I know that within today’s complicated world, folks have many beliefs and this has made it to be really hard for learners just like me. However, you have made the idea very easy for me to comprehend and I now know the correct thing. Your continued reputation among the top experts with this topic may be enhanced through words of appreciation from followers like me. Thanks, once more.

Avatar_small
해외안전놀이터 说:
2024年1月16日 16:41

Hi there I am so excited I found your webpage, I really found you by error, while I was researching on Askjeeve for something else, Anyhow I am here now and would just like to say thank you for a incredible post and a all round interesting blog (I also love the theme/design), I don’t have time to look over it all at the minute but I have saved it and also added in your RSS feeds, so when I have time I will be back to read much more, Please do keep up the awesome work.|

Avatar_small
크랩스게임방법 说:
2024年1月16日 16:42

The vacation delivers on offer are : believed a selection of some of the most selected and additionally budget-friendly global. Any of these lodgings tend to be very used along units may accented by means of pretty shoreline supplying crystal-clear turbulent waters, concurrent with the Ocean. hotels packages

Avatar_small
안전토토사이트 说:
2024年1月16日 16:45

I have to express some thanks to you for bailing me out of this challenge. After scouting throughout the internet and coming across concepts which were not powerful, I was thinking my entire life was done. Living devoid of the answers to the issues you have sorted out as a result of your article content is a crucial case, as well as the kind that would have badly damaged my entire career if I hadn’t encountered your blog. Your primary knowledge and kindness in controlling almost everything was precious. I am not sure what I would have done if I had not encountered such a subject like this.

Avatar_small
먹튀검증사이트 说:
2024年1月16日 16:50

The vacation delivers on offer are : believed a selection of some of the most selected and additionally budget-friendly global. Any of these lodgings tend to be very used along units may accented by means of pretty shoreline supplying crystal-clear turbulent waters, concurrent with the Ocean. hotels packages

Avatar_small
แทงบอลออนไลน์ 说:
2024年1月16日 16:53

you’re actually a just right webmaster. The web site loading pace is incredible. It seems that you are doing any unique trick. Moreover, The contents are masterpiece. you have done a wonderful job on this matter! you’re in point of fact a excellent webmaster. The web site loading velocity is amazing. It sort of feels that you’re doing any unique trick. Also, The contents are masterpiece. you’ve done a wonderful job on this matter!

Avatar_small
토토경비대 说:
2024年1月16日 16:55

Hmm is anyone else experiencing problems with the images on this blog loading? I’m trying to figure out if its a problem on my end or if it’s the blog. Any suggestions would be greatly appreciated.|Very nice post. I just stumbled upon your blog and wanted to say that I have truly enjoyed browsing your blog posts. In any case I will be subscribing to your rss feed and I hope you write again soon!|

Avatar_small
동행복권로또 说:
2024年1月16日 17:05

Does your blog have a contact page? I’m having a tough time locating it but, I’d like to shoot you an e-mail. I’ve got some ideas for your blog you might be interested in hearing. Either way, great blog and I look forward to seeing it grow over time.|

Avatar_small
양방사이트 说:
2024年1月16日 17:10

Fantastic blog! Do you have any tips and hints for aspiring writers? I’m planning to start my own site soon but I’m a little lost on everything. Would you suggest starting with a free platform like WordPress or go for a paid option? There are so many options out there that I’m totally overwhelmed .. Any suggestions? Bless you!|

Avatar_small
엔트리파워사다리 说:
2024年1月16日 17:13

Hey there would you mind stating which blog platform you’re working with? I’m going to start my own blog soon but I’m having a difficult time choosing between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design and style seems different then most blogs and I’m looking for something completely unique. P.S My apologies for getting off-topic but I had to ask!|

Avatar_small
메이저사이트추천 说:
2024年1月16日 18:07

Greetings, I think your blog could be having internet browser compatibility issues. When I look at your website in Safari, it looks fine however, when opening in IE, it has some overlapping issues. I merely wanted to give you a quick heads up! Apart from that, great website!|

Avatar_small
안전놀이터 说:
2024年1月16日 18:12

Hmm is anyone else experiencing problems with the images on this blog loading? I’m trying to figure out if its a problem on my end or if it’s the blog. Any suggestions would be greatly appreciated.|Very nice post. I just stumbled upon your blog and wanted to say that I have truly enjoyed browsing your blog posts. In any case I will be subscribing to your rss feed and I hope you write again soon!|

Avatar_small
토토사이트 说:
2024年1月16日 18:22

I just want to mention I am new to blogging and site-building and really savored this blog site. Very likely I’m likely to bookmark your blog post . You surely come with tremendous article content. Appreciate it for sharing with us your web-site. There may be clearly a bunch to understand this particular. I believe you’ve made certain pleasant points within features also.

Avatar_small
카지노게임 说:
2024年1月16日 18:31

I am extremely impressed with your writing skills as well as with the layout on your weblog. Is this a paid theme or did you customize it yourself? Either way keep up the nice quality writing, it’s rare to see a great blog like this one these days.| Informative, pretty much as I had come to expect from this site.

Avatar_small
사설토토 说:
2024年1月16日 18:36

Being grateful for for your post. I know that within today’s complicated world, folks have many beliefs and this has made it to be really hard for learners just like me. However, you have made the idea very easy for me to comprehend and I now know the correct thing. Your continued reputation among the top experts with this topic may be enhanced through words of appreciation from followers like me. Thanks, once more.

Avatar_small
메이저추천인코드 说:
2024年1月16日 18:42

Hi there I am so excited I found your webpage, I really found you by error, while I was researching on Askjeeve for something else, Anyhow I am here now and would just like to say thank you for a incredible post and a all round interesting blog (I also love the theme/design), I don’t have time to look over it all at the minute but I have saved it and also added in your RSS feeds, so when I have time I will be back to read much more, Please do keep up the awesome work.|

Avatar_small
먹튀검증 说:
2024年1月16日 18:49

I just want to mention I am new to blogging and site-building and really savored this blog site. Very likely I’m likely to bookmark your blog post . You surely come with tremendous article content. Appreciate it for sharing with us your web-site. There may be clearly a bunch to understand this particular. I believe you’ve made certain pleasant points within features also.

Avatar_small
메이저공원가입 说:
2024年1月16日 19:09

I know this if off topic but I’m looking into starting my own weblog and was wondering what all is needed to get set up? I’m assuming having a blog like yours would cost a pretty penny? I’m not very web savvy so I’m not 100 certain. Any tips or advice would be greatly appreciated. Many thanks|

Avatar_small
사설토토 说:
2024年1月16日 19:23

Nice post. I discover something tougher on various blogs everyday. Most commonly it is stimulating to see content off their writers and practice a little there. I’d would prefer to use some using the content on my own weblog whether you do not mind. Natually I’ll supply you with a link with your web blog. Thanks for sharing.

Avatar_small
먹튀검증소주소 说:
2024年1月16日 19:27

It’s the best time to make a few plans for the longer term and it is time to be happy. I have learn this post and if I may just I wish to counsel you few attention-grabbing issues or advice. Maybe you can write next articles relating to this article. I wish to read even more things approximately it!|

Avatar_small
안전토토사이트 가입 说:
2024年1月16日 19:42

you’re actually a just right webmaster. The web site loading pace is incredible. It seems that you are doing any unique trick. Moreover, The contents are masterpiece. you have done a wonderful job on this matter! you’re in point of fact a excellent webmaster. The web site loading velocity is amazing. It sort of feels that you’re doing any unique trick. Also, The contents are masterpiece. you’ve done a wonderful job on this matter!


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee